<template>
  <div class="manage-container">
    <el-table :data="tableData" stripe border style="width: 100%">
      <el-table-column
        fixed
        prop="deviceName"
        :label="$t('dataEnquiry.zdmc')"
        width="150"
        align="center"
      ></el-table-column>
      <el-table-column
        fixed
        prop="sensorName"
        :label="$t('siteDetails.cgqmc')"
        width="150"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="alarmTime"
        :label="$t('alarm.bjsj')"
        width="200"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="alarmLevel"
        :label="$t('alarm.bjjb')"
        width="200"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="alarmCategory"
        :label="$t('alarm.bjlb')"
        width="200"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="alarmContent"
        :label="$t('alarm.bjnr')"
        width="300"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="handlerName"
        :label="$t('alarm.clr')"
        width="120"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="status"
        :label="$t('dataEnquiry.zt')"
        width="120"
        align="center"
      >
        <template #default="{ row }">
          <el-tag
            v-if="row.status == '已派工'"
            key="已派工"
            effect="dark"
            type="success"
            size="mini"
          >
            {{ $t('alarm.ypg') }}
          </el-tag>
          <el-tag
            v-if="row.status == '未派工'"
            key="未派工"
            effect="dark"
            type="danger"
            size="mini"
          >
            {{ $t('alarm.wpg') }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="remark"
        :label="$t('alarm.bz')"
        width="120"
        align="center"
      ></el-table-column>
      <el-table-column fixed="right" :label="$t('defalult.sc')" align="center">
        <template #default="{ row }">
          <el-button type="text" @click="handleDelete(row)">
            {{ $t('defalult.sc') }}
          </el-button>
          <el-button
            v-if="row.status == '未派工'"
            type="text"
            @click="handleTask(row)"
          >
            {{ $t('alarm.zgd') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :background="background"
      :current-page="queryForm.page"
      :layout="layout"
      :page-size="queryForm.size"
      :total="total"
      style="text-align: right"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    ></el-pagination>
    <add ref="add"></add>
    <to-task ref="toTask"></to-task>
  </div>
</template>
<script>
import {
  getCurrentData,
  alarmLevelDict,
  alarmCategoryDict,
  doDelete,
  alarmExport,
} from '@/api/alarm';
import ajax from '@/api/download';
import add from './add';
import toTask from './toTask';
export default {
  components: {
    add,
    toTask,
  },
  data() {
    return {
      tableData: [],
      lazy: true,
      layout: 'total, sizes, prev, pager, next, jumper',
      total: 0,
      background: true,
      listLoading: false,
      elementLoadingText: '正在加载...',
      levelData: [],
      categoryData: [],
      statusData: [
        {
          value: 3,
          label: 'all',
        },
        {
          value: 0,
          label: '未派工',
        },
        {
          value: 1,
          label: '已派工',
        },
      ],
      queryForm: {
        page: 1,
        size: 20,
        projectId: '',
      },
    };
  },
  created() {
    this.queryForm.projectId = sessionStorage.getItem('projectId');
    this.getLevel();
    this.getCategory();
    setTimeout(() => {
      this.fetchData();
    }, 1000);
  },
  methods: {
    async fetchData() {
      const { data } = await getCurrentData(this.queryForm);
      this.tableData = data.items;
      this.total = data.total;
    },
    showData() {
      this.fetchData();
    },
    save() {
      this.$refs['add'].showEdit();
    },
    //报警级别
    async getLevel() {
      const { data } = await alarmLevelDict();
      this.levelData = data;
    },
    //报警类别
    async getCategory() {
      const { data } = await alarmCategoryDict();
      this.categoryData = data;
    },
    //导出
    async exportData() {
      const url = await alarmExport(this.queryForm);
      ajax.downloadFile(url, { fileName: '报警记录.xls' });
    },
    //搜索
    search() {
      this.fetchData();
    },
    resetForm() {
      this.$refs.queryForm.resetFields();
    },
    //转工单
    handleTask(row) {
      console.log(this.tableData);
      this.$refs['toTask'].showEdit(row);
    },
    //删除
    deletes(dis) {
      const that = this;
      this.$confirm('你确定要删除当前项吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          const { msg } = doDelete(dis);
          this.$message({
            type: 'success',
            message: msg == undefined ? '删除成功' : msg,
          });
          setTimeout(function () {
            that.fetchData();
          }, 1000);
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除',
          });
        });
    },
    handleDelete(row) {
      const that = this;
      that.deletes([row.id]);
    },
    handleQuery() {
      this.queryForm.page = 1;
      this.fetchData();
    },
    handleSizeChange(val) {
      this.queryForm.size = val;
      this.fetchData();
    },
    handleCurrentChange(val) {
      this.queryForm.page = val;
      this.fetchData();
    },
  },
};
</script>
