<template>
  <div class="standard-page">
    <BaseTable
      ref="baseTableRef"
      table-name="SIMULATION_KNOWLEDGE"
      :hide-pagination="true"
      :full-height="true"
      :action-list="actionList"
    >
      <template #fileSize="{ row }">
        <span>{{ formatFileSize(row.fileSize) }}</span>
      </template>
    </BaseTable>
    <FilePreview v-model="previewVisible" :fileId="currentRow?.id" />
  </div>
</template>

<script setup lang="ts">
import { nextTick, ref, watch } from 'vue';
import BaseTable from '@/components/common/table/baseTable.vue';
import { getFileBaseInfoApi } from '@/api/data/data';
import { useI18n } from 'vue-i18n';
import { downloadFileById, formatFileSize } from '@/utils/file';
import FilePreview from '@/components/common/filePreview/index.vue';

const props = defineProps({
  runInfo: {
    type: Object,
    default: () => {},
  },
});

const baseTableRef = ref();
const { t } = useI18n();

const tableData = ref<any>([]);
const getFileBaseInfoFun = async (data: any) => {
  const list = data.standard.replaceAll(';', ',').split(',');
  tableData.value = [];
  for (let i = 0; i < list.length; i++) {
    const res: any = await getFileBaseInfoApi({ fileId: list[i] });
    if (res && res.code === 200) {
      if (res.data.dataType === 2) {
        tableData.value.push(res.data);
      }
    }
  }
};

const actionList = ref([
  {
    title: t('通用.预览'),
    type: 'primary',
    click: (row: any) => {
      previewFileFun(row);
    },
    hide: (row: any) => {
      return row.dataType !== 2;
    },
  },
  {
    title: t('通用.下载'),
    type: 'primary',
    click: (row: any) => {
      downloadFileById(row.id);
    },
    hide: (row: any) => {
      return row.dataType !== 2;
    },
  },
]);

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

watch(
  () => props.runInfo,
  async (newVal) => {
    if (newVal && newVal?.standard) {
      await getFileBaseInfoFun(newVal);

      nextTick(() => {
        baseTableRef.value.setDataFun(tableData.value);
      });
    }
  },
  {
    deep: true,
    immediate: true,
  }
);
</script>

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