<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备名称" prop="equipmentId">
        <!-- 可搜索设备名称，支持模糊查询 -->
        <el-select v-model="queryParams.equipmentId" placeholder="请选择对应设备" clearable style="width: 150px;" filterable>
          <el-option v-for="dict in equipmentOptions" :key="dict.equipmentId" :label="dict.equipmentName"
            :value="dict.equipmentId" />
        </el-select>
      </el-form-item>
      <el-form-item label="告警名称" prop="alarmName">
        <el-input v-model="queryParams.alarmName" placeholder="请输入告警名称" style="width: 150px;" clearable
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="告警等级" prop="alarmLevel">
        <el-select v-model="queryParams.alarmLevel" placeholder="请选择告警等级" style="width: 150px;" clearable>
          <el-option v-for="dict in alarm_level" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="告警类型" prop="alarmType">
        <el-select v-model="queryParams.alarmType" placeholder="请选择告警类型" style="width: 150px;" clearable>
          <el-option v-for="dict in alarm_type" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="告警源" prop="source">
        <el-select v-model="queryParams.source" placeholder="请选择告警源" style="width: 150px;" clearable>
          <el-option v-for="dict in alarm_source" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="单位" prop="unit">
        <el-select v-model="queryParams.unit" placeholder="请选择单位" style="width: 150px;" clearable>
          <el-option v-for="dict in unit_type" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="通知方式" prop="notificationMethod">
        <el-select v-model="queryParams.notificationMethod" placeholder="请选择通知方式" style="width: 150px;" clearable>
          <el-option v-for="dict in notification_method" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @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="Plus" @click="handleAdd"
          v-hasPermi="['hnyz:alarmConfig:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
          v-hasPermi="['hnyz:alarmConfig:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['hnyz:alarmConfig:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['hnyz:alarmConfig:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="alarmConfigList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="设备id" align="center" prop="equipmentId" />
      <el-table-column label="告警代码" align="center" prop="alarmCode" />
      <el-table-column label="告警名称" align="center" prop="alarmName" />
      <el-table-column label="告警等级" align="center" prop="alarmLevel">
        <template #default="scope">
          <dict-tag :options="alarm_level" :value="scope.row.alarmLevel" />
        </template>
      </el-table-column>
      <el-table-column label="告警类型" align="center" prop="alarmType">
        <template #default="scope">
          <dict-tag :options="alarm_type" :value="scope.row.alarmType" />
        </template>
      </el-table-column>
      <el-table-column label="告警源" align="center" prop="source">
        <template #default="scope">
          <dict-tag :options="alarm_source" :value="scope.row.source" />
        </template>
      </el-table-column>
      <el-table-column label="阈值类型" align="center" prop="thresholdType">
        <template #default="scope">
          <dict-tag :options="threshold_type" :value="scope.row.thresholdType" />
        </template>
      </el-table-column>
      <el-table-column label="阈值设置" align="center" prop="thresholdValue" />
      <el-table-column label="单位" align="center" prop="unit">
        <template #default="scope">
          <dict-tag :options="unit_type" :value="scope.row.unit" />
        </template>
      </el-table-column>
      <el-table-column label="延迟触发时间(s)" align="center" prop="delaySeconds" />
      <el-table-column label="告警通知" align="center" prop="description" />
      <el-table-column label="通知方式" align="center" prop="notificationMethod">
        <template #default="scope">
          <dict-tag :options="notification_method" :value="scope.row.notificationMethod" />
        </template>
      </el-table-column>
      <el-table-column label="启用状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="sys_normal_disable" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="备注信息" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['hnyz:alarmConfig:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['hnyz:alarmConfig:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改告警配置管理对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="alarmConfigRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="设备名称" prop="equipmentId">
          <!-- 可搜索设备名称，支持模糊查询 -->
          <el-select v-model="form.equipmentId" placeholder="请选择对应设备,不选默认适应全部设备" clearable filterable>
            <el-option v-for="dict in equipmentOptions" :key="dict.equipmentId" :label="dict.equipmentName"
              :value="dict.equipmentId" />
          </el-select>
        </el-form-item>
        <el-form-item label="告警代码" prop="alarmCode">
          <el-input v-model="form.alarmCode" placeholder="请输入告警代码" />
        </el-form-item>
        <el-form-item label="告警名称" prop="alarmName">
          <el-input v-model="form.alarmName" placeholder="请输入告警名称" />
        </el-form-item>
        <el-form-item label="告警等级" prop="alarmLevel">
          <el-select v-model="form.alarmLevel" placeholder="请选择告警等级">
            <el-option v-for="dict in alarm_level" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="告警类型" prop="alarmType">
          <el-select v-model="form.alarmType" placeholder="请选择告警类型">
            <el-option v-for="dict in alarm_type" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="告警源" prop="source">
          <el-select v-model="form.source" placeholder="请选择告警源">
            <el-option v-for="dict in alarm_source" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="阈值类型" prop="thresholdType">
          <el-select v-model="form.thresholdType" placeholder="请选择阈值类型">
            <el-option v-for="dict in threshold_type" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="阈值设置" prop="thresholdValue">
          <el-input v-model="form.thresholdValue" placeholder="请输入阈值设置" />
        </el-form-item>
        <el-form-item label="单位" prop="unit">
          <el-select v-model="form.unit" placeholder="请选择单位">
            <el-option v-for="dict in unit_type" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="延迟触发时间" prop="delaySeconds">
          <el-input v-model="form.delaySeconds" placeholder="请输入延迟触发时间(s)" />
        </el-form-item>
        <el-form-item label="告警通知" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="通知方式" prop="notificationMethod">
          <el-select v-model="form.notificationMethod" placeholder="请选择通知方式">
            <el-option v-for="dict in notification_method" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注信息" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注信息" />
        </el-form-item>
        <el-form-item label="启用状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio v-for="dict in sys_normal_disable" :key="dict.value" :label="dict.value">{{ dict.label
              }}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="AlarmConfig">
