<template>
  <div>
    <a-button type="primary" @click="handleAdd">添加指标</a-button>
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'metricName'">
          <JEllipsisFormat :isLink="true"  :value="record.metricName" :length="60" @click="goToPage(record)"/>
        </template>
      </template>
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" :dropDownActions="getDropDownAction(record)"/>
      </template>
      </BasicTable>
    <StMetricModalSelect @register="registerModal" @success="handleSuccess"></StMetricModalSelect>
  </div>
</template>
<script lang="ts" setup>
import { watch } from 'vue';
import StMetricModalSelect from '../StMetricModalSelect.vue';
import { useModal } from '/@/components/Modal';
import { BasicTable, TableAction } from '/@/components/Table';
import { useListPage } from '/@/hooks/system/useListPage';
import { deleteOneRef, list } from '/@/views/standardlib/controlactivity/StMetric.api';
import { columns } from '/@/views/standardlib/controlactivity/StMetric.data';
import JEllipsisFormat from '/@/views/standardlib/components/JEllipsisFormat.vue';
import { useRouter } from 'vue-router';
const props = defineProps({
  actInfo: Map,
});
const router = useRouter()
//注册model
const [registerModal, {openModal}] = useModal();
  //注册table数据
  const { prefixCls,tableContext} = useListPage({
      tableProps:{
           title: '',
           api: list,
           columns,
           canResize:false,
           showTableSetting: false,
           useSearchForm: false,
           immediate: false,
           actionColumn: {
               width: 200,
               fixed:'right',
            },
            beforeFetch(params) {
                params = Object.assign(params,{ column: 'updatedTime', order: 'desc' })
            },
      },
  })
  const [registerTable, {setProps,reload},{ rowSelection, selectedRowKeys }] = tableContext
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 handleAdd() {
     openModal(true, {
       caNo: props.actInfo.caNo,
       isUpdate: false,
       showFooter: true,
     });
  }
  function goToPage(record){
    router.push({path: '/standardlib/controlactivity/StMetricList',query:{bizNo: record.metricNo}})
  }
   /**
    * 编辑事件
    */
  function handleEdit(record: Recordable) {
     openModal(true, {
       record,
       isUpdate: true,
       showFooter: true,
     });
   }
   /**
    * 详情
   */
  function handleDetail(record: Recordable) {
     openModal(true, {
       record,
       isUpdate: true,
       showFooter: false,
     });
   }
      /**
    * 成功回调
    */
  function handleSuccess() {
      (selectedRowKeys.value = []) && reload();
   }
   /**
    * 删除事件
    */
  async function handleDelete(record) {
     await deleteOneRef({id: record.id,caNo: props.actInfo.caNo}, handleSuccess);
   }
  
/**
      * 操作栏
      */
      function getTableAction(record){
       return [
        {
           label: '删除',
           color: 'error',
           popConfirm: {
             title: '是否确认删除',
             confirm: handleDelete.bind(null, record),
           }
         },
       ]
   }
     /**
        * 下拉操作栏
        */
  function getDropDownAction(record){
       return [
       ]
   }
</script>