<template>
  <el-dialog
    v-model="props.visible"
    :title="dialogTitle"
    width="600"
    :close-on-click-modal="false"
    :before-close="dialogClose"
  >
    <el-form ref="formRef" :model="formData" label-width="90px">
      <el-form-item
        label="模板名称"
        prop="title"
        :rules="{ required: true, message: '请输入模板名称', trigger: 'blur' }"
      >
        <el-input
          :maxlength="50"
          placeholder="请输入模板名称"
          clearable
          v-model.trim="formData.title"
        />
      </el-form-item>
      <el-form-item
        label="模板编码"
        prop="code"
        :rules="{ required: true, message: '请输入模板编码', trigger: 'blur' }"
      >
        <el-input
          v-if="!formData.id"
          :maxlength="100"
          placeholder="请输入模板编码"
          clearable
          v-model.trim="formData.code"
        />
        <span v-else>{{ formData.code }}</span>
      </el-form-item>
      <el-form-item
        label="模板编号"
        prop="number"
        :rules="{ required: true, message: '请输入模板编号', trigger: 'blur' }"
      >
        <el-input
          :maxlength="100"
          placeholder="请输入模板编号"
          clearable
          v-model.trim="formData.number"
        />
      </el-form-item>
      <el-form-item
        label="模板类型"
        prop="type"
        :rules="{ required: true, message: '请选择模板类型', trigger: 'blur' }"
      >
        <el-select v-model="formData.type" placeholder="请选择模板类型">
          <el-option label="阿里云" :value="1" />
          <el-option label="腾讯云" :value="2" />
          <el-option label="华为云" :value="3" />
        </el-select>
      </el-form-item>
      <el-form-item
        label="模板内容"
        prop="content"
        :rules="{ required: true, message: '请输入模板内容', trigger: 'blur' }"
      >
        <el-input
          placeholder="请输入模板内容"
          clearable
          type="textarea"
          v-model.trim="formData.content"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogClose">取消</el-button>
        <el-button :loading="subLoading" type="primary" @click="submit">确认</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
  import { smsTemplateAdd, smsTemplateUpdate, getSmsTemplateDetail } from '@/api/file/smsTemplate';
  import { computed, onMounted, shallowRef, reactive, ref } from 'vue';
  import { FormInstance } from 'element-plus';
  import { message } from '@/utils/auth';
  import { useLockFn } from '@/utils/useLockFn';

  const formRef = shallowRef<FormInstance>();

  /**
   * 定义接收的参数
   */
  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 formData = reactive({
    id: '',
    title: '',
    code: '',
    number: '',
    content: '',
    type: '',
  });

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

  /**
   * 设置表单数据
   * @param id 参数
   */
  const setFormData = async (id: any) => {
    const data = await getSmsTemplateDetail(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 smsTemplateUpdate(formData) : await smsTemplateAdd(formData);
    message('操作成功');
    emit('update:visible', false);
    emit('success');
  };

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

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