<template>
  <div class="manage-container">
    <div class="manage-input">
      <el-form ref="queryForm" :model="queryForm" label-width="100px">
        <vab-query-form>
          <el-form-item
            width="100"
            prop="deviceName"
            :label="$t('dataEnquiry.zdmc')"
          >
            <el-input
              v-model="queryForm.deviceName"
              size="small"
              :placeholder="$t('dataEnquiry.qsrzdmc')"
            ></el-input>
          </el-form-item>
          <el-form-item width="100" prop="handlerName" :label="$t('alarm.clr')">
            <el-input
              v-model="queryForm.handlerName"
              size="small"
              :placeholder="$t('alarm.qsrclr')"
            ></el-input>
          </el-form-item>
          <el-form-item width="100" prop="time" :label="$t('defalult.rq')">
            <el-date-picker
              v-model="queryForm.date"
              size="small"
              type="daterange"
              align="right"
              :start-placeholder="$t('defalult.ksrq')"
              :end-placeholder="$t('defalult.jsrq')"
              @change="changeTime"
            ></el-date-picker>
          </el-form-item>
        </vab-query-form>
      </el-form>
    </div>
    <div class="manage-input">
      <el-form ref="queryForm" :model="queryForm" label-width="100px">
        <vab-query-form>
          <el-form-item width="100" prop="alarmLevel" :label="$t('alarm.bjjb')">
            <el-select
              v-model="queryForm.alarmLevel"
              size="small"
              :placeholder="$t('alarm.bjjb')"
            >
              <el-option
                v-for="item in levelData"
                :key="item.dataCode"
                :label="item.dataValue"
                :value="item.dataCode"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item
            width="100"
            prop="alarmCategory"
            :label="$t('alarm.bjlb')"
          >
            <el-select
              v-model="queryForm.alarmCategory"
              size="small"
              :placeholder="$t('alarm.bjlb')"
            >
              <el-option
                v-for="item in categoryData"
                :key="item.dataCode"
                :label="item.dataValue"
                :value="item.dataCode"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item width="100" prop="date" :label="$t('dataEnquiry.zt')">
            <el-select
              v-model="queryForm.status"
              size="small"
              :placeholder="$t('dataEnquiry.zt')"
            >
              <el-option
                v-for="item in statusData"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </vab-query-form>
      </el-form>
    </div>
    <div class="manage-button">
      <el-button size="small" @click="resetForm()">
        {{ $t('defalult.chongz') }}
      </el-button>
      <el-button type="primary" size="small" @click="search">
        {{ $t('defalult.cx') }}
      </el-button>
      <el-button type="primary" size="small" @click="save">
        {{ $t('defalult.tj') }}
      </el-button>
      <el-button type="primary" size="small" @click="exportData">
        {{ $t('defalult.dc') }}
      </el-button>
    </div>
    <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.cz')" 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="tack"></to-task>
  </div>
</template>
<script>
import { getDate, getPastTime } from '@/common/times';
import toTask from './toTask';
import {
  getAlarm,
  alarmLevelDict,
  alarmCategoryDict,
  doDelete,
  alarmExport,
} from '@/api/alarm';
import ajax from '@/api/download';
import add from './add';
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: '',
        deviceName: '',
        handlerName: '',
        alarmLevel: '',
        alarmCategory: '',
        date: '',
        start: '',
        end: '',
        status: 3,
      },
    };
  },
  created() {
    this.queryForm.projectId = sessionStorage.getItem('projectId');
  },
  methods: {
    showData() {
      this.setDefaultTime();
      this.getLevel();
      this.getCategory();
      this.fetchData();
    },
    changeTime() {
      this.queryForm.start = getDate(this.queryForm.date[0]);
      this.queryForm.end = getDate(this.queryForm.date[1]);
    },
    setDefaultTime() {
      this.queryForm.date = getPastTime(1);
      this.queryForm.start = getDate(this.queryForm.date[0]);
      this.queryForm.end = getDate(this.queryForm.date[1]);
    },
    async fetchData() {
      const { data } = await getAlarm(this.queryForm);
      this.tableData = data.items;
      this.total = data.total;
    },
    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();
    },
    //转工单
    async handleTask(row) {
      this.$refs['tack'].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>

<style lang="scss" scoped>
.el-input {
  width: 200px !important;
}
.el-select {
  width: 200px !important;
}
.manage-button {
  padding-left: 12px;
  margin-bottom: 30px;
}
</style>
