<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="NAME">
        <el-input v-model="data.form.NAME" />
      </el-form-item>
      <el-form-item label="备注" prop="REMARKS">
        <el-input
          v-model="data.form.REMARKS"
          type="textarea"
          :autosize="{
            minRows: 3,
          }"
        />
      </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 { setSystemDocumentsAddFolder } from "@/request/system_operation.js";

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 = {
  NAME: [{ required: true, message: "名称不能为空", trigger: "blur" }],
};
const formRef = ref(null);
const data = reactive({
  form: {
    NAME: "",
    REMARKS: "",
  },
});
const fnClose = () => {
  formRef.value.resetFields();
  visible.value = false;
};
const fnSubmit = debounce(
  1000,
  async () => {
    await useFormValidate(formRef);
    await setSystemDocumentsAddFolder({
      ...data.form,
      PARENT_ID: props.mfolderId,
    });
    fnClose();
    ElMessage.success("保存成功");
    emits("get-data");
  },
  { atBegin: true }
);
</script>

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