<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="recoverType">
        <el-select v-model="queryParams.recoverType" placeholder="请选择自动恢复标识" clearable>
          <el-option
            v-for="dict in dict.type.dms_recover_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!--      <el-form-item label="报警持续时间(ms)" prop="continueTime">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.continueTime"-->
      <!--          placeholder="请输入报警持续时间(ms)"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item label="报警状态字" prop="alarmStatusWord">
        <el-input
          v-model="queryParams.alarmStatusWord"
          placeholder="请输入报警状态字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </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:dmsBaseAlarmRule: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:dmsBaseAlarmRule: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:dmsBaseAlarmRule: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:dmsBaseAlarmRule:export']"-->
<!--        >导出-->
<!--        </el-button>-->
<!--      </el-col>-->
    </el-row>

    <el-table v-loading="loading" :data="dmsBaseAlarmRuleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="主键标识" align="center" prop="alarmRuleId" v-if="columns[0].visible"/>
      <el-table-column label="设备名称" align="center" prop="deviceName" v-if="columns[1].visible"/>
      <el-table-column label="报警类型" align="center" prop="alarmType" v-if="columns[2].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[3].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[4].visible">
        <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[5].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="报警持续时间(ms)" align="center" prop="continueTime" v-if="columns[6].visible" width="130"/>
      <el-table-column label="报警状态字" align="center" prop="alarmStatusWord" v-if="columns[7].visible"/>
      <el-table-column label="报警原因" align="center" prop="alarmReason" v-if="columns[8].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="remark" v-if="columns[10].visible"/>
      <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="handleUpdate(scope.row)"
            v-hasPermi="['dms:dmsBaseAlarmRule:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['dms:dmsBaseAlarmRule: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="deviceId">
          <el-select v-model="form.deviceId" placeholder="请选择设备名称">
            <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-radio-group v-model="form.alarmType">
            <el-radio
              v-for="dict in dict.type.dms_alarm_type"
              :key="dict.value"
              :label="dict.value"
            >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="报警级别" prop="alarmLevel">
          <el-select v-model="form.alarmLevel" placeholder="请选择报警级别">
            <el-option
              v-for="dict in dict.type.dms_alarm_level"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="通知方式" prop="noticeType">
          <el-checkbox-group v-model="form.noticeType">
            <el-checkbox
              v-for="dict in dict.type.dms_notice_type"
              :key="dict.value"
              :label="dict.value">
              {{ dict.label }}
            </el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="自动恢复标识" prop="recoverType">
          <el-radio-group v-model="form.recoverType">
            <el-radio
              v-for="dict in dict.type.dms_recover_type"
              :key="dict.value"
              :label="dict.value"
            >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="报警持续时间(ms)" prop="continueTime">
          <el-input-number v-model="form.continueTime" placeholder="请输入报警持续时间(ms)"/>
        </el-form-item>
        <el-form-item label="报警状态字" prop="alarmStatusWord">
          <el-input v-model="form.alarmStatusWord" placeholder="请输入报警状态字"/>
        </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="handleSuggest">
          <el-input v-model="form.handleSuggest" 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 {
  listDmsBaseAlarmRule,
  getDmsBaseAlarmRule,
  delDmsBaseAlarmRule,
  addDmsBaseAlarmRule,
  updateDmsBaseAlarmRule
} from "@/api/dms/dmsBaseAlarmRule";
import {getDeviceLedgerList} from "@/api/dms/ledger";

export default {
  name: "DmsBaseAlarmRule",
  dicts: ['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,
      // 设备报警规则表格数据
      dmsBaseAlarmRuleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deviceId: null,
        alarmType: null,
        alarmLevel: null,
        noticeType: null,
        recoverType: null,
        continueTime: null,
        alarmStatusWord: null,
        alarmReason: null,
        handleSuggest: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        deviceId: [
          {required: true, message: "设备名称", trigger: "blur"}
        ],
        alarmType: [
          {required: true, message: "报警类型不能为空", trigger: "change"}
        ],
        alarmLevel: [
          {required: true, message: "报警级别不能为空", trigger: "change"}
        ],
      },
      columns: [
        {key: 0, label: `主键标识`, visible: false},
        {key: 1, label: `设备名称`, visible: true},
        {key: 2, label: `报警类型`, visible: true},
        {key: 3, label: `报警级别`, visible: true},
        {key: 4, label: `通知方式`, visible: true},
        {key: 5, label: `自动恢复标识`, visible: true},
        {key: 6, label: `报警持续时间(ms)`, visible: true},
        {key: 7, label: `报警状态字`, visible: true},
        {key: 8, label: `报警原因`, visible: true},
        {key: 9, label: `处理建议`, visible: true},
        {key: 10, label: `备注`, visible: true}
      ],
      //设备名称选项
      deviceLedgerList: []
    };
  },
  created() {
    getDeviceLedgerList({}).then(response => {
      this.deviceLedgerList = response.data
    })
    this.getList();
  },
  methods: {
    /** 查询设备报警规则列表 */
    getList() {
      this.loading = true;
      listDmsBaseAlarmRule(this.queryParams).then(response => {
        this.dmsBaseAlarmRuleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        alarmRuleId: null,
        deviceId: null,
        deviceCode: null,
        deviceName: null,
        alarmType: null,
        alarmLevel: null,
        noticeType: [],
        recoverType: null,
        continueTime: null,
        alarmStatusWord: null,
        alarmReason: null,
        handleSuggest: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: 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.alarmRuleId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加设备报警规则";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const alarmRuleId = row.alarmRuleId || this.ids
      getDmsBaseAlarmRule(alarmRuleId).then(response => {
        this.form = response.data;
        this.form.noticeType = this.form.noticeType.split(",");
        this.open = true;
        this.title = "修改设备报警规则";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.noticeType = this.form.noticeType.join(",");
          if (this.form.alarmRuleId != null) {
            updateDmsBaseAlarmRule(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDmsBaseAlarmRule(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const alarmRuleIds = row.alarmRuleId || this.ids;
      this.$modal.confirm('是否确认删除设备报警规则编号为"' + alarmRuleIds + '"的数据项？').then(function () {
        return delDmsBaseAlarmRule(alarmRuleIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('dms/dmsBaseAlarmRule/export', {
        ...this.queryParams
      }, `dmsBaseAlarmRule_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
