<template>
  <Dialog
    v-model="visible"
    diaTitle="文件选择"
    :width="'70%'"
    :height="'50%'"
    :zIndex="100"
    @close="handleCloseFun"
    show-footer
  >
    <div class="file-page">
      <BaseTable
        ref="FileTableRef"
        tableName="RUN_RESULT_FILE_TABLE"
        :full-height="true"
        :show-checkbox="true"
        :row-config="{ keyField: 'objectKey' }"
        :api="queryRunDirApi"
        :params="{
          fileId: dirId,
        }"
      >
        <template #fileSize="{ row }">
          <span>{{ formatFileSize(row.fileSize) }}</span>
        </template>
      </BaseTable>
    </div>
    <template #footer>
      <el-button type="" @click="handleCloseFun">关闭</el-button>
      <el-button type="primary" @click="submitFun">确认</el-button>
    </template>
  </Dialog>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import Dialog from '@/components/common/dialog/index.vue';
import BaseTable from '@/components/common/table/baseTable.vue';
// import { previewNodeInputFilesApi } from '@/api/flowable/process';
import { formatFileSize } from '@/utils/file';
import { queryRunDirApi } from '@/api/project/run';

defineProps({
  files: {
    type: Array,
    default: () => [],
  },
  onlineFileParam: {
    type: Object,
    default: () => {},
  },
  dirId: {
    type: Number,
    default: 0,
  },
});

const emits = defineEmits(['close', 'update']);

const visible = ref(true);
const FileTableRef = ref();
const handleCloseFun = () => {
  emits('close');
};

// const setTableCheckedFun = async (param: any) => {
//   const list = props.files || [];
//   try {
//     const res: any = await previewNodeInputFilesApi(param);
//     if (res && res.code === 200) {
//       const tableData = res.data.map((item: any) => {
//         return {
//           ...item,
//           originalName: item.fileName,
//           objectKey: item.filePath.replace('/home/simulation/', ''),
//           updateTime: dayjs(new Date(Number(item.createTime))).format('YYYY-MM-DD HH:mm:ss'),
//           fileSize: Number(item.fileSize) ? item.fileSize : 1024,
//         };
//       });
//       FileTableRef.value.setDataFun(tableData);
//       FileTableRef.value.tableRef.setCheckboxRow(list, true);
//     }
//   } catch {
//   }
// };

const submitFun = () => {
  const checkFiles = FileTableRef.value.tableRef.getCheckboxRecords();
  const paths = checkFiles.map((item: any) => {
    return item.objectKey;
  });
  emits('update', paths);
};

// watch(
//   () => props.onlineFileParam,
//   async (newVal) => {
//     if (newVal) {
//       const param = newVal;
//       await setTableCheckedFun(param);
//     }
//   },
//   {
//     immediate: true,
//     deep: true,
//   }
// );

// watch(
//   () => props.dirId,
//   async (newVal) => {
//     if (newVal) {
//       const inputDirId = newVal;
//       await setTableCheckedFun({ current: 1, fileId: inputDirId, size: 9999 });
//     }
//   },
//   {
//     immediate: true,
//     deep: true,
//   }
// );
</script>

<style lang="scss" scoped>
.file-page {
  width: 100%;
  height: 100%;
}
</style>
