<template>
  <el-dialog v-model="visible" title="上传文件" :before-close="fnClose">
    <el-form ref="formRef" :model="data.form" :rules="rules" label-width="80px">
      <el-form-item label="附件" prop="file">
        <layout-upload
          v-model:file-list="data.form.file"
          accept=".pdf"
          :limit="99"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="fnClose">取消</el-button>
      <el-button type="primary" @click="fnSubmit"> 确定 </el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import { useVModel } from "@vueuse/core";
import { reactive, ref } from "vue";
import useFormValidate from "@/assets/js/useFormValidate.js";
import { ElMessage } from "element-plus";
import { debounce } from "throttle-debounce";
import { setSystemDocumentsBatchUploadFile } from "@/request/system_operation.js";
import LayoutUpload from "@/components/upload/index.vue";

const props = defineProps({
  visible: {
    type: Boolean,
    required: true,
    default: false,
  },
  mfolderId: {
    type: String,
    required: true,
    default: "",
  },
});
const emits = defineEmits(["update:visible", "get-data"]);
const visible = useVModel(props, "visible", emits);
const rules = {
  file: [{ required: true, message: "附件不能为空", trigger: "change" }],
};
const formRef = ref(null);
const data = reactive({
  form: {
    file: [],
  },
});
const fnClose = () => {
  formRef.value.resetFields();
  visible.value = false;
};
const fnSubmit = debounce(
  1000,
  async () => {
    await useFormValidate(formRef);
    const formData = new FormData();
    Object.keys(data.form).forEach((key) => {
      formData.append(key, data.form[key]);
    });
    formData.delete("file");
    formData.append("PARENT_ID", props.mfolderId);
    for (let i = 0; i < data.form.file.length; i++) {
      formData.append("FFILE", data.form.file[i].raw);
    }
    await setSystemDocumentsBatchUploadFile(formData);
    fnClose();
    ElMessage.success("保存成功");
    emits("get-data");
  },
  { atBegin: true }
);
</script>

<style scoped lang="scss"></style>
