<template>
  <n-drawer v-model:show="props.visible" :width="size" @update:show="handleUpdate">
    <n-drawer-content :title="props.noticeId ? '编辑公告' : '新增公告'" closable>
      <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="通知封面"
            path="cover"
            :rule="{ required: true, message: '请上传通知封面', trigger: 'change' }"
          >
            <UploadImg
              @changeFileName="(name) => (formData.imageImgName = name)"
              :fileType="['image/jpeg', 'image/png', 'image/jpg', 'image/gif']"
              name="notice"
              :fileSize="20"
              v-model:image-url="formData.cover"
            >
              <template #tip>支持扩展名: jpg png jpeg;文件大小不超过20M</template>
            </UploadImg>
          </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"
              :rule="{ required: true, message: '请输入通知内容', trigger: 'blur' }"
            >
              <Editor ref="editorRef" :height="fwbHeight" class="flex-1" name="notice" />
            </n-form-item>
          </div>
        </n-form>
      </template>
      <template #footer>
        <n-space>
          <n-button @click="handleClose">取消</n-button>
          <n-button :loading="subLoading" type="primary" @click="submit"> 确定 </n-button>
        </n-space>
      </template>
    </n-drawer-content>
  </n-drawer>
</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 UploadImg from '@/components/Upload/Image.vue';
  import Editor from '@/components/Editor/tinymce.vue';
  import { useLockFn } from '@/utils/useLockFn';

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

  /**
   * 定义表单参数
   */
  const message = useMessage();
  const size = document.body.clientWidth - 500;

  /**
   * 定义表单参数
   */
  const formData = reactive({
    id: '',
    title: '',
    cover: '',
    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 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) => {});
  };

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

  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>
