<template>
  <div>
      <BasicTable @register="registerResourceTable">
          <template #action="{ record }">
              <TableAction :actions="getTableAction(record)" />
          </template>
      </BasicTable>
  </div>
</template>

<script lang="ts" name="UploadfileDetail" setup>
//ts语法
import { defineProps, onMounted } from 'vue';
import { BasicTable, ActionItem, TableAction, useTable } from '@/components/Table';
import { PlaninfoFiletableColumns } from '@/views/projectLib/projectPlan/projectPlan.data';
import { planFilePageList ,getplaninfoByid } from '@/views/projectLib/projectPlan/projectPlan.api'
import { BasicColumn } from '@/components/Table';
import { downloadFile,downloadResource,downloadonlinePreview } from "@/api/common/api"

let dataTo = defineProps(["stage", "planinfoid"])
//我的待办表格组件
//项目资料table
const [registerResourceTable,{setColumns}] = useTable({
  size: 'small',//紧凑型表格
  api: planFilePageList,
  columns: PlaninfoFiletableColumns,
  useSearchForm: false,
  actionColumn: {
      width: 140,
      title: '操作',
      dataIndex: 'action',
      slots: { customRender: 'action' },
  },
  beforeFetch(params) {
      params.stage = dataTo.stage
      params.planinfoid = dataTo.planinfoid
  },
});
onMounted(async()=>{
 let details = await getplaninfoByid({planinfoid:dataTo.planinfoid})
 console.log("sdsdadad",details)
 if(details[0].taskName=="可研报告送审稿编制"){
  setColumns(PlaninfoFiletableColumns)
 }else{
  setColumns(PlaninfoFiletableColumns12)
 }
})
function getTableAction(record): ActionItem[] {
  return [
      {
          label: '下载',
          ifShow: true,
          onClick: handleDetailpage.bind(null, record),
      },
      {
          label: '预览',
          ifShow: true,
          onClick: handlePreview.bind(null, record),
      }
  ];
}
function handlePreview(record) {
  console.log("我这一行的数据是", record)
  let param = {
      path: record.documentPath,
      fileName: record.documentName
  }
  //
  console.log("我这一行的数据是", param)

  // downloadFile("/huzhouUploadfileinfo/downloadfile", record.documentName, param)
  downloadonlinePreview("/huzhouUploadfileinfo/downloadfile", record.documentName,param)
}
function handleDetailpage(record) {
  console.log("我这一行的数据是", record)
  let param = {
      path: record.documentPath,
      fileName: record.documentName
  }
  //
  console.log("我这一行的数据是", param)

  // downloadFile("/huzhouUploadfileinfo/downloadfile", record.documentName, param)
  downloadResource("/huzhouUploadfileinfo/downloadfile", record.documentName,param)
}
const PlaninfoFiletableColumns12: BasicColumn[] = [
  {
    title: '文件名称',
    dataIndex: 'documentName',
  },
  {
    title: '文件大小',
    dataIndex: 'size',
    width: 100,
    customRender: ({ record }) => {
      let kbNum: number = record.size / 1024
      if (kbNum < 1024) {
        return kbNum.toFixed(2).toString() + "KB"
      } else {
        let mbNum: number = kbNum / 1024
        return mbNum.toFixed(2).toString() + "MB"
      }

    },
  },
  {
    title: '上传时间',
    dataIndex: 'createDate',
  },
  {
    title: '处理人',
    dataIndex: 'createUser',
  },
  {
    title: '文件状态',
    dataIndex: 'status',
    customRender: ({ record }) => {
      if (record.status == 1) {
        return "有效"
      }
      else {
        return "历史文件" //0
      }
    },
  }
]
</script>
<style scoped></style>