<template>
  <approvalTypeHeader :data="data" />
  <BaseTable
    ref="baseTableRef"
    :tableName="TABLE_NAME.SIMULATION_KNOWLEDGE_APPROVE_PREVIEW"
    showIndex
    :actionsWidth="200"
    :actionList="actionList"
  >
    <template #originalName="{ row }">
      <el-icon v-if="row.dataType === 1">
        <Folder />
      </el-icon>
      <el-icon v-else>
        <Document />
      </el-icon>
      {{ row.originalName }}
    </template>
    <template #fileSize="{ row }">
      {{ formatFileSize(row.fileSize) }}
    </template>
  </BaseTable>
  <FilePreview v-model="previewVisible" :fileId="currentRow?.id" />
</template>

<script setup lang="ts">
import { computed, ref, watchEffect } from 'vue';
import BaseTable from '@/components/common/table/baseTable.vue';
import approvalTypeHeader from './approvalTypeHeader.vue';
import i18n from '@/utils/i18n';
import { formatFileSize } from '@/utils/file';
import FilePreview from '@/components/common/filePreview/index.vue';
import { TABLE_NAME } from '@/utils/enum/tableName';
import { downloadFileById } from '@/utils/file';

interface Props {
  data: any;
}

const props = withDefaults(defineProps<Props>(), {
  data: {},
});
const knowledgeList = ref<any[]>([]);
const baseTableRef = ref<any>(null);
watchEffect(() => {
  if (props.data) {
    const approveContents = JSON.parse(props.data?.approveContents || '{}');
    knowledgeList.value = approveContents.afterData || approveContents.beforeData || null;
    if (baseTableRef.value && knowledgeList.value) {
      baseTableRef.value?.setDataFun(knowledgeList.value);
    }
  }
});
const currentRow = ref<any>(null);
const previewVisible = ref<boolean>(false);
const previewFileFun = (row: any) => {
  currentRow.value = row;
  previewVisible.value = true;
};

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

});
</script>
