<template>
  <a-modal
    v-model:visible="props.visible"
    :title="props.noticeId ? '编辑' : '新增'"
    :width="`${size}px`"
    @cancel="dialogClose"
    style="top: 20px"
  >
    <a-form
      class="ls-form"
      ref="formRef"
      :model="formData"
      :label-col="{ style: { width: '80px' } }"
    >
      <a-form-item
        label="通知标题"
        name="title"
        :rules="{ required: true, message: '请输入通知标题', trigger: 'blur' }"
      >
        <a-input class="ls-input" v-model:value="formData.title" placeholder="通知标题" clearable />
      </a-form-item>
      <a-form-item label="通知类型">
        <a-select v-model:value="formData.type" placeholder="请选择通知类型">
          <a-select-option :value="1">通知</a-select-option>
          <a-select-option :value="2">公告</a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item label="通知状态" name="status">
        <a-radio-group v-model:value="formData.status" name="status">
          <a-radio :value="1">正常</a-radio>
          <a-radio :value="2">关闭</a-radio>
        </a-radio-group>
      </a-form-item>
      <div class="flex">
        <a-form-item
          class="flex-1"
          label="通知内容"
          name="content"
          :rules="{ required: true, message: '请输入通知内容', trigger: 'blur' }"
        >
          <Editor ref="editorRef" :height="fwbHeight" class="flex-1" name="notice" />
        </a-form-item>
      </div>
    </a-form>
    <template #footer>
      <span class="dialog-footer">
        <a-button @click="dialogClose">取消</a-button>
        <a-button :loading="subLoading" type="primary" @click="submit"> 确定 </a-button>
      </span>
    </template>
  </a-modal>
</template>
<script lang="ts" setup>
  import type { FormInstance } from 'element-plus';
  import { getNoticeDetail, noticeAdd, noticeUpdate } from '@/api/data/notice';
  import { onMounted, reactive, shallowRef, ref } from 'vue';
  import Editor from '@/components/Editor/tinymce.vue';
  import { message } from 'ant-design-vue';
  import { useLockFn } from '@/utils/useLockFn';

  /**
   * 定义参数变量
   */
  const size = document.body.clientWidth - 500;
  const emit = defineEmits(['success', 'update:visible']);
  const formRef = shallowRef<FormInstance>();
  const editorRef = ref();

  /**
   * 定义表单参数
   */
  const formData = reactive({
    id: '',
    title: '',
    status: 1,
    type: 1,
    content: '',
  });

  /**
   * 定义接收的参数
   */
  const props = defineProps({
    visible: {
      type: Boolean,
      required: true,
      default: false,
    },
    noticeId: {
      type: Number,
      required: true,
      default: 0,
    },
  });
  const fwbHeight = document.body.clientHeight - 400;

  /**
   * 执行提交表单
   */
  const handleSubmit = async () => {
    formData.content = editorRef.value.myValue;
    await formRef.value?.validate();
    let ruleForm = JSON.parse(JSON.stringify(formData));
    ruleForm.content = editorRef.value.myValue;
    props.noticeId ? await noticeUpdate(ruleForm) : await noticeAdd(ruleForm);
    message.success('操作成功');
    emit('update:visible', false);
    emit('success');
  };

  /**
   * 关闭窗体
   */
  const dialogClose = () => {
    emit('update:visible', false);
  };

  const { isLock: subLoading, lockFn: submit } = useLockFn(handleSubmit);

  /**
   * 设置表单数据
   */
  const setFormData = async () => {
    const data = await getNoticeDetail(props.noticeId);
    for (const key in formData) {
      if (data[key] != null && data[key] != undefined) {
        //@ts-ignore
        formData[key] = data[key];
      }
    }
    editorRef.value.myValue = formData.content;
  };

  /**
   * 钩子函数
   */
  onMounted(() => {
    if (props.noticeId) {
      setFormData();
    }
  });
</script>
