<template>
  <div class="manage-container">
    <div class="manage-input">
      <el-form ref="queryForm" :model="queryForm" label-width="80px">
        <vab-query-form>
          <el-form-item width="100" prop="deviceName" label="站点名称">
            <el-input
              v-model="queryForm.deviceName"
              size="small"
              placeholder="请输入站点名称"
            ></el-input>
          </el-form-item>
          <el-form-item width="100" prop="handlerName" label="处理人">
            <el-input
              v-model="queryForm.handlerName"
              size="small"
              placeholder="请输入处理人"
            ></el-input>
          </el-form-item>
          <el-form-item width="100" prop="time" label="日期">
            <el-date-picker
              v-model="queryForm.date"
              size="small"
              type="daterange"
              align="right"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
              @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="80px">
        <vab-query-form>
          <el-form-item width="100" prop="alarmLevel" label="告警级别">
            <el-select
              v-model="queryForm.alarmLevel"
              size="small"
              placeholder="告警级别"
            >
              <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="告警类别">
            <el-select
              v-model="queryForm.alarmCategory"
              size="small"
              placeholder="告警类别"
            >
              <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="状态">
            <el-select
              v-model="queryForm.status"
              size="small"
              placeholder="状态"
            >
              <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()">重置</el-button>
      <el-button type="primary" size="small" @click="search">查询</el-button>
      <el-button type="primary" size="small" @click="save">添加</el-button>
      <el-button type="primary" size="small" @click="exportData">
        导出
      </el-button>
    </div>
    <el-table :data="tableData" stripe border style="width: 100%">
      <el-table-column
        fixed
        prop="deviceName"
        label="站点名称"
        width="150"
        align="center"
      ></el-table-column>
      <el-table-column
        fixed
        prop="sensorName"
        label="传感器名称"
        width="150"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="alarmTime"
        label="报警时间"
        width="200"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="alarmLevel"
        label="报警级别"
        width="200"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="alarmCategory"
        label="报警类别"
        width="200"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="alarmContent"
        label="告警内容"
        width="300"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="handlerName"
        label="处理人"
        width="120"
        align="center"
      ></el-table-column>
      <el-table-column prop="status" label="状态" width="120" align="center">
        <template #default="{ row }">
          <el-tag
            v-if="row.status == '已派工'"
            key="已派工"
            effect="dark"
            type="success"
            size="mini"
          >
            已派工
          </el-tag>
          <el-tag
            v-if="row.status == '未派工'"
            key="未派工"
            effect="dark"
            type="danger"
            size="mini"
          >
            未派工
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="remark"
        label="备注"
        width="120"
        align="center"
      ></el-table-column>
      <el-table-column fixed="right" label="操作" align="center">
        <template #default="{ row }">
          <el-button type="text" @click="handleDelete(row)">删除</el-button>
          <el-button
            v-if="row.status == '未派工'"
            type="text"
            @click="handleTask(row)"
          >
            转工单
          </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: '全部',
        },
        {
          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>
