<template>
  <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 === 'scenarioName'">
              <JEllipsisFormat  :isLink="true" :value="record.scenarioName" :length="60" @click="goToPage(record)"/>
          </template>
          <template v-else-if="column.key === 'riskEventName'">
              <JEllipsisFormat  :isLink="true" :value="record.riskEventName" :length="18" @click="goRiskEventDetail(record)"/>
          </template>
          <template v-else-if="column.key === 'problemExample'">
              <JEllipsisFormat :value="record.problemExample" :length="60"/>
          </template>
    </template>
  </BasicTable>
  <StRiskEventModalDetail @register="registerModalDetail"></StRiskEventModalDetail>
</template>
<script lang="ts" setup>
import { defineExpose } from 'vue';
import { BasicColumn, BasicTable, useTable } from '/@/components/Table';
import { getFileAccessHttpUrl } from '/@/utils/common/compUtils';
import JEllipsisFormat from '/@/views/standardlib/components/JEllipsisFormat.vue';
import { list } from '/@/views/standardlib/risk/StRiskEvent.api';
import { useRouter } from 'vue-router';
import StRiskEventModalDetail from '/@/views/standardlib/risk/components/StRiskEventModalDetail.vue';
import { useModal } from '/@/components/Modal';
const router = useRouter()
const [registerModalDetail, {openModal: openModalDetail}] = useModal();

const columns: BasicColumn[] = [
{
    title: '场景编号',
    align:"left",
    dataIndex: 'scenarioNo'
   },
   {
    title: '风险场景',
    align:"left",
    dataIndex: 'scenarioName'
   },
   {
    title: '风险事件',
    align:"left",
    dataIndex: 'riskEventName'
   },
   {
    title: '风险事件描述',
    align:"left",
    dataIndex: 'riskEventDesc'
   },
   {
    title: '适用对象',
    align:"left",
    dataIndex: 'applicableName'
   },
  ];
const [registerTable,{setProps,reload}] = useTable({
        title: '',
        api: list,
        columns: columns,
        bordered: true,
        immediate: false,
        showIndexColumn: false,
        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 goToPage(record){
  router.push({path: '/standardlib/risk/StRiskEventList',query:{bizNo: record.riskEventNo}})
  }
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 goRiskEventDetail(record: Recordable) {
    openModalDetail(true, {
      record,
      isUpdate: false,
      showFooter: false,
    });
  }

  defineExpose({setProps,reload})
</script>