<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="设备名称" prop="deviceId">
        <el-select v-model="queryParams.deviceId" placeholder="请选择设备名称" filterable clearable>
          <el-option
            v-for="item in deviceLedgerList"
            :key="item.deviceId"
            :label="item.deviceName"
            :value="item.deviceId"
          ></el-option>
        </el-select>
      </el-form-item>
<!--      <el-form-item label="报警类型" prop="alarmType">-->
<!--        <el-select v-model="queryParams.alarmType" placeholder="请选择报警类型" clearable>-->
<!--          <el-option-->
<!--            v-for="dict in dict.type.dms_alarm_type"-->
<!--            :key="dict.value"-->
<!--            :label="dict.label"-->
<!--            :value="dict.value"-->
<!--          />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="报警级别" prop="alarmLevel">-->
<!--        <el-select v-model="queryParams.alarmLevel" placeholder="请选择报警级别" clearable>-->
<!--          <el-option-->
<!--            v-for="dict in dict.type.dms_alarm_level"-->
<!--            :key="dict.value"-->
<!--            :label="dict.label"-->
<!--            :value="dict.value"-->
<!--          />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
      <el-form-item label="报警状态" prop="alarmStatus">
        <el-select v-model="queryParams.alarmStatus" placeholder="请选择报警状态" clearable>
          <el-option
            v-for="dict in dict.type.dms_alarm_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="通知状态" prop="noticeStatus">-->
<!--        <el-select v-model="queryParams.noticeStatus" placeholder="请选择通知状态" clearable>-->
<!--          <el-option-->
<!--            v-for="dict in dict.type.dms_notice_status"-->
<!--            :key="dict.value"-->
<!--            :label="dict.label"-->
<!--            :value="dict.value"-->
<!--          />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
      <el-form-item label="报警开始时间">
        <el-date-picker
          v-model="daterangeAlarmBeginTime"
          style="width: 340px"
          value-format=" yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="-"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
        ></el-date-picker>
      </el-form-item>
      <!--      <el-form-item label="报警结束时间">-->
      <!--        <el-date-picker-->
      <!--          v-model="daterangeAlarmEndTime"-->
      <!--          style="width: 240px"-->
      <!--          value-format=" yyyy-MM-dd HH:mm:ss"-->
      <!--          type="datetimerange"-->
      <!--          range-separator="-"-->
      <!--          start-placeholder="开始时间"-->
      <!--          end-placeholder="结束时间"-->
      <!--        ></el-date-picker>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="处理人员" prop="handleUser">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.handleUser"-->
      <!--          placeholder="请输入处理人员"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="处理时间">-->
      <!--        <el-date-picker-->
      <!--          v-model="daterangeHandleTime"-->
      <!--          style="width: 240px"-->
      <!--          value-format=" yyyy-MM-dd HH:mm:ss"-->
      <!--          type="datetimerange"-->
      <!--          range-separator="-"-->
      <!--          start-placeholder="开始时间"-->
      <!--          end-placeholder="结束时间"-->
      <!--        ></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-row :gutter="10" class="mb8">
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="primary"-->
      <!--          plain-->
      <!--          icon="el-icon-plus"-->
      <!--          size="mini"-->
      <!--          @click="handleAdd"-->
      <!--          v-hasPermi="['dms:dmsRecordAlarmInfo:add']"-->
      <!--        >新增-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="success"-->
      <!--          plain-->
      <!--          icon="el-icon-edit"-->
      <!--          size="mini"-->
      <!--          :disabled="single"-->
      <!--          @click="handleUpdate"-->
      <!--          v-hasPermi="['dms:dmsRecordAlarmInfo:edit']"-->
      <!--        >修改-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['dms:dmsRecordAlarmInfo:remove']"
        >删除
        </el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['dms:dmsRecordAlarmInfo:export']"-->
<!--        >导出-->
<!--        </el-button>-->
<!--      </el-col>-->
    </el-row>

    <el-table v-loading="loading" :data="dmsRecordAlarmInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="主键标识" align="center" prop="alarmId" v-if="columns[0].visible"/>
      <el-table-column label="设备名称" align="center" prop="deviceName" v-if="columns[1].visible"/>
      <el-table-column label="设备报警规则id" align="center" prop="alarmRuleId" v-if="columns[2].visible"/>
<!--      <el-table-column label="报警类型" align="center" prop="alarmType" v-if="columns[19].visible">-->
<!--        <template slot-scope="scope">-->
<!--          <dict-tag :options="dict.type.dms_alarm_type" :value="scope.row.alarmType"/>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="报警级别" align="center" prop="alarmLevel" v-if="columns[20].visible">-->
<!--        <template slot-scope="scope">-->
<!--          <dict-tag :options="dict.type.dms_alarm_level" :value="scope.row.alarmLevel"/>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="通知方式" align="center" prop="noticeType" v-if="columns[21].visible" width="100">-->
<!--        <template slot-scope="scope">-->
<!--          <dict-tag :options="dict.type.dms_notice_type"-->
<!--                    :value="scope.row.noticeType ? scope.row.noticeType.split(',') : []"/>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="自动恢复标识" align="center" prop="recoverType" v-if="columns[22].visible">-->
<!--        <template slot-scope="scope">-->
<!--          <dict-tag :options="dict.type.dms_recover_type" :value="scope.row.recoverType"/>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="报警状态字" align="center" prop="alarmStatusWord" v-if="columns[23].visible" width="90"/>-->
      <el-table-column label="报警状态" align="center" prop="alarmStatus" v-if="columns[8].visible" width="90">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.dms_alarm_status" :value="scope.row.alarmStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="报警原因" align="center" prop="alarmReason" v-if="columns[6].visible"
                       :show-overflow-tooltip="true"/>
<!--      <el-table-column label="报警数据" align="center" prop="alarmData" v-if="columns[7].visible"-->
<!--                       :show-overflow-tooltip="true"/>-->
<!--      <el-table-column label="处理建议" align="center" prop="handleSuggest" v-if="columns[9].visible"-->
<!--                       :show-overflow-tooltip="true"/>-->
<!--      <el-table-column label="通知状态" align="center" prop="noticeStatus" v-if="columns[10].visible">-->
<!--        <template slot-scope="scope">-->
<!--          <dict-tag :options="dict.type.dms_notice_status" :value="scope.row.noticeStatus"/>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="报警开始时间" align="center" prop="alarmBeginTime" width="180" v-if="columns[3].visible">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.alarmBeginTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="报警结束时间" align="center" prop="alarmEndTime" width="180" v-if="columns[4].visible">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.alarmEndTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="报警持续时间(ms)" align="center" prop="continueTime" v-if="columns[5].visible" width="130"/>
<!--      <el-table-column label="处理人员" align="center" prop="handleUser" v-if="columns[11].visible"/>-->
<!--      <el-table-column label="处理时间" align="center" prop="handleTime" width="180" v-if="columns[12].visible">-->
<!--        <template slot-scope="scope">-->
<!--          <span>{{ parseTime(scope.row.handleTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="原因分析" align="center" prop="causeAnalysis" v-if="columns[13].visible"/>
      <el-table-column label="备注" align="center" prop="remark" v-if="columns[14].visible"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-success"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['dms:dmsRecordAlarmInfo:edit']"
            v-if="scope.row.alarmStatus === '0'"
          >处理
          </el-button>
          <!--          <el-button-->
          <!--            size="mini"-->
          <!--            type="text"-->
          <!--            icon="el-icon-delete"-->
          <!--            @click="handleDelete(scope.row)"-->
          <!--            v-hasPermi="['dms:dmsRecordAlarmInfo:remove']"-->
          <!--          >删除-->
          <!--          </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="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-form-item label="设备名称" prop="deviceName">
          <el-input v-model="form.deviceName" placeholder="请输入设备名称" disabled/>
        </el-form-item>
        <!--        <el-form-item label="设备报警规则id" prop="alarmRuleId">-->
        <!--          <el-input v-model="form.alarmRuleId" placeholder="请输入设备报警规则id"/>-->
        <!--        </el-form-item>-->
        <el-form-item label="报警开始时间" prop="alarmBeginTime">
          <el-date-picker clearable
                          v-model="form.alarmBeginTime"
                          type="datetime"
                          value-format=" yyyy-MM-dd HH:mm:ss"
                          disabled>
          </el-date-picker>
        </el-form-item>
        <el-form-item label="报警结束时间" prop="alarmEndTime">
          <el-date-picker clearable
                          v-model="form.alarmEndTime"
                          type="datetime"
                          value-format=" yyyy-MM-dd HH:mm:ss"
                          disabled>
          </el-date-picker>
        </el-form-item>
        <!--        <el-form-item label="报警持续时间(ms)" prop="continueTime">-->
        <!--          <el-input v-model="form.continueTime" placeholder="请输入报警持续时间(ms)"/>-->
        <!--        </el-form-item>-->
        <el-form-item label="报警原因" prop="alarmReason">
          <el-input v-model="form.alarmReason" type="textarea" placeholder="请输入报警原因"/>
        </el-form-item>
<!--        <el-form-item label="报警数据" prop="alarmData">-->
<!--          <el-input v-model="form.alarmData" type="textarea" disabled/>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="报警状态" prop="alarmStatus">-->
<!--          <el-radio-group v-model="form.alarmStatus" disabled>-->
<!--            <el-radio-->
<!--              v-for="dict in dict.type.dms_alarm_status"-->
<!--              :key="dict.value"-->
<!--              :label="dict.value"-->
<!--            >{{ dict.label }}-->
<!--            </el-radio>-->
<!--          </el-radio-group>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="处理建议" prop="handleSuggest">-->
<!--          <el-input v-model="form.handleSuggest" type="textarea" disabled/>-->
<!--        </el-form-item>-->
        <!--        <el-form-item label="通知状态" prop="noticeStatus">-->
        <!--          <el-radio-group v-model="form.noticeStatus" disabled>-->
        <!--            <el-radio-->
        <!--              v-for="dict in dict.type.dms_notice_status"-->
        <!--              :key="dict.value"-->
        <!--              :label="dict.value"-->
        <!--            >{{ dict.label }}-->
        <!--            </el-radio>-->
        <!--          </el-radio-group>-->
        <!--        </el-form-item>-->
        <el-form-item label="原因分析" prop="causeAnalysis">
          <el-input v-model="form.causeAnalysis" type="textarea" placeholder="请输入原因分析"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" 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 {
  listDmsRecordAlarmInfo,
  getDmsRecordAlarmInfo,
  delDmsRecordAlarmInfo,
  addDmsRecordAlarmInfo,
  updateDmsRecordAlarmInfo,
  handleDmsRecordAlarmInfo
} from "@/api/dms/dmsRecordAlarmInfo";

import { getDeviceLedgerList } from '@/api/base/deviceLedger'

export default {
  name: "DmsRecordAlarmInfo",
  dicts: ['dms_notice_status', 'dms_alarm_status', 'dms_recover_type', 'dms_notice_type', 'dms_alarm_type', 'dms_alarm_level'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备报警记录表格数据
      dmsRecordAlarmInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 备注时间范围
      daterangeAlarmBeginTime: [],
      // 备注时间范围
      daterangeAlarmEndTime: [],
      // 备注时间范围
      daterangeHandleTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deviceId: null,
        alarmRuleId: null,
        alarmBeginTime: null,
        alarmEndTime: null,
        continueTime: null,
        alarmReason: null,
        alarmData: null,
        alarmStatus: null,
        handleSuggest: null,
        noticeStatus: null,
        handleUser: null,
        handleTime: null,
        causeAnalysis: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        deviceId: [
          {required: true, message: "设备名称不能为空", trigger: "blur"}
        ],
        alarmRuleId: [
          {required: true, message: "设备报警规则id不能为空", trigger: "blur"}
        ],
        alarmStatus: [
          {required: true, message: "报警状态不能为空", trigger: "change"}
        ],
        noticeStatus: [
          {required: true, message: "通知状态不能为空", trigger: "change"}
        ],
      },
      columns: [
        {key: 0, label: `主键标识`, visible: false},
        {key: 1, label: `设备名称`, visible: true},
        {key: 2, label: `设备报警规则id`, visible: false},
        {key: 3, label: `报警开始时间`, visible: true},
        {key: 4, label: `报警结束时间`, visible: true},
        {key: 5, label: `报警持续时间(ms)`, visible: true},
        {key: 6, label: `报警原因`, visible: true},
        {key: 7, label: `报警数据`, visible: false},
        {key: 8, label: `报警状态`, visible: true},
        {key: 9, label: `处理建议`, visible: false},
        {key: 10, label: `通知状态`, visible: false},
        {key: 11, label: `处理人员`, visible: false},
        {key: 12, label: `处理时间`, visible: false},
        {key: 13, label: `原因分析`, visible: true},
        {key: 14, label: `备注`, visible: true},
        {key: 15, label: `创建人`, visible: false},
        {key: 16, label: `创建时间`, visible: false},
        {key: 17, label: `更新人`, visible: false},
        {key: 18, label: `更新时间`, visible: false},
        {key: 19, label: `报警类型`, visible: false},
        {key: 20, label: `报警级别`, visible: false},
        {key: 21, label: `通知方式`, visible: false},
        {key: 22, label: `自动恢复标识`, visible: false},
        {key: 23, label: `报警状态字`, visible: false},
      ],
      //设备名称选项
      deviceLedgerList: []
    };
  },
  created() {
    console.log('url参数：',this.$route.query)
    getDeviceLedgerList({}).then(response => {
      this.deviceLedgerList = response.data
    })
    const alarmId = this.$route.query.alarmId
    if (alarmId == null){
      this.getList();
    } else {
      listDmsRecordAlarmInfo({alarmId: alarmId}).then(response => {
        this.dmsRecordAlarmInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    }
  },
  methods: {
    /** 查询设备报警记录列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeAlarmBeginTime && '' != this.daterangeAlarmBeginTime) {
        this.queryParams.params["beginAlarmBeginTime"] = this.daterangeAlarmBeginTime[0];
        this.queryParams.params["endAlarmBeginTime"] = this.daterangeAlarmBeginTime[1];
      }
      if (null != this.daterangeAlarmEndTime && '' != this.daterangeAlarmEndTime) {
        this.queryParams.params["beginAlarmEndTime"] = this.daterangeAlarmEndTime[0];
        this.queryParams.params["endAlarmEndTime"] = this.daterangeAlarmEndTime[1];
      }
      if (null != this.daterangeHandleTime && '' != this.daterangeHandleTime) {
        this.queryParams.params["beginHandleTime"] = this.daterangeHandleTime[0];
        this.queryParams.params["endHandleTime"] = this.daterangeHandleTime[1];
      }
      listDmsRecordAlarmInfo(this.queryParams).then(response => {
        this.dmsRecordAlarmInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        alarmId: null,
        deviceId: null,
        alarmRuleId: null,
        alarmBeginTime: null,
        alarmEndTime: null,
        continueTime: null,
        alarmReason: null,
        alarmData: null,
        alarmStatus: null,
        handleSuggest: null,
        noticeStatus: null,
        handleUser: null,
        handleTime: null,
        causeAnalysis: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeAlarmBeginTime = [];
      this.daterangeAlarmEndTime = [];
      this.daterangeHandleTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.alarmId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加设备报警记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const alarmId = row.alarmId || this.ids
      getDmsRecordAlarmInfo(alarmId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "设备报警处理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.alarmId != null) {
            handleDmsRecordAlarmInfo(this.form).then(response => {
              this.$modal.msgSuccess("处理成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDmsRecordAlarmInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const alarmIds = row.alarmId || this.ids;
      this.$modal.confirm('是否确认删除设备报警记录编号为"' + alarmIds + '"的数据项？').then(function () {
        return delDmsRecordAlarmInfo(alarmIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('dms/dmsRecordAlarmInfo/export', {
        ...this.queryParams
      }, `dmsRecordAlarmInfo_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
