<template>
    <el-dialog
      v-model="open"
      width="800px"
      append-to-body
    >
      <template v-slot:header>
        <div class="cleartitle" style="display: flex;justify-content: flex-start;align-items: center;">
          <img src="@/assets/images/logo_video.png" width="25px" alt=""> <span>{{title}}</span>
        </div>
      </template>
         <el-row :gutter="10" class="mb8" style="justify-content: space-between;">
          <el-col :span="1.5">
            <div class="form-title" style="display: flex;justify-content: flex-start;align-items: center;">
               <span>告警等级</span>
            </div>
         </el-col>
         <el-col :span="6.8" style="display: flex;">
            <el-button
               type="primary"
               plain
               icon="Plus"
               @click="handleLevelAdd"
            >新增</el-button>
         </el-col>
        </el-row>
         <el-card class="left-list">
          <el-table v-loading="loading" :data="algLevelList">
            <el-table-column label="告警等级名称" align="center" prop="alarmName" />
            <el-table-column label="告警等级颜色" align="center" prop="alarmColor"> 
              <template #default="scope">
                <span>
                  <el-tag type="success" :style="{backgroundColor: scope.row.alarmColor,width:'50px'}">{{ }}</el-tag>
                </span>
              </template>
            </el-table-column>
            <el-table-column label="告警等级展示方式" align="center" prop="alarmShowType" />
            <el-table-column label="操作" width="180" align="center" class-name="small-padding fixed-width">
                <template #default="scope">
                  <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:post:edit']">修改</el-button>
                  <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:post:remove']">删除</el-button>
                </template>
            </el-table-column>
          </el-table>
         </el-card>
 
    </el-dialog>
    <el-dialog
      v-model="openAdd"
      width="800px"
      append-to-body
    > 
      <template v-slot:header>
        <div class="cleartitle" style="display: flex;justify-content: flex-start;align-items: center;">
          <img src="@/assets/images/logo_video.png" width="25px" alt=""> <span>{{titleLevel}}</span>
        </div>
      </template>
      <el-form ref="algLevelRef" :model="form" :rules="rules" label-width="160px">
          <el-form-item label="告警等级名称" prop="alarmName">
            <el-input v-model="form.alarmName" placeholder="请输入告警等级名称" />
          </el-form-item>
          <el-form-item label="告警等级颜色" prop="alarmColor">
            <el-color-picker v-model="form.alarmColor" />
          </el-form-item>
          <el-form-item label="告警展示方式" prop="alarmShowType">
            <el-checkbox-group v-model="form.alarmShowType">
              <el-checkbox label="H5推送" value="H5推送" />
              <el-checkbox label="企业微信群推送" value="企业微信群推送" />
              <el-checkbox label="钉钉群推送" value="钉钉群推送" />
              <el-checkbox label="短信推送" value="短信推送" />
              <el-checkbox label="语音推送" value="语音推送" />
            </el-checkbox-group>
          </el-form-item>
      </el-form>
      <template #footer>
            <div class="dialog-footer">
               <el-button type="primary" :loading="butLoading" @click="submitForm">确 定</el-button>
               <el-button @click="cancel">取 消</el-button>
            </div>
         </template>
    </el-dialog>
</template>

<script setup>
import { listAlgLevel, detailAlgLevel , addAlgLevel, updateAlgLevel, deleteAlgLevel} from "@/api/algorithmList/algorithmDown.js";
const { proxy } = getCurrentInstance();
const emit = defineEmits();
const open = ref(false);
const openAdd = ref(false);
const butLoading = ref(false);
const title = ref("");
const titleLevel = ref("");
const loading = ref(true);
const algLevelList = ref([]);
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    postCode: undefined,
  },
  rules: {
    alarmName: [{ required: true, message: "告警等级名称不能为空", trigger: "blur" }],
    postCode: [{ required: true, message: "岗位编码不能为空", trigger: "blur" }],
    postSort: [{ required: true, message: "岗位顺序不能为空", trigger: "blur" }],
  }
});

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

/** 表单重置 */
function reset() {
  butLoading.value = false;
  form.value = {
    id: undefined,
    alarmName: undefined,
    alarmColor: '#409EFF',
    alarmShowType: []
  };
  proxy.resetForm("algLevelRef");
}

/** 查询算法列表 */
function getLevelList() {
  loading.value = true;
  listAlgLevel(queryParams.value).then(response => {
    algLevelList.value = response.rows
    loading.value = false;
  });
}

/** 新增按钮操作 */
function handleAdd() {
  open.value = true;
  title.value = "告警等级列表";
  getLevelList()
}
/** 等级新增按钮操作 */
function handleLevelAdd() {
  reset();
  openAdd.value = true;
  titleLevel.value = "新增告警等级";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const id = row.id || ids.value;
  detailAlgLevel(id).then(response => {
    form.value = response.data;
    form.value.alarmShowType=form.value.alarmShowType.split(',')
    openAdd.value = true;
    titleLevel.value = "修改告警等级";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["algLevelRef"].validate(valid => {
    if (valid) {
      form.value.alarmShowType=form.value.alarmShowType.join()
      butLoading.value = true;
      if (form.value.id != undefined) {
        updateAlgLevel(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          openAdd.value = false;
          butLoading.value = false;
          getLevelList();
        });
      } else {
        addAlgLevel(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          openAdd.value = false;
          butLoading.value = false;
          getLevelList();
        });
      }
    }
  });
}
/** 取消按钮 */
function cancel() {
  openAdd.value = false;
  reset();
}
/** 删除按钮操作 */
function handleDelete(row) {
    const id = row.id || ids.value;
    proxy.$modal.confirm('删除后，已配置的告警等级将被清空').then(function() {
      return deleteAlgLevel(id);
    }).then(() => {
      getLevelList();
      proxy.$modal.msgSuccess("删除成功");
    }).catch(() => {});
  }
defineExpose({ handleAdd , handleUpdate })
</script>

<style scoped lang="scss">
    .form-title{
      display: flex;
      align-items:center;/*for vertical align*/
      font-size: 16px;
      font-weight: 600;
      border-radius: 5px;
      padding: 0;
    }
    .form-title::before{
      content:"";
      display: inline-block;
      width: 10px; /* 矩形的宽度 */
      height: 30px; /* 矩形的高度 */
      background-color: #1890FF; /* 矩形的背景颜色 */
      margin-right: 10px;
      border-radius: 8px;
    }
</style>