<template>
  <BasicTable @register="registerTable" >
    <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'fileName'">
          <JEllipsisFormat :isLink="true"  :value="record.fileName" :length="60" @click="goToPage(record)"/>
        </template>
        <template v-if="column.key === 'complianeItem'">
          <JEllipsisFormat :isLink="true"  :value="record.complianeItem" :length="60" @click="goToPageDetail(record)"/>
        </template>
        <template v-else-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>
  </BasicTable>
  <StComplianeItemModal @register="registerModal"></StComplianeItemModal>
</template>
<script lang="ts" setup>
import { watch,defineExpose,ref } from 'vue';
import { BasicColumn, BasicTable, useTable } from '/@/components/Table';
import { getFileAccessHttpUrl } from '/@/utils/common/compUtils';
import { listca } from '/@/views/standardlib/compliane/StComplianeItem.api';
import JEllipsisFormat from '/@/views/standardlib/components/JEllipsisFormat.vue';
import { useRouter } from 'vue-router';
import { useModal } from '/@/components/Modal';
import StComplianeItemModal from '/@/views/standardlib/compliane/components/StComplianeItemModal.vue';

const router = useRouter()
const [registerModal, {openModal}] = useModal();

const columns: BasicColumn[] = [
{
    title: '文件编号',
    align:"left",
    dataIndex: 'complianeNo',
   },
   {
    title: '文件名称',
    align:"left",
    dataIndex: 'fileName'
   },
   {
    title: '合规条目',
    align:"left",
    dataIndex: 'complianeItem'
   },
   {
    title: '合规内容',
    align:"left",
    dataIndex: 'complianeAsk'
   },
   {
    title: '适用对象',
    align:"left",
    dataIndex: 'applicableName'
   },
   {
    title: '附件',
    align:"left",
    dataIndex: 'fileUploadPath'
   },
  ];
 
const caName = ref("")
  
const [registerTable,{setProps,reload}] = useTable({
        title: '',
        api: listca,
        columns: columns,
        bordered: true,
        showIndexColumn: false,
        immediate: false,
        beforeFetch(params) {
            caName.value = params.caName 
            params.caName = ""
            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 goToPage(record) {
  if(record.complianeNo.indexOf("FL")==0){
    router.push({path: '/standardlib/compliane/fl',query:{bizNo: record.complianeNo}})
  }else if(record.complianeNo.indexOf("FG")==0) {
    router.push({path: '/standardlib/compliane/fg',query:{bizNo: record.complianeNo}})
  }else if(record.complianeNo.indexOf("JG")==0){
    router.push({path: '/standardlib/compliane/jg',query:{bizNo: record.complianeNo}})
  }else if(record.complianeNo.indexOf("FX")==0){
    router.push({path: '/standardlib/compliane/fx',query:{bizNo: record.complianeNo}})
  }else if(record.complianeNo.indexOf("CF")==0){
    router.push({path: '/standardlib/compliane/cf',query:{bizNo: record.complianeNo}})
  }
}

function goToPageDetail(record: Recordable) {
     record["caName"] = caName.value
     openModal(true, {
       record,
       isUpdate: true,
       showFooter: false,
     });
}

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;
  }
  defineExpose({setProps,reload})
</script>