<template>
  <div
    class="ele-body"
    :style="
      $route.query.isDrawer
        ? {
            position: 'fixed',
            height: '100vh',
            top: '0',
            bottom: '0',
            left: '0',
            right: '0',
            zIndex: 1000,
            background: '#fff'
          }
        : {}
    "
  >
    <el-card shadow="never" v-loading="loading">
      <message-search @search="reload"> </message-search>
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :pageSizes="tablePageSizes"
        :columns="columns"
        :datasource="datasource"
        cache-key="systemRoleTable"
      >
        <template v-slot:action="{ row }">
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="openEdit(row)"
            v-if="
              !row.handleStatus && $hasPermission('eam:alarmlogsheet:update')
            "
          >
            处理
          </el-link>
        </template>
        <template v-slot:triggerCount="{ row }">
          <el-link type="primary" :underline="false" @click="triggerCount(row)">
            {{ row.triggerCount }}
          </el-link>
        </template>
        <template v-slot:deviceName="{ row }">
          <el-link
            type="primary"
            :underline="false"
            @click="openEdit(row, 'view')"
          >
            {{ row.deviceName }}
          </el-link>
        </template>
      </ele-pro-table>
    </el-card>
    <processDialog ref="processDialogRef" @reload="reload"></processDialog>

    <!-- 告警次数详情弹窗 -->
    <el-dialog
      title="告警详情"
      :visible.sync="triggerDialogVisible"
      width="70%"
      append-to-body
    >
      <el-table :data="triggerList" border style="width: 100%">
        <el-table-column type="index" label="序号" width="55" align="center" />
        <el-table-column
          prop="description"
          label="告警描述"
          align="center"
          show-overflow-tooltip
          min-width="180"
        />
        <el-table-column
          prop="triggerTime"
          label="告警时间"
          align="center"
          show-overflow-tooltip
          min-width="160"
        />
        <el-table-column
          label="告警内容"
          align="center"
          show-overflow-tooltip
          min-width="200"
        >
          <template slot-scope="{ row }">
            <div v-if="row._deviceData && row._deviceData.length">
              <div v-for="(item, index) in row._deviceData" :key="index">
                点位：{{ item.attributeName}}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;    告警值：{{ item.thresholdValue }}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  当前值： {{ item.value }} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 差值：{{
                  parseFloat((item.value - item.thresholdValue).toFixed(3))
                }}
              </div>
            </div>
            <span v-else>-</span>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
  import MessageSearch from './components/message-search.vue';
  import processDialog from './components/processDialog.vue';
  import { alarmlogsheetPage, getDetail } from '@/api/warning/index.js';

  import dictMixins from '@/mixins/dictMixins';
  export default {
    mixins: [dictMixins],
    components: {
      MessageSearch,
      processDialog
    },
    data() {
      return {
        // 表格列配置
        columns: [
          {
            columnKey: 'index',
            label: '序号',
            type: 'index',
            width: 55,
            align: 'center',
            showOverflowTooltip: true,
            fixed: 'left'
          },
          {
            prop: 'alarmLevel',
            label: '告警级别',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 80,
            formatter: (_row, _column, cellValue) => {
              const map = {
                1: '轻微',
                2: '中等',
                3: '严重',
                4: '紧急',
                5: '致命'
              };
              return map[cellValue] || '-';
            }
          },
          {
            prop: 'deviceCode',
            label: '设备编号',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'deviceName',
            label: '设备名称',
            slot: 'deviceName',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 200
          },
          {
            prop: 'description',
            label: '告警描述',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 180
          },
          {
            prop: 'generateDocumentType',
            label: '生成单据类型',
            formatter: (_row, _column, cellValue) => {
              let message =
                cellValue == 1
                  ? '巡点检'
                  : cellValue == 2
                  ? '保养'
                  : cellValue == 3
                  ? '维修'
                  : cellValue == 5
                  ? '量具送检'
                  : '';
              if (_row.orderStatusName) {
                message += ' (' + _row.orderStatusName + ')';
              } else if (message) {
                message += ' (待派单)';
              }
              return message;
            },
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'generateDocumentCode',
            label: '生成单据编码',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'triggerCount',
            slot: 'triggerCount',
            label: '告警次数',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'alarmTime',
            label: '首次告警时间',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 160,
            formatter: (_row, _column, cellValue) => {
              return this.$util.toDateString(cellValue);
            }
          },
          {
            prop: 'lastTriggerTime',
            label: '最近告警时间',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 160,
            formatter: (_row, _column, cellValue) => {
              return this.$util.toDateString(cellValue);
            }
          },

          {
            prop: 'handleStatus',
            label: '处理状态',
            align: 'center',
            formatter: (_row, _column, cellValue) => {
              return { 0: '未处理', 1: '处理中', 2: '已处理' }[cellValue];
            },
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'handleUserName',
            label: '处理人',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'remark',
            label: '处理意见',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            columnKey: 'action',
            label: '操作',
            width: 150,
            align: 'center',
            resizable: false,
            slot: 'action',
            showOverflowTooltip: true
          }
        ],
        // 加载状态
        loading: false,
        triggerDialogVisible: false,
        triggerList: []
      };
    },
    computed: {},
    created() {
      this.requestDict('规则状态');
    },
    methods: {
      /* 表格数据源 */
      datasource({ page, limit, where, order }) {
        return alarmlogsheetPage({
          pageNum: page,
          size: limit,
          ...where
        });
      },
      openEdit(row, type) {
        this.$refs.processDialogRef.open(row, type);
      },
      triggerCount(row) {
        getDetail(row.id).then((res) => {
          let list = Array.isArray(res) ? res : [];
          list = list.map((item) => {
            let deviceDataArr = [];
            if (item.deviceData) {
              try {
                const parsed = JSON.parse(item.deviceData);
                deviceDataArr = Array.isArray(parsed) ? parsed : [];
              } catch (e) {
                deviceDataArr = [];
              }
            }
            return {
              ...item,
              _deviceData: deviceDataArr
            };
          });
          this.triggerList = list;
          this.triggerDialogVisible = true;
        });
      },
      /* 刷新表格 */
      reload(where) {
        this.$refs.table.reload({ page: 1, where, ruleType: 2 });
      }
    }
  };
</script>

<style lang="scss" scoped></style>
