<template>
  <Dialog v-model="diaVisible" diaTitle="上传文件" :width="400" @close="closeFun">
    <div>
      <TableForm
        ref="tableFormRef"
        tableName="DATA_OVERVIEW_ADD_FILE"
        :itemNum="1"
        v-model:data="formData"
      />
    </div>
    <template #footer>
      <div>
        <el-button @click="closeFun">{{ $t('通用.取消') }}</el-button>
        <el-button type="primary" @click="submitFun">{{ $t('通用.确定') }}</el-button>
      </div>
    </template>
  </Dialog>
</template>

<script setup lang="ts">
import { ref, watch } from 'vue';
import Dialog from '@/components/common/dialog/index.vue';
import TableForm from '@/components/common/table/tableForm.vue';
import { batchAddFileInfoApi } from '@/api/data/data';
import { uploadBigFile } from '@/utils/file';

interface Props {
  modelValue: boolean;
  data: any;
}

const props = withDefaults(defineProps<Props>(), {
  modelValue: false,
  data: {},
});

const formData = ref<any>({});

watch(
  () => props.modelValue,
  (val: boolean) => {
    diaVisible.value = val;
    if (!val) {
      tableFormRef.value?.resetFun();
    }
  }
);

const emit = defineEmits(['update:modelValue']);
const diaVisible = ref(false);

const tableFormRef = ref<any>();
const submitFun = async () => {
  const valid = await tableFormRef.value.validateFun();
  if (valid) {
    const params = {
      // 必填参数
      fileList: formData.value.files, // 文件列表
      dirId: props.data.id, // 文件目录id
      projectId: props.data.relatedResourceUuid, // 项目id
      // 非必填参数
      isApprove: 0, // 0不需要审批 1需要审批 默认0
      callbackFlag: '/data/overview', // 回调标识
      type: 0,
      // fileType: 1, // 文件类型，不传组件内获取
      // 其他透传给回调接口的参数
      // ...
    };
    await uploadBigFile(params, batchAddFileInfoApi);
    closeFun();
  }
};

const closeFun = () => {
  emit('update:modelValue', false);
};
</script>
