<template>
  <a-modal v-model:visible="props.visible" :title="dialogTitle" width="600px" @cancel="dialogClose">
    <a-form ref="formRef" :model="formData" :label-col="{ style: { width: '90px' } }">
      <a-form-item
        label="模板名称"
        name="title"
        :rules="{ required: true, message: '请输入模板名称', trigger: 'blur' }"
      >
        <a-input
          :maxlength="50"
          placeholder="请输入模板名称"
          allow-clear
          v-model:value="formData.title"
        />
      </a-form-item>
      <a-form-item
        label="模板code"
        name="code"
        :rules="{ required: true, message: '请输入模板code', trigger: 'blur' }"
      >
        <a-input
          v-if="!formData.id"
          :maxlength="100"
          placeholder="请输入模板code"
          allow-clear
          v-model:value="formData.code"
        />
        <span v-else>{{ formData.code }}</span>
      </a-form-item>
      <a-form-item
        label="模板编号"
        name="number"
        :rules="{ required: true, message: '请输入模板编号', trigger: 'blur' }"
      >
        <a-input
          :maxlength="100"
          placeholder="请输入模板编号"
          allow-clear
          v-model:value="formData.number"
        />
      </a-form-item>
      <a-form-item
        label="消息类型"
        name="type"
        :rules="{ required: true, message: '请选择模板类型', trigger: 'blur' }"
      >
        <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-option :value="3">代办事项</a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item
        label="模板内容"
        name="content"
        :rules="{ required: true, message: '请输入模板内容', trigger: 'blur' }"
      >
        <a-textarea placeholder="请输入模板内容" allow-clear v-model:value="formData.content" />
      </a-form-item>
    </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 {
    messageTemplateAdd,
    messageTemplateUpdate,
    getMessageTemplateDetail,
  } from '@/api/file/messageTemplate';
  import { computed, onMounted, shallowRef, reactive } from 'vue';
  import type { FormInstance } from 'ant-design-vue';
  import { message } from 'ant-design-vue';
  import { useLockFn } from '@/utils/useLockFn';

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

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

  /**
   * 定义弹窗标题
   */
  const dialogTitle = computed(() => {
    return props.id ? '编辑消息模板' : '新增消息模板';
  });

  const formRef = shallowRef<FormInstance>();

  /**
   * 接收表单参数
   */
  const formData = reactive({
    id: '',
    title: '',
    code: '',
    number: '',
    content: '',
    type: undefined,
  });

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

  /**
   * 设置表单数据
   */
  const setFormData = async (id: any) => {
    const data = await getMessageTemplateDetail(id);
    for (const key in formData) {
      if (data[key] != null && data[key] != undefined) {
        formData[key] = data[key];
      }
    }
  };

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

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

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