<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="模板名称"
          name="title"
          :rule="{ required: true, message: '请输入模板名称', trigger: 'blur' }"
        >
          <n-input
            :maxlength="50"
            placeholder="请输入模板名称"
            clearable
            v-model:value="formData.title"
          />
        </n-form-item>
        <n-form-item
          label="模板code"
          name="code"
          :rule="{ required: true, message: '请输入模板code', trigger: 'blur' }"
        >
          <n-input
            v-if="!formData.id"
            :maxlength="100"
            placeholder="请输入模板code"
            clearable
            v-model:value="formData.code"
          />
          <span v-else>{{ formData.code }}</span>
        </n-form-item>
        <n-form-item
          label="模板编号"
          name="number"
          :rule="{ required: true, message: '请输入模板编号', trigger: 'blur' }"
        >
          <n-input
            :maxlength="100"
            placeholder="请输入模板编号"
            clearable
            v-model:value="formData.number"
          />
        </n-form-item>
        <n-form-item
          label="消息类型"
          name="type"
          :rule="{ type: 'number', required: true, message: '请选择模板类型', trigger: 'blur' }"
        >
          <n-select
            v-model:value="formData.type"
            placeholder="请选择模板类型"
            :options="typeList"
          />
        </n-form-item>
        <n-form-item
          label="模板内容"
          name="content"
          :rule="{ required: true, message: '请输入模板内容', trigger: 'blur' }"
        >
          <n-input
            type="textarea"
            placeholder="请输入模板内容"
            clearable
            v-model:value="formData.content"
          />
        </n-form-item>
      </n-form>
    </template>
  </basicModal>
</template>
<script lang="ts" setup>
  import {
    messageTemplateAdd,
    messageTemplateUpdate,
    getMessageTemplateDetail,
  } from '@/api/file/messageTemplate';
  import { onMounted, reactive, ref } from 'vue';
  import { useMessage } from 'naive-ui';
  import { useModal } from '@/components/Modal';

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

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

  /**
   * 定义接收的参数
   */
  const props = defineProps({
    visible: {
      type: Boolean,
      required: true,
      default: false,
    },
    messageId: {
      type: Number,
      required: true,
      default: 0,
    },
  });

  /**
   * 定义数据源
   */
  const typeList = [
    { label: '系统通知', value: 1 },
    { label: '用户私信', value: 2 },
    { label: '待办事项', value: 3 },
  ];

  /**
   * 定义模态
   */
  const [modalRegister, { openModal, setSubLoading }] = useModal({
    title: props.messageId ? '编辑文件模板' : '添加文件模板',
    subBtuText: '确定',
    width: 600,
  });

  /**
   * 执行提交
   */
  const handleSubmit = async () => {
    formRef.value
      .validate()
      .then(async () => {
        props.messageId
          ? await messageTemplateUpdate(formData)
          : await messageTemplateAdd(formData);
        message.success('操作成功');
        setSubLoading(false);
        emit('update:visible', false);
        emit('success');
      })
      .catch((error) => {
        setSubLoading(false);
      });
  };

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

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

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

  /**
   * 定义函数
   */
  defineExpose({
    openModal,
  });
</script>
