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

<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"
    >
      <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>
    </BaseTable>
  </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 { ElMessage } from 'element-plus';

const env = import.meta.env;

const props = defineProps({
  nodeInfo: {
    type: Object,
    default: () => {},
  },
  fileId: {
    type: String,
    default: '',
  },
});

const baseTableRef = ref();
const beforeUploadFun = async (file:any) => {

  if (!props.fileId) {
    return;
  }

  const { name } = file;

  const param = {
    file,
    dirId: props.fileId,
    fileName: name,
    fileType: 1,
  };

  await uploadRunFilesApi(param).then(async (res:any) => {
    if (res.code === 200) {
      ElMessage.success('上传成功');
      await reloadFun();
    }
  }).finally(() => {

  });

};

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

const downLoadFileFun = async () => {

  const ckeckData = baseTableRef.value.tableRef.getCheckboxRecords();

  for (let i = 0;i < ckeckData.length;i++) {

    const downloadUrl = `${env.VITE_API_FILE_URL}/data/downloadFile?fileId=${ckeckData[i].id}`;
    window.open(downloadUrl, '_blank');

  }

  console.log(ckeckData, 'ckeckData');

};

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

  }
};

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>