import { listAlarmConfig, getAlarmConfig, delAlarmConfig, addAlarmConfig, updateAlarmConfig } from "@/api/hnyz/alarmConfig";
import { getEquipmentList } from "@/api/hnyz/equipment";
import { onMounted } from "vue";

onMounted(() => {
  getEquipmentOptions();
});

const equipmentOptions = ref([]);
function getEquipmentOptions() {
  getEquipmentList().then(response => {
    equipmentOptions.value = response.data;
  });
}
const { proxy } = getCurrentInstance();
const { threshold_type, alarm_level, alarm_source, unit_type, notification_method, alarm_type } = proxy.useDict('threshold_type', 'alarm_level', 'alarm_source', 'unit_type', 'notification_method', 'alarm_type');
const { sys_normal_disable } = proxy.useDict('sys_normal_disable');

const alarmConfigList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    equipmentId: null,
    alarmCode: null,
    alarmName: null,
    alarmLevel: null,
    alarmType: null,
    source: null,
    thresholdType: null,
    thresholdValue: null,
    unit: null,
    delaySeconds: null,
    description: null,
    notificationMethod: null,
    status: null,
  },
  rules: {
    alarmCode: [
      { required: true, message: "告警代码，唯一标识一个告警类型不能为空", trigger: "blur" }
    ],
    alarmName: [
      { required: true, message: "告警名称，便于识别和管理不能为空", trigger: "blur" }
    ],
    alarmLevel: [
      { required: true, message: "告警等级不能为空", trigger: "change" }
    ],
    alarmType: [
      { required: true, message: "告警类型，例如：高温、低压、通信异常等不能为空", trigger: "change" }
    ],
    source: [
      { required: true, message: "告警源不能为空", trigger: "change" }
    ],
    thresholdType: [
      { required: true, message: "阈值类型，支持多种比较方式不能为空", trigger: "change" }
    ],
    delaySeconds: [
      { required: true, message: "延迟触发时间不能为空", trigger: "blur" }
    ],
    description: [
      { required: true, message: "告警通知不能为空", trigger: "blur" }
    ],
    notificationMethod: [
      { required: true, message: "不能为空", trigger: "change" }
    ],
    status: [
      { required: true, message: "是否启用该告警配置不能为空", trigger: "change" }
    ],
    createTime: [
      { required: true, message: "创建时间不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询告警配置管理列表 */
function getList() {
  loading.value = true;
  listAlarmConfig(queryParams.value).then(response => {
    alarmConfigList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    configId: null,
    equipmentId: null,
    alarmCode: null,
    alarmName: null,
    alarmLevel: null,
    alarmType: null,
    source: null,
    thresholdType: null,
    thresholdValue: null,
    unit: null,
    delaySeconds: null,
    description: null,
    notificationMethod: null,
    status: null,
    createTime: null,
    createBy: null,
    updateTime: null,
    updateBy: null,
    remark: null
  };
  proxy.resetForm("alarmConfigRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.configId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加告警配置管理";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _configId = row.configId || ids.value
  getAlarmConfig(_configId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改告警配置管理";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["alarmConfigRef"].validate(valid => {
    if (valid) {
      if (form.value.configId != null) {
        updateAlarmConfig(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addAlarmConfig(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _configIds = row.configId || ids.value;
  proxy.$modal.confirm('是否确认删除告警配置管理编号为"' + _configIds + '"的数据项？').then(function () {
    return delAlarmConfig(_configIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('hnyz/alarmConfig/export', {
    ...queryParams.value
  }, `alarmConfig_${new Date().getTime()}.xlsx`)
}

getList();
</script>
