<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="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="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="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 { messageTemplateAdd,messageTemplateUpdate,getMessageTemplateDetail } from '@/api/file/messageTemplate';
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 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("操作成功");
  emit("update:visible", false);
  emit("success");
};

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


onMounted(() => {
  if (props.id) {
    setFormData( props.id );
  }
});

</script>
