<template>
  <el-dialog v-model="props.visible" :title="dialogTitle" :append-to-body="true" 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="模板code"
          prop="code"
          :rules="{ required: true, message: '请输入模板code', trigger: 'blur' }"
      >
        <el-input
            v-if="!formData.id"
            :maxlength="100"
            placeholder="请输入模板code"
            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>
            <el-option label="腾讯云" :value="2"></el-option>
            <el-option label="华为云" :value="3"></el-option>
          </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";
const formRef = shallowRef<FormInstance>();
  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 formData = reactive({
  id: "",
  title:'',
  code:'',
  number:'',
  content:'',
  type:'',
});


const dialogClose = () => {
  emit("update:visible", false);
};

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>
