<!-- 节点输入文件 -->
<!-- 节点输出文件 -->

<template>
  <div class="node-file-page">
    <BaseTable
      showIndex
      ref="baseTableRef"
      tableName="RUN_RESULT_FILE_TABLE"
      :api="queryRunDirFun"
      :params="{
        fileId: fileId,
        // uuid:nodeInfo.uuid
      }"
      :show-checkbox="true"
      :action-list="actionList"
      :full-height="true"
    >
      <template #leftOptions>
        <el-upload :show-file-list="false" :before-upload="beforeUploadFun">
          <el-button>上传文件</el-button>
        </el-upload>
        <el-button type="primary" class="ml10" @click="downLoadFileFun">下载文件</el-button>
      </template>
      <template #fileSize="{ row }">
        <span>{{ formatFileSize(row.fileSize) }}</span>
      </template>
    </BaseTable>
    <FilePreview v-model="previewVisible" :fileId="currentRow?.id" />
  </div>
</template>

<script setup lang="ts">
import { ref, watch } from 'vue';
import BaseTable from '@/components/common/table/baseTable.vue';
import { queryRunDirApi, uploadRunFilesApi } from '@/api/project/run';
import { downloadFileById, downloadFileByStream, fileUploadAllocationTypeFun } from '@/utils/file';
import FilePreview from '@/components/common/filePreview/index.vue';
import { formatFileSize } from '@/utils/file';
import emitter from '@/utils/eventBus';
import { dataDelFileApi } from '@/api/data/data';
import { ElMessage } from 'element-plus';

const props = defineProps({
  nodeInfo: {
    type: Object,
    default: () => {},
  },
  fileId: {
    type: String,
    default: '',
  },
});
const baseTableRef = ref();
const beforeUploadFun = async (file: any) => {
  if (!props.fileId) {
    return;
  }

  const list = [file];

  const sourceFiles = [
    {
      fileName: file.name,
      size: file.size,
      fileType: fileUploadAllocationTypeFun(file.name),
    },
  ];

  const param = {
    sourceFiles,
    uploadTaskId: new Date().getTime(),
    dirId: props.fileId,
  };

  const res: any = await uploadRunFilesApi(param);
  if (res && res.code === 200) {
    res.data.forEach((item: any, index: any) => {
      item.isSaveLocal = 'Y';
      emitter.emit('ADD_UPLOAD_FILE', {
        file: list[index],
        data: {
          // 接口返回的文件目录信息，包括配置信息
          ...item,
          isApprove: 0, // 0否 1是
          taskType: 4, // 4交付物
        },
      });
    });
  }

  if (res.code === 200) {
    reloadFun();
  }
};

const queryRunDirFun = async (param: any) => {
  if (param.fileId) {
    const res: any = await queryRunDirApi(param);
    return res;
  } else {
    return {};
  }
};

const downLoadFileFun = async () => {
  const ckeckData = baseTableRef.value.tableRef.getCheckboxRecords();
  for (let i = 0; i < ckeckData.length; i++) {
    downloadFileByStream(ckeckData[i].id);
  }
};

const reloadFun = async () => {
  if (baseTableRef.value) {
    await baseTableRef.value.resetFun();
  }
};

const currentRow = ref();
const previewVisible = ref(false);
const previewFileFun = (row: any) => {
  currentRow.value = row;
  previewVisible.value = true;
};

const actionList = ref([
  {
    title: '下载',
    type: 'primary',
    click: (row: any) => {
      downloadFileById(row.id);
    },
  },
  {
    title: '预览',
    type: 'primary',
    click: (row: any) => {
      previewFileFun(row);
    },
  },
  {
    title: '删除',
    type: 'danger',
    click: async (row: any) => {
      const res: any = await dataDelFileApi({ delFileId: row.id });
      if (res && res.code === 200) {
        ElMessage.success('删除成功');
        reloadFun();
      }
    },
  },
]);

watch(
  () => props.nodeInfo,
  (newVal) => {
    if (newVal) {
    }
  },
  {
    immediate: true,
    deep: true,
  }
);
</script>

<style lang="scss" scoped>
.node-file-page {
  width: 100%;
  height: 100%;
  padding: 10px;

  .ml10 {
    margin-left: 10px;
  }
}
</style>
