<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="区域ID" prop="areaId">
        <el-input
          v-model="queryParams.areaId"
          placeholder="请输入区域ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="区域名称" prop="areaName">
        <el-input
          v-model="queryParams.areaName"
          placeholder="请输入区域名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="区域启用 Y/N" prop="areaEnable">
        <el-input
          v-model="queryParams.areaEnable"
          placeholder="请输入区域启用 Y/N"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="有效期开始日期" prop="validBeginDate">
        <el-date-picker
          clearable
          v-model="queryParams.validBeginDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择有效期开始日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="有效期结束日期" prop="validEndDate">
        <el-date-picker
          clearable
          v-model="queryParams.validEndDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择有效期结束日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="每日开始时间" prop="validBeginTime">
        <el-date-picker
          clearable
          v-model="queryParams.validBeginTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择每日开始时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="每日结束时间" prop="validEndTime">
        <el-date-picker
          clearable
          v-model="queryParams.validEndTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择每日结束时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="报警通知配置ID" prop="alarmNoticeId">
        <el-input
          v-model="queryParams.alarmNoticeId"
          placeholder="请输入报警通知配置ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="通知配置名称" prop="alarmNoticeName">
        <el-input
          v-model="queryParams.alarmNoticeName"
          placeholder="请输入通知配置名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="部门名称" prop="deptName">
        <el-input
          v-model="queryParams.deptName"
          placeholder="请输入部门名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="围栏名称" prop="railName">
        <el-input
          v-model="queryParams.railName"
          placeholder="请输入围栏名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="静态围栏名称" prop="staticRailName">
        <el-input
          v-model="queryParams.staticRailName"
          placeholder="请输入静态围栏名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="人员姓名" prop="realName">
        <el-input
          v-model="queryParams.realName"
          placeholder="请输入人员姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="一级报警人员规则" prop="firstAlarmPeopleRule">
        <el-input
          v-model="queryParams.firstAlarmPeopleRule"
          placeholder="请输入一级报警人员规则"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="二级报警规则" prop="secondAlarmRule">
        <el-input
          v-model="queryParams.secondAlarmRule"
          placeholder="请输入二级报警规则"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="二级报警人员规则" prop="secondAlarmPeopleRule">
        <el-input
          v-model="queryParams.secondAlarmPeopleRule"
          placeholder="请输入二级报警人员规则"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="二级报警通知ID" prop="secondAlarmNoticeId">
        <el-input
          v-model="queryParams.secondAlarmNoticeId"
          placeholder="请输入二级报警通知ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="三级报警规则" prop="thirdAlarmRule">
        <el-input
          v-model="queryParams.thirdAlarmRule"
          placeholder="请输入三级报警规则"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="三级报警人员规则" prop="thirdAlarmPeopleRule">
        <el-input
          v-model="queryParams.thirdAlarmPeopleRule"
          placeholder="请输入三级报警人员规则"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="三级报警通知ID" prop="thirdAlarmNoticeId">
        <el-input
          v-model="queryParams.thirdAlarmNoticeId"
          placeholder="请输入三级报警通知ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="聚集半径" prop="aggregationRadius">
        <el-input
          v-model="queryParams.aggregationRadius"
          placeholder="请输入聚集半径"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="持续时间" prop="duration">
        <el-input
          v-model="queryParams.duration"
          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"
          >新增</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="['system:setting: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="['system:setting: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="['system:setting:export']"
          >导出</el-button
        >
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="settingList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="区域ID" align="center" prop="areaId" />
      <el-table-column label="区域名称" align="center" prop="areaName" />
      <el-table-column label="区域启用 Y/N" align="center" prop="areaEnable" />
      <el-table-column label="报警类型" align="center" prop="alarmType" />
      <el-table-column label="规则JSON" align="center" prop="rule" />
      <el-table-column
        label="有效期开始日期"
        align="center"
        prop="validBeginDate"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.validBeginDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="有效期结束日期"
        align="center"
        prop="validEndDate"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.validEndDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="每日开始时间"
        align="center"
        prop="validBeginTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.validBeginTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="每日结束时间"
        align="center"
        prop="validEndTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.validEndTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="报警通知配置ID"
        align="center"
        prop="alarmNoticeId"
      />
      <el-table-column
        label="通知配置名称"
        align="center"
        prop="alarmNoticeName"
      />
      <el-table-column label="围栏ID列表" align="center" prop="railIds" />
      <el-table-column label="部门ID列表" align="center" prop="deptIds" />
      <el-table-column label="部门名称" align="center" prop="deptName" />
      <el-table-column
        label="报警人员ID列表"
        align="center"
        prop="alarmPersonIds"
      />
      <el-table-column
        label="报警人员姓名"
        align="center"
        prop="alarmPersonNames"
      />
      <el-table-column
        label="报警岗位ID列表"
        align="center"
        prop="alarmPostIds"
      />
      <el-table-column
        label="报警岗位名称"
        align="center"
        prop="alarmPostNames"
      />
      <el-table-column label="围栏名称" align="center" prop="railName" />
      <el-table-column
        label="静态围栏名称"
        align="center"
        prop="staticRailName"
      />
      <el-table-column label="人员姓名" align="center" prop="realName" />
      <el-table-column label="人员ID列表" align="center" prop="personIds" />
      <el-table-column
        label="一级报警人员规则"
        align="center"
        prop="firstAlarmPeopleRule"
      />
      <el-table-column
        label="二级报警规则"
        align="center"
        prop="secondAlarmRule"
      />
      <el-table-column
        label="二级报警人员规则"
        align="center"
        prop="secondAlarmPeopleRule"
      />
      <el-table-column
        label="二级报警通知ID"
        align="center"
        prop="secondAlarmNoticeId"
      />
      <el-table-column
        label="三级报警规则"
        align="center"
        prop="thirdAlarmRule"
      />
      <el-table-column
        label="三级报警人员规则"
        align="center"
        prop="thirdAlarmPeopleRule"
      />
      <el-table-column
        label="三级报警通知ID"
        align="center"
        prop="thirdAlarmNoticeId"
      />
      <el-table-column
        label="聚集半径"
        align="center"
        prop="aggregationRadius"
      />
      <el-table-column label="持续时间" align="center" prop="duration" />
      <el-table-column
        label="静态围栏ID列表"
        align="center"
        prop="staticRailIds"
      />
      <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-edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(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="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="区域ID" prop="areaId">
          <el-input v-model="form.areaId" placeholder="请输入区域ID" />
        </el-form-item>
        <el-form-item label="区域名称" prop="areaName">
          <el-input v-model="form.areaName" placeholder="请输入区域名称" />
        </el-form-item>
        <el-form-item label="区域启用 Y/N" prop="areaEnable">
          <el-input
            v-model="form.areaEnable"
            placeholder="请输入区域启用 Y/N"
          />
        </el-form-item>
        <el-form-item label="规则JSON" prop="rule">
          <el-input
            v-model="form.rule"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="有效期开始日期" prop="validBeginDate">
          <el-date-picker
            clearable
            v-model="form.validBeginDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择有效期开始日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="有效期结束日期" prop="validEndDate">
          <el-date-picker
            clearable
            v-model="form.validEndDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择有效期结束日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="每日开始时间" prop="validBeginTime">
          <el-date-picker
            clearable
            v-model="form.validBeginTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择每日开始时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="每日结束时间" prop="validEndTime">
          <el-date-picker
            clearable
            v-model="form.validEndTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择每日结束时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="报警通知配置ID" prop="alarmNoticeId">
          <el-input
            v-model="form.alarmNoticeId"
            placeholder="请输入报警通知配置ID"
          />
        </el-form-item>
        <el-form-item label="通知配置名称" prop="alarmNoticeName">
          <el-input
            v-model="form.alarmNoticeName"
            placeholder="请输入通知配置名称"
          />
        </el-form-item>
        <el-form-item label="部门名称" prop="deptName">
          <el-input v-model="form.deptName" placeholder="请输入部门名称" />
        </el-form-item>
        <el-form-item label="报警人员姓名" prop="alarmPersonNames">
          <el-input
            v-model="form.alarmPersonNames"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="报警岗位名称" prop="alarmPostNames">
          <el-input
            v-model="form.alarmPostNames"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="围栏名称" prop="railName">
          <el-input v-model="form.railName" placeholder="请输入围栏名称" />
        </el-form-item>
        <el-form-item label="静态围栏名称" prop="staticRailName">
          <el-input
            v-model="form.staticRailName"
            placeholder="请输入静态围栏名称"
          />
        </el-form-item>
        <el-form-item label="人员姓名" prop="realName">
          <el-input v-model="form.realName" placeholder="请输入人员姓名" />
        </el-form-item>
        <el-form-item label="一级报警人员规则" prop="firstAlarmPeopleRule">
          <el-input
            v-model="form.firstAlarmPeopleRule"
            placeholder="请输入一级报警人员规则"
          />
        </el-form-item>
        <el-form-item label="二级报警规则" prop="secondAlarmRule">
          <el-input
            v-model="form.secondAlarmRule"
            placeholder="请输入二级报警规则"
          />
        </el-form-item>
        <el-form-item label="二级报警人员规则" prop="secondAlarmPeopleRule">
          <el-input
            v-model="form.secondAlarmPeopleRule"
            placeholder="请输入二级报警人员规则"
          />
        </el-form-item>
        <el-form-item label="二级报警通知ID" prop="secondAlarmNoticeId">
          <el-input
            v-model="form.secondAlarmNoticeId"
            placeholder="请输入二级报警通知ID"
          />
        </el-form-item>
        <el-form-item label="三级报警规则" prop="thirdAlarmRule">
          <el-input
            v-model="form.thirdAlarmRule"
            placeholder="请输入三级报警规则"
          />
        </el-form-item>
        <el-form-item label="三级报警人员规则" prop="thirdAlarmPeopleRule">
          <el-input
            v-model="form.thirdAlarmPeopleRule"
            placeholder="请输入三级报警人员规则"
          />
        </el-form-item>
        <el-form-item label="三级报警通知ID" prop="thirdAlarmNoticeId">
          <el-input
            v-model="form.thirdAlarmNoticeId"
            placeholder="请输入三级报警通知ID"
          />
        </el-form-item>
        <el-form-item label="聚集半径" prop="aggregationRadius">
          <el-input
            v-model="form.aggregationRadius"
            placeholder="请输入聚集半径"
          />
        </el-form-item>
        <el-form-item label="持续时间" prop="duration">
          <el-input v-model="form.duration" 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 {
  listSetting,
  getSetting,
  delSetting,
  addSetting,
  updateSetting,
} from "@/api/jinrun/alarm";

export default {
  name: "Setting",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 报警设置表格数据
      settingList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        areaId: null,
        areaName: null,
        areaEnable: null,
        alarmType: null,
        rule: null,
        validBeginDate: null,
        validEndDate: null,
        validBeginTime: null,
        validEndTime: null,
        alarmNoticeId: null,
        alarmNoticeName: null,
        railIds: null,
        deptIds: null,
        deptName: null,
        alarmPersonIds: null,
        alarmPersonNames: null,
        alarmPostIds: null,
        alarmPostNames: null,
        railName: null,
        staticRailName: null,
        realName: null,
        personIds: null,
        firstAlarmPeopleRule: null,
        secondAlarmRule: null,
        secondAlarmPeopleRule: null,
        secondAlarmNoticeId: null,
        thirdAlarmRule: null,
        thirdAlarmPeopleRule: null,
        thirdAlarmNoticeId: null,
        aggregationRadius: null,
        duration: null,
        staticRailIds: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询报警设置列表 */
    getList() {
      this.loading = true;
      listSetting(this.queryParams).then((response) => {
        this.settingList = response.data;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        createBy: null,
        createTime: null,
        updateTime: null,
        remark: null,
        areaId: null,
        areaName: null,
        areaEnable: null,
        alarmType: null,
        rule: null,
        validBeginDate: null,
        validEndDate: null,
        validBeginTime: null,
        validEndTime: null,
        alarmNoticeId: null,
        alarmNoticeName: null,
        railIds: null,
        deptIds: null,
        deptName: null,
        alarmPersonIds: null,
        alarmPersonNames: null,
        alarmPostIds: null,
        alarmPostNames: null,
        railName: null,
        staticRailName: null,
        realName: null,
        personIds: null,
        firstAlarmPeopleRule: null,
        secondAlarmRule: null,
        secondAlarmPeopleRule: null,
        secondAlarmNoticeId: null,
        thirdAlarmRule: null,
        thirdAlarmPeopleRule: null,
        thirdAlarmNoticeId: null,
        aggregationRadius: null,
        duration: null,
        staticRailIds: 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.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加报警设置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getSetting(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改报警设置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateSetting(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSetting(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除报警设置编号为"' + ids + '"的数据项？')
        .then(function () {
          return delSetting(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/setting/export",
        {
          ...this.queryParams,
        },
        `setting_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
