<template>
  <basicModal
    @register="modalRegister"
    ref="modalRef"
    class="basicModal basicFormModal"
    @on-ok="handleSubmit"
    @on-close="handleClose"
  >
    <template #default>
      <n-form
        class="ls-form"
        ref="formRef"
        :model="formData"
        label-placement="left"
        label-width="85px"
      >
        <n-form-item
          label="通知标题"
          path="title"
          :rule="{ required: true, message: '请输入通知标题', trigger: 'blur' }"
        >
          <n-input
            class="ls-input"
            v-model:value="formData.title"
            placeholder="通知标题"
            clearable
          />
        </n-form-item>
        <n-form-item label="通知类型">
          <n-select
            v-model:value="formData.type"
            placeholder="请选择通知类型"
            :options="typeList"
          />
        </n-form-item>
        <n-form-item label="通知状态" path="status">
          <n-radio-group v-model:value="formData.status" name="status">
            <n-radio :value="1">正常</n-radio>
            <n-radio :value="2">关闭</n-radio>
          </n-radio-group>
        </n-form-item>
        <div class="flex">
          <n-form-item
            class="flex-1"
            label="通知内容"
            path="content"
            :rules="{ required: true, message: '请输入通知内容', trigger: 'blur' }"
          >
            <Editor ref="editorRef" :height="fwbHeight" class="flex-1" name="notice" />
          </n-form-item>
        </div>
      </n-form>
    </template>
  </basicModal>
</template>
<script lang="ts" setup>
  import { getNoticeDetail, noticeAdd, noticeUpdate } from '@/api/data/notice';
  import { onMounted, reactive, ref } from 'vue';
  import { useMessage } from 'naive-ui';
  import Editor from '@/components/Editor/tinymce.vue';
  import { useModal } from '@/components/Modal';

  const emit = defineEmits(['success', 'update:visible']);
  const formRef = ref();

  /**
   * 定义表单参数
   */
  const message = useMessage();
  const formData = reactive({
    id: '',
    title: '',
    status: 1,
    type: 1,
    content: '',
  });
  const editorRef = ref();
  const fwbHeight = document.body.clientHeight - 400;

  /**
   * 定义接收的参数
   */
  const props = defineProps({
    visible: {
      type: Boolean,
      required: true,
      default: false,
    },
    noticeId: {
      type: Number,
      required: true,
      default: 0,
    },
  });
  const [modalRegister, { openModal, setSubLoading }] = useModal({
    title: props.noticeId ? '编辑公告' : '添加公告',
    subBtuText: '确定',
    width: document.body.clientWidth - 500,
  });
  const typeList = [
    { label: '通知', value: 1 },
    { label: '公告', value: 2 },
  ];
  /**
   * 执行提交
   */
  const handleSubmit = async () => {
    formData.content = editorRef.value.myValue;
    formRef.value
      .validate()
      .then(async () => {
        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');
      })
      .catch((error) => {
        setSubLoading(false);
      });
  };

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

  /**
   * 设置表单数据
   */
  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();
    }
  });
  //导出方法
  defineExpose({
    openModal,
  });
</script>
