<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? '新增通知' : '修改通知'" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="100px">
      <el-form-item prop="level" label="级别">
        <el-select v-model="dataForm.level" placeholder="级别">
          <el-option label="提示" value="info"></el-option>
          <el-option label="警告" value="warning"></el-option>
          <el-option label="紧急" value="danger"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="title" label="标题">
        <el-input v-model="dataForm.title" placeholder="标题"></el-input>
      </el-form-item>
      <el-form-item prop="content" label="内容">
        <el-input v-model="dataForm.content" type="textarea" :rows="5" placeholder="通知正文"></el-input>
      </el-form-item>
      <el-form-item prop="sourceType" label="来源类型">
        <el-select v-model="dataForm.sourceType" placeholder="来源类型">
          <el-option label="手动发布" value="manual"></el-option>
          <el-option label="导入任务" value="import"></el-option>
          <el-option label="定时任务" value="schedule"></el-option>
          <el-option label="数据源监测" value="datasource"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="expireTime" label="过期时间">
        <el-date-picker
          v-model="dataForm.expireTime"
          type="datetime"
          placeholder="过期时间（选填）"
          value-format="YYYY-MM-DD HH:mm:ss"
          style="width: 100%"
        ></el-date-picker>
      </el-form-item>
      <el-form-item v-if="dataForm.id" label="发布时间">
        <span>{{ dataForm.publishTime }}</span>
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">确定</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import baseService from "@/service/baseService";
import { ElMessage } from "element-plus";

const emit = defineEmits(["refreshDataList"]);

const visible = ref(false);
const dataFormRef = ref();

const defaultForm = () => ({
  id: "",
  level: "info",
  title: "",
  content: "",
  sourceType: "manual",
  expireTime: "",
  publishTime: ""
});

const dataForm = reactive(defaultForm());

const rules = ref({
  level: [{ required: true, message: "必填项不能为空", trigger: "change" }],
  title: [{ required: true, message: "必填项不能为空", trigger: "blur" }],
  content: [{ required: true, message: "必填项不能为空", trigger: "blur" }]
});

const init = (id?: string) => {
  visible.value = true;
  Object.assign(dataForm, defaultForm());

  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }

  if (id) {
    getInfo(id);
  }
};

const getInfo = (id: string) => {
  baseService.get(`/sys/alert/${id}`).then((res) => {
    Object.assign(dataForm, res.data);
  });
};

const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    (!dataForm.id ? baseService.post : baseService.put)("/sys/alert", {
      ...dataForm,
      id: dataForm.id || undefined,
      expireTime: dataForm.expireTime || undefined,
      publishTime: undefined
    }).then(() => {
      ElMessage.success({
        message: "成功",
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({ init });
</script>
