<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="规则名称" prop="alarmRuleName">
        <el-input
          v-model="queryParams.alarmRuleName"
          placeholder="请输入规则名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="租户ID，关联hw_tenant的tenant_id" prop="tenantId">
        <el-input
          v-model="queryParams.tenantId"
          placeholder="请输入租户ID，关联hw_tenant的tenant_id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属场景，关联hw_scene表的scene_id字段" prop="sceneId">
        <el-input
          v-model="queryParams.sceneId"
          placeholder="请输入所属场景，关联hw_scene表的scene_id字段"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报警级别，关联表hw_alarm_level字段alarm_level_id" prop="alarmLevelId">
        <el-input
          v-model="queryParams.alarmLevelId"
          placeholder="请输入报警级别，关联表hw_alarm_level字段alarm_level_id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报警类型，关联表hw_alarm_type字段alarm_type_id" prop="alarmTypeId">
        <el-input
          v-model="queryParams.alarmTypeId"
          placeholder="请输入报警类型，关联表hw_alarm_type字段alarm_type_id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备，关联表hw_device字段device_id" prop="deviceId">
        <el-input
          v-model="queryParams.deviceId"
          placeholder="请输入设备，关联表hw_device字段device_id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备模型，关联表hw_device_mode字段device_mode_id" prop="deviceModeId">
        <el-input
          v-model="queryParams.deviceModeId"
          placeholder="请输入设备模型，关联表hw_device_mode字段device_mode_id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备属性，关联表hw_device_mode_function字段mode_function_id" prop="modeFunctionId">
        <el-input
          v-model="queryParams.modeFunctionId"
          placeholder="请输入设备属性，关联表hw_device_mode_function字段mode_function_id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="触发条件" prop="triggerCondition">
        <el-input
          v-model="queryParams.triggerCondition"
          placeholder="请输入触发条件"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="触发定义A值" prop="triggerDataA">
        <el-input
          v-model="queryParams.triggerDataA"
          placeholder="请输入触发定义A值"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="触发定义B值" prop="triggerDataB">
        <el-input
          v-model="queryParams.triggerDataB"
          placeholder="请输入触发定义B值"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="触发死区" prop="triggerDeadzone">
        <el-input
          v-model="queryParams.triggerDeadzone"
          placeholder="请输入触发死区"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="触发阈值次数" prop="triggerNumber">
        <el-input
          v-model="queryParams.triggerNumber"
          placeholder="请输入触发阈值次数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="时间范围" prop="triggerTimeFrame">
        <el-input
          v-model="queryParams.triggerTimeFrame"
          placeholder="请输入时间范围"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联动标识" prop="linkFlag">
        <el-input
          v-model="queryParams.linkFlag"
          placeholder="请输入联动标识"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联动设备，关联表hw_device字段device_id" prop="linkDeviceId">
        <el-input
          v-model="queryParams.linkDeviceId"
          placeholder="请输入联动设备，关联表hw_device字段device_id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联动设备功能，关联表hw_device_mode_function字段mode_function_id" prop="linkDeviceFunctionId">
        <el-input
          v-model="queryParams.linkDeviceFunctionId"
          placeholder="请输入联动设备功能，关联表hw_device_mode_function字段mode_function_id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联动设备属性值" prop="linkDeviceFunctionData">
        <el-input
          v-model="queryParams.linkDeviceFunctionData"
          placeholder="请输入联动设备属性值"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报警推送标识(1、是，2、否）" prop="alarmPushFlag">
        <el-input
          v-model="queryParams.alarmPushFlag"
          placeholder="请输入报警推送标识(1、是，2、否）"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预留字段" prop="alarmRuleField">
        <el-input
          v-model="queryParams.alarmRuleField"
          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="['business:alarmRule: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="['business:alarmRule: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="['business:alarmRule: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="['business:alarmRule:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="alarmRuleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="报警规则ID" align="center" prop="alarmRuleId" />
      <el-table-column label="规则名称" align="center" prop="alarmRuleName" />
      <el-table-column label="租户ID，关联hw_tenant的tenant_id" align="center" prop="tenantId" />
      <el-table-column label="所属场景，关联hw_scene表的scene_id字段" align="center" prop="sceneId" />
      <el-table-column label="报警级别，关联表hw_alarm_level字段alarm_level_id" align="center" prop="alarmLevelId" />
      <el-table-column label="报警类型，关联表hw_alarm_type字段alarm_type_id" align="center" prop="alarmTypeId" />
      <el-table-column label="规则类型" align="center" prop="ruleType" />
      <el-table-column label="设备，关联表hw_device字段device_id" align="center" prop="deviceId" />
      <el-table-column label="设备模型，关联表hw_device_mode字段device_mode_id" align="center" prop="deviceModeId" />
      <el-table-column label="设备属性，关联表hw_device_mode_function字段mode_function_id" align="center" prop="modeFunctionId" />
      <el-table-column label="触发条件" align="center" prop="triggerCondition" />
      <el-table-column label="触发定义A值" align="center" prop="triggerDataA" />
      <el-table-column label="触发定义B值" align="center" prop="triggerDataB" />
      <el-table-column label="触发死区" align="center" prop="triggerDeadzone" />
      <el-table-column label="触发阈值次数" align="center" prop="triggerNumber" />
      <el-table-column label="时间范围" align="center" prop="triggerTimeFrame" />
      <el-table-column label="联动标识" align="center" prop="linkFlag" />
      <el-table-column label="联动类型" align="center" prop="linkType" />
      <el-table-column label="联动设备，关联表hw_device字段device_id" align="center" prop="linkDeviceId" />
      <el-table-column label="联动设备功能，关联表hw_device_mode_function字段mode_function_id" align="center" prop="linkDeviceFunctionId" />
      <el-table-column label="联动设备属性值" align="center" prop="linkDeviceFunctionData" />
      <el-table-column label="报警启用状态(1、是，2、否）" align="center" prop="alarmRuleStatus" />
      <el-table-column label="报警推送标识(1、是，2、否）" align="center" prop="alarmPushFlag" />
      <el-table-column label="报警推送内容" align="center" prop="alarmPushContent" />
      <el-table-column label="恢复正常推送内容" align="center" prop="alarmRecoverContent" />
      <el-table-column label="描述" align="center" prop="remark" />
      <el-table-column label="预留字段" align="center" prop="alarmRuleField" />
      <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="['business:alarmRule:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:alarmRule: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="80px">
        <el-form-item label="规则名称" prop="alarmRuleName">
          <el-input v-model="form.alarmRuleName" placeholder="请输入规则名称" />
        </el-form-item>
        <el-form-item label="租户ID，关联hw_tenant的tenant_id" prop="tenantId">
          <el-input v-model="form.tenantId" placeholder="请输入租户ID，关联hw_tenant的tenant_id" />
        </el-form-item>
        <el-form-item label="所属场景，关联hw_scene表的scene_id字段" prop="sceneId">
          <el-input v-model="form.sceneId" placeholder="请输入所属场景，关联hw_scene表的scene_id字段" />
        </el-form-item>
        <el-form-item label="报警级别，关联表hw_alarm_level字段alarm_level_id" prop="alarmLevelId">
          <el-input v-model="form.alarmLevelId" placeholder="请输入报警级别，关联表hw_alarm_level字段alarm_level_id" />
        </el-form-item>
        <el-form-item label="报警类型，关联表hw_alarm_type字段alarm_type_id" prop="alarmTypeId">
          <el-input v-model="form.alarmTypeId" placeholder="请输入报警类型，关联表hw_alarm_type字段alarm_type_id" />
        </el-form-item>
        <el-form-item label="设备，关联表hw_device字段device_id" prop="deviceId">
          <el-input v-model="form.deviceId" placeholder="请输入设备，关联表hw_device字段device_id" />
        </el-form-item>
        <el-form-item label="设备模型，关联表hw_device_mode字段device_mode_id" prop="deviceModeId">
          <el-input v-model="form.deviceModeId" placeholder="请输入设备模型，关联表hw_device_mode字段device_mode_id" />
        </el-form-item>
        <el-form-item label="设备属性，关联表hw_device_mode_function字段mode_function_id" prop="modeFunctionId">
          <el-input v-model="form.modeFunctionId" placeholder="请输入设备属性，关联表hw_device_mode_function字段mode_function_id" />
        </el-form-item>
        <el-form-item label="触发条件" prop="triggerCondition">
          <el-input v-model="form.triggerCondition" placeholder="请输入触发条件" />
        </el-form-item>
        <el-form-item label="触发定义A值" prop="triggerDataA">
          <el-input v-model="form.triggerDataA" placeholder="请输入触发定义A值" />
        </el-form-item>
        <el-form-item label="触发定义B值" prop="triggerDataB">
          <el-input v-model="form.triggerDataB" placeholder="请输入触发定义B值" />
        </el-form-item>
        <el-form-item label="触发死区" prop="triggerDeadzone">
          <el-input v-model="form.triggerDeadzone" placeholder="请输入触发死区" />
        </el-form-item>
        <el-form-item label="触发阈值次数" prop="triggerNumber">
          <el-input v-model="form.triggerNumber" placeholder="请输入触发阈值次数" />
        </el-form-item>
        <el-form-item label="时间范围" prop="triggerTimeFrame">
          <el-input v-model="form.triggerTimeFrame" placeholder="请输入时间范围" />
        </el-form-item>
        <el-form-item label="联动标识" prop="linkFlag">
          <el-input v-model="form.linkFlag" placeholder="请输入联动标识" />
        </el-form-item>
        <el-form-item label="联动设备，关联表hw_device字段device_id" prop="linkDeviceId">
          <el-input v-model="form.linkDeviceId" placeholder="请输入联动设备，关联表hw_device字段device_id" />
        </el-form-item>
        <el-form-item label="联动设备功能，关联表hw_device_mode_function字段mode_function_id" prop="linkDeviceFunctionId">
          <el-input v-model="form.linkDeviceFunctionId" placeholder="请输入联动设备功能，关联表hw_device_mode_function字段mode_function_id" />
        </el-form-item>
        <el-form-item label="联动设备属性值" prop="linkDeviceFunctionData">
          <el-input v-model="form.linkDeviceFunctionData" placeholder="请输入联动设备属性值" />
        </el-form-item>
        <el-form-item label="报警推送标识(1、是，2、否）" prop="alarmPushFlag">
          <el-input v-model="form.alarmPushFlag" placeholder="请输入报警推送标识(1、是，2、否）" />
        </el-form-item>
        <el-form-item label="报警推送内容">
          <editor v-model="form.alarmPushContent" :min-height="192"/>
        </el-form-item>
        <el-form-item label="恢复正常推送内容">
          <editor v-model="form.alarmRecoverContent" :min-height="192"/>
        </el-form-item>
        <el-form-item label="描述" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="预留字段" prop="alarmRuleField">
          <el-input v-model="form.alarmRuleField" 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 { listAlarmRule, getAlarmRule, delAlarmRule, addAlarmRule, updateAlarmRule } from "@/api/business/alarmRule";

export default {
  name: "AlarmRule",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 报警规则表格数据
      alarmRuleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        alarmRuleName: null,
        tenantId: null,
        sceneId: null,
        alarmLevelId: null,
        alarmTypeId: null,
        ruleType: null,
        deviceId: null,
        deviceModeId: null,
        modeFunctionId: null,
        triggerCondition: null,
        triggerDataA: null,
        triggerDataB: null,
        triggerDeadzone: null,
        triggerNumber: null,
        triggerTimeFrame: null,
        linkFlag: null,
        linkType: null,
        linkDeviceId: null,
        linkDeviceFunctionId: null,
        linkDeviceFunctionData: null,
        alarmRuleStatus: null,
        alarmPushFlag: null,
        alarmPushContent: null,
        alarmRecoverContent: null,
        alarmRuleField: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        alarmRuleName: [
          { required: true, message: "规则名称不能为空", trigger: "blur" }
        ],
        tenantId: [
          { required: true, message: "租户ID，关联hw_tenant的tenant_id不能为空", trigger: "blur" }
        ],
        sceneId: [
          { required: true, message: "所属场景，关联hw_scene表的scene_id字段不能为空", trigger: "blur" }
        ],
        alarmLevelId: [
          { required: true, message: "报警级别，关联表hw_alarm_level字段alarm_level_id不能为空", trigger: "blur" }
        ],
        alarmTypeId: [
          { required: true, message: "报警类型，关联表hw_alarm_type字段alarm_type_id不能为空", trigger: "blur" }
        ],
        ruleType: [
          { required: true, message: "规则类型不能为空", trigger: "change" }
        ],
        modeFunctionId: [
          { required: true, message: "设备属性，关联表hw_device_mode_function字段mode_function_id不能为空", trigger: "blur" }
        ],
        triggerCondition: [
          { required: true, message: "触发条件不能为空", trigger: "blur" }
        ],
        triggerDataA: [
          { required: true, message: "触发定义A值不能为空", trigger: "blur" }
        ],
        linkFlag: [
          { required: true, message: "联动标识不能为空", trigger: "blur" }
        ],
        alarmRuleStatus: [
          { required: true, message: "报警启用状态(1、是，2、否）不能为空", trigger: "change" }
        ],
        alarmPushFlag: [
          { required: true, message: "报警推送标识(1、是，2、否）不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询报警规则列表 */
    getList() {
      this.loading = true;
      listAlarmRule(this.queryParams).then(response => {
        this.alarmRuleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        alarmRuleId: null,
        alarmRuleName: null,
        tenantId: null,
        sceneId: null,
        alarmLevelId: null,
        alarmTypeId: null,
        ruleType: null,
        deviceId: null,
        deviceModeId: null,
        modeFunctionId: null,
        triggerCondition: null,
        triggerDataA: null,
        triggerDataB: null,
        triggerDeadzone: null,
        triggerNumber: null,
        triggerTimeFrame: null,
        linkFlag: null,
        linkType: null,
        linkDeviceId: null,
        linkDeviceFunctionId: null,
        linkDeviceFunctionData: null,
        alarmRuleStatus: null,
        alarmPushFlag: null,
        alarmPushContent: null,
        alarmRecoverContent: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        alarmRuleField: 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
      getAlarmRule(alarmRuleId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改报警规则";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.alarmRuleId != null) {
            updateAlarmRule(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAlarmRule(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 delAlarmRule(alarmRuleIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/alarmRule/export', {
        ...this.queryParams
      }, `alarmRule_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
