<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="name"
          :rules="{ required: true, message: '请输入模板名称', trigger: 'blur' }"
      >
        <el-input
            :maxlength="50"
            placeholder="请输入模板名称"
            clearable
            v-model.trim="formData.name"
        />
      </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="filePath"
          :rules="{ required: true, message: '模板文件', trigger: 'blur' }"
      >
        <UploadFile @upload="fileUpload" file-type=".xlsx.xls.doc.docx" name="file"
                    :file-list="formData.filePath?[{name:formData.fileName,url:formData.filePath}]:[]"/>
      </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 { fileTemplateAdd,fileTemplateUpdate,getFileTemplateDetail } from '@/api/file/fileTemplate';
import { computed, onMounted,shallowRef,reactive,ref,nextTick  } from "vue";
import { FormInstance } from "element-plus";
import { message } from "@/utils/auth";
const formRef = shallowRef<FormInstance>();
  import { useLockFn } from "@/utils/useLockFn";
import UploadFile from '@/components/Upload/file.vue';

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: "",
  name:'',
  code:'',
  filePath:'',
  fileName:''
});

const fileUpload = async (filePath: any, fileName: any) => {
  formData.filePath = filePath;
  formData.fileName = fileName;
  await nextTick(()=>{
    formRef.value?.validateField("filePath");
  })
};


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

const setFormData = async (id: any) => {
  const data = await getFileTemplateDetail(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 fileTemplateUpdate(formData) : await fileTemplateAdd(formData);
  message("操作成功");
  emit("update:visible", false);
  emit("success");
};

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


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

</script>
