<template>
  <div>
  <BasicTable @register="registerTable" >
    <template #bodyCell="{ column, record }">
          <template v-if="column.key === 'fileUploadPath'">
            <a-button type="link" v-for="(item,index) in parsePathsValue(record.fileUploadPath)" :key="index" :href="item.url" target="_blank">
              {{item.name}}
            </a-button>
          </template>
          <template v-else-if="column.key === 'checkContent'">
              <JEllipsisFormat :isLink="true" :value="record.checkContent" :length="60"  @click="goToPage(record)"/>
          </template>
          <template v-else-if="column.key === 'checkMethod'">
              <JEllipsisFormat :value="record.checkMethod" :length="60"/>
          </template>
      </template>
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" :dropDownActions="getDropDownAction(record)" />
      </template>
  </BasicTable>
  <!-- 表单区域 -->
  <StCaMainCheckModal @register="registerModal" @success="handleSuccess"></StCaMainCheckModal>
  </div>
</template>
<script lang="ts" setup>
import { defineExpose } from 'vue';
import { useRouter } from 'vue-router';
import StCaMainCheckModal from '../StCaMainCheckModal.vue';
import { useModal } from '/@/components/Modal';
import { BasicColumn, BasicTable, TableAction, useTable } from '/@/components/Table';
import { getFileAccessHttpUrl } from '/@/utils/common/compUtils';
import JEllipsisFormat from '/@/views/standardlib/components/JEllipsisFormat.vue';
import { list } from '/@/views/standardlib/controlactivity/StCaMainCheck.api';
  //注册model
  const [registerModal, {openModal}] = useModal();
  const router = useRouter()
// const props = defineProps({
//   actInfo: Map,
// });
const columns: BasicColumn[] = [
  {
    title: '检查内容',
    align:"left",
    dataIndex: 'checkContent'
   }
  ];
const [registerTable,{setProps,reload,setSelectedRowKeys}] = useTable({
        title: '',
        api: list,
        columns: columns,
        bordered: true,
        immediate: false,
        showIndexColumn: false,
        actionColumn: {
          width: 160,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
        },
        beforeFetch(params) {
            params = Object.assign(params,{ column: 'updatedTime', order: 'desc' })
         },
      });
//       watch(
//     ()=>props.actInfo,
//     (newVal,oldVla)=>{
//       console.log("metric")
//       console.log(newVal)
//       let parm = {caNo:newVal.caNo}
//       setProps({searchInfo:parm});
//       reload()
//       console.log(oldVla)
//       console.log("metric")
//     }
// )
function handleSuccess() {
  setSelectedRowKeys.bind(null,[]) && reload();
}
function goToPage(record) {
    router.push({path: '/standardlib/controlactivity/StCaMainCheckList',query:{bizNo: record.id}})
  }
function getFileName(path) {
    if (path.lastIndexOf('\\') >= 0) {
      let reg = new RegExp('\\\\', 'g');
      path = path.replace(reg, '/');
    }
    return path.substring(path.lastIndexOf('/') + 1);
  }
  
  function uidGenerator() {
    return '-' + parseInt(Math.random() * 10000 + 1, 10);
  }
// 解析数据库存储的逗号分割
function parsePathsValue(paths) {
  console.log(paths)
    if (!paths || paths.length == 0) {
      return [];
    }
    let list: any[] = [];
    for (const item of paths.split(',')) {
      let url = getFileAccessHttpUrl(item);
      list.push({
        uid: uidGenerator(),
        name: getFileName(item),
        status: 'done',
        url: url,
        response: { status: 'history', message: item },
      });
    }
    return list;
  }
  function handleDetail(record: Recordable) {
     openModal(true, {
       record,
       isUpdate: true,
       showFooter: false,
     });
   }
  function getTableAction(record){
       return [
         {
           label: '详情',
           onClick: handleDetail.bind(null, record),
         }, 
       ]
   }
    /**
        * 下拉操作栏
        */
  function getDropDownAction(record){
       return [
       ]
   }
  defineExpose({setProps,reload})
</script>