<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备编号" prop="deviceCode">
        <el-input
          v-model="queryParams.deviceCode"
          placeholder="请输入设备编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报警类型" prop="alarmType">
        <el-select v-model="queryParams.alarmType" placeholder="请选择报警类型" clearable size="small">
          <el-option
            v-for="dict in typeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="deviceAlarmList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" type="index" />
      <el-table-column label="设备编号" align="center" prop="deviceCode" />
      <el-table-column label="报警类型" align="center" prop="alarmType" />
      <el-table-column label="报警值" align="center" prop="alarmValue" />
      <el-table-column label="报警开始时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="报警结束时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="处理状态" align="center" prop="dealStatus" >
        <template slot-scope="scope">
          <span v-if="scope.row.dealStatus == null">未处理</span>
          <span v-if="scope.row.dealStatus == 3">已解决</span>
          <span v-if="scope.row.dealStatus != 3 && scope.row.dealStatus != null">已解决</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleAdd(scope.row)"
            v-hasPermi="['workOrder:basicsInfo:add']"
            v-if="scope.row.orderId == '' || scope.row.orderId == null"
          >下发工单</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="showDetail(scope.row)"
            v-if="scope.row.orderId != '' && scope.row.orderId != null"
          >详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加工单信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-form-item label="工单名称" prop="orderName">
          <el-input v-model="form.orderName" placeholder="请输入工单名称" />
        </el-form-item>
        <el-form-item label="指定执行人员" prop="appointInspector">
          <el-input v-model="form.appointInspector" placeholder="请输入指定执行人员" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDeviceAlarm } from "@/api/dataMonitoring/deviceAlarm";
import { addBasicsInfo } from "@/api/workOrder/basicsInfo";
import { inspectorList } from "@/api/system/user";

export default {
  name: "DeviceAlarm",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 报警信息表格数据
      deviceAlarmList: [],
      // 巡检员列表数据
      inspectorList: [],
      // 报警类型字典
      typeOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deviceId: null,
        orderId: null,
        alarmType: null,
        alarmValue: null,
        startTime: null,
        endTime: null,
        dealStatus: null,
        remarks: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("t_alarm_type").then(response => {
      this.typeOptions = response.data;
    });
  },
  methods: {
    /** 查询报警信息列表 */
    getList() {
      this.loading = true;
      listDeviceAlarm(this.queryParams).then(response => {
        this.deviceAlarmList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 获取用户信息列表 */
    getUserList() {
      this.loading = true;
      inspectorList().then(response => {
        this.inspectorList = response.rows;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        orderId: null,
        orderType: null,
        orderName: null,
        orderStatus: "0",
        createTime: null,
        appointInspector: null,
        allotTime: null,
        actualInspector: null,
        actualTime: null,
        remarks: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.alarmId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.form.resourceId = row.alarmId;
      this.form.orderType = "3";
      this.open = true;
      this.title = "填写工单信息";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          addBasicsInfo(this.form).then(response => {
            this.msgSuccess("下发工单成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    /** 工单详细信息跳转 */
    showDetail(row) {
      this.$router.push({
        path: '/deviceAlarm/alarmdetail',
        query:{
          orderId : row.orderId
        }
      }) //带参跳转
    },
  }
};
</script>
