<template>
  <div class="app-container">
    <div v-if="fromGisMap">
      <div
        style="width: 5%; height: 45px; margin-left: 20px"
        @click="$router.go(-1)"
      >
        <el-button
          size="medium"
          type="text"
          style="font-size: 18px; color: rgb(7, 63, 112); float: left"
          >返回</el-button
        >
        <div style="float: left; margin-top: 8px; margin-left: 5px">
          <img
            src="../../../assets/logo/fanhui.png"
            style="width: 25px"
            alt=""
          />
        </div>
      </div>
    </div>

    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <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 label="报警开始时间" prop="startTime1">
        <el-date-picker
          clearable
          size="small"
          v-model="queryParams.startTime1"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetime"
          placeholder="请选择起始时间"
          align="right"
        >
        </el-date-picker>
        <font color="#C0C4CC"> 至</font>
      </el-form-item>
      <el-form-item label="" prop="endTime1">
        <el-date-picker
          clearable
          size="small"
          v-model="queryParams.endTime1"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetime"
          placeholder="请选择截止时间"
          align="right"
        >
        </el-date-picker>
      </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" prop="deviceName" />
      <el-table-column label="设备编号" align="center" prop="deviceCode" />
      <el-table-column label="设备类型" align="center" prop="deviceType" />
      <el-table-column label="报警类型" align="center" prop="alarmType" />
      <el-table-column label="报警值" align="center" prop="alarmValue" />
      <el-table-column label="报警开始时间" align="center" prop="startTime" />
      <el-table-column label="报警结束时间" align="center" prop="endTime" />
      <el-table-column label="处理状态" align="center" prop="dealStatus">
        <template slot-scope="scope">
          <span v-if="scope.row.orderId == null || scope.row.orderId == ''">未生成工单</span>
          <span v-if="(scope.row.dealStatus == null || scope.row.dealStatus == '') &&
              scope.row.orderId != null && scope.row.orderId != ''">未处理</span>
          <span v-if="scope.row.dealStatus == 1">不需处理</span>
          <span v-if="scope.row.dealStatus == 2">已处理完成</span>
          <span v-if="scope.row.dealStatus == 3">未处理完成</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="normal"
            type="text"
            icon="el-icon-edit"
            @click="handleIssue(scope.row)"
            v-hasPermi="['workOrder:basicsInfo:add']"
            v-if="scope.row.orderId == '' || scope.row.orderId == null">
            生成工单
          </el-button>
          <el-button
            size="normal"
            type="text"
            icon="el-icon-edit"
            @click="showDetail(scope.row)">
            详情
          </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="deviceName">
          <font>{{ form.deviceName }}</font>
        </el-form-item>
        <el-form-item label="报警类型" prop="alarmType">
          <font>{{ form.alarmType }}</font>
        </el-form-item>
        <el-form-item label="报警值" prop="alarmValue">
          <font>{{ form.alarmValue }}</font>
        </el-form-item>
        <el-form-item label="工单名称" prop="orderName">
          <el-input v-model="form.orderName" placeholder="请输入工单名称" />
        </el-form-item>
        <el-form-item label="指定执行人员" prop="appointInspector">
          <el-select v-model="form.appointInspector" placeholder="请选择执行人员" clearable size="small" @change="setUserId">
            <el-option
              v-for="item in inspector"
              :key="item.userId"
              :label="item.nickName"
              :value="item.userId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="工单描述" prop="remarks">
          <el-input type="textarea" v-model="form.remarks" 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, getDeviceAlarm } from "@/api/dataMonitoring/deviceAlarm";
  import { addBasicsInfo } from "@/api/workOrder/basicsInfo";
  import { inspectorList } from "@/api/system/user";

export default {
  name: "DeviceAlarm",
  components: {},
  data() {
    return {
      // 判断是从哪里来的，如果是地图里来的，则可以返回
      fromGisMap:false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 报警信息表格数据
      deviceAlarmList: [],
      // 报警类型字典
      typeOptions: [],
      // 设备级联
      options: [],

      props: {
        multiple: true,
        value: "id",
        label: "name",
        level: "level",
        children: "childList",
      },
      devices: null,
      // 巡检员列表
      inspector: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deviceCode: null,
        orderId: null,
        alarmType: null,
        startTime1: null,
        endTime1: null,
        dealStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        orderName: [
          { required: true, message: "工单名称不能为空", trigger: "blur" },
        ],
        appointInspector: [
          { required: true, message: "请选择巡检人员", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getDicts("t_alarm_type").then((response) => {
      this.typeOptions = response.data;
    });
  },
  beforeRouteEnter: (to, from, next) => {
    next((vm) => {
      // 如果是从地图里来的
      if(from.path=="/enterprise/mapView"){
        // console.log(123)
        vm.fromGisMap=true;
      }else{
        vm.fromGisMap=false;
      }
    });
  },
  methods: {
    /** 查询报警信息列表 */
    getList() {
      this.loading = true;
      listDeviceAlarm(this.queryParams).then((response) => {
        this.deviceAlarmList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getInspectorList() {
      this.loading = true;
      inspectorList().then((response) => {
        this.inspector = response.data;
        this.loading = false;
      });
    },
    setUserId(val) {
      this.form.appointInspector = val;
    },
    // 取消按钮
    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
    },
    /** 下发按钮操作 */
    handleIssue(row) {
      this.reset();
      this.getInspectorList();
      const alarmId = row.alarmId || this.ids;
      getDeviceAlarm(alarmId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "填写工单信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.resourceId = this.form.alarmId;
          this.form.orderType = "3";
          addBasicsInfo(this.form).then((response) => {
            this.msgSuccess("生成工单成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    /** 详细信息跳转 */
    showDetail(row) {
      this.$router.push({
        path: '/deviceAlarm/alarmdetail',
        query: {
          alarmId: row.alarmId
        }
      }) //带参跳转
    },
  },
};
</script>
