<template>
  <div class="st_coolection_cont">
    <div class="st_coolection">
    <a-form>
      <a-button type="link" @click="returnJh">{{collectionInfo.collectionNo}}</a-button>
     {{ collectionInfo.collectionName }}
      <a-button type="link" @click="returnUpper">上一级</a-button>
    </a-form>
    <!--引用表格-->
   <BasicTable @register="registerTable" :rowSelection="rowSelection">
     <!--插槽:table标题-->
      <template #tableTitle>
          <a-button type="primary" @click="handleAdd" preIcon="ant-design:plus-outlined"> 新建</a-button>
          <a-button @click="batchHandleDelete" preIcon="ant-design:minus-outlined"> 移除</a-button>
          <a-button v-show="showUpBtn" type="primary" preIcon="ant-design:export-outlined" @click="onExportXls"> 导出</a-button>
          <j-upload-button v-show="showUpBtn" type="primary" preIcon="ant-design:import-outlined" @click="onImportXls">导入</j-upload-button>
          <a-dropdown v-if="selectedRowKeys.length > 0 && showUpBtn">
              <template #overlay>
                <a-menu>
                  <a-menu-item key="1" @click="batchHandleDelete">
                    <Icon icon="ant-design:delete-outlined"></Icon>
                    删除
                  </a-menu-item>
                </a-menu>
              </template>
              <a-button>批量操作
                <Icon icon="mdi:chevron-down"></Icon>
              </a-button>
        </a-dropdown>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'caName'">
          <JEllipsisFormat :isLink="true"  :value="record.caName" :length="60" @click="goToCa(record)"/>
        </template>
          <template v-else-if="column.key === 'lableNameList'">
            <JTagsFormat :value="record.lableNameList" :length="4" :strLen="14" />
          </template>
      </template>
       <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" :dropDownActions="getDropDownAction(record)"/>
      </template>
      <!--字段回显插槽-->
      <template #htmlSlot="{text}">
         <div v-html="text"></div>
      </template>
      <!--省市区字段回显插槽-->
      <template #pcaSlot="{text}">
         {{ getAreaTextByCode(text) }}
      </template>
      <template #fileSlot="{text}">
         <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
         <a-button v-else :ghost="true" type="primary" preIcon="ant-design:download-outlined" size="small" @click="downloadFile(text)">下载</a-button>
      </template>
    </BasicTable>
    <div class="table-col-detail" v-show="otherParam.tbshow">
       <StCaMainTabs :actInfo="otherParam.record" ref="tabsRef" />
    </div>
    <!-- 表单区域 -->
    <StCaMainRelCollectionModal @register="registerModal" @success="handleSuccess"></StCaMainRelCollectionModal>
  </div>
</div>
</template>

<script lang="ts" name="act.collection-stCaMainRelCollection" setup>
  import { message } from 'ant-design-vue';
import { reactive, ref, watch } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import JTagsFormat from '../components/JTagsFormat.vue';
import { batchDelete, deleteOne, getExportUrl, getImportUrl, list } from './StCaMainRelCollection.api';
import { columns } from './StCaMainRelCollection.data';
import StCaMainRelCollectionModal from './components/StCaMainRelCollectionModal.vue';
import StCaMainTabs from './components/StCaMainTabs.vue';
import { useModal } from '/@/components/Modal';
import { BasicTable, TableAction } from '/@/components/Table';
import { useListPage } from '/@/hooks/system/useListPage';
import { downloadFile } from '/@/utils/common/renderUtils';
import JEllipsisFormat from '/@/views/standardlib/components/JEllipsisFormat.vue';
const route = useRoute();
const router = useRouter()
const props = defineProps({
  collectionInfo: {}
});
const otherParam = reactive({
  id: null,
  tbshow: false,
  record: {}
})
const tabsRef = ref(null)
const emit = defineEmits(['callBack']);
  const showUpBtn = ref(false)
  const checkedKeys = ref<Array<string | number>>([]);
  //注册model
  const [registerModal, {openModal}] = useModal();
  //注册table数据
  const { prefixCls,tableContext,onExportXls,onImportXls } = useListPage({
      tableProps:{
           title: 'st_ca_main_rel_collection',
           api: list,
           columns,
           canResize:false,
           useSearchForm: false,
           immediate: false,
           actionColumn: {ifShow: true},
            showTableSetting: false,
            beforeFetch(params) {
              params = Object.assign(params,{ column: 'updatedTime', order: 'desc'})
            },
            customRow: (record, _index) =>{
              return {
                onClick: (event) => {
                    if(event.target.localName!="tr" && event.target.parentNode.localName!="tr"){
                      return
                    }
                    // 清楚所有样式
                    let oldList = document.querySelectorAll(".ant-table-row-selected")
                    if(oldList){
                        for (let j = 0; j < oldList.length; j++) {
                            oldList[j].classList.remove("ant-table-row-selected")
                        }
                    }
                    // 如果是true 并且 id相同就影藏下方管理的信息
                    if(otherParam.tbshow && record.id ===  otherParam.id){
                      otherParam.tbshow = false
                      return 
                    }
                    otherParam.tbshow = true
                    otherParam.id = record.id
                    otherParam.record = record
                    let node = event.target;
                    if(node.localName!="tr"){
                      node = event.target.parentNode
                    }
                    //console.log(node.getAttribute('data-row-key'))
                    node.classList.add("ant-table-row-selected")
                    tabsRef.value?.initActiveKey()
                },
              };
            }
      },
       exportConfig: {
            name:"st_ca_main_rel_collection",
            url: getExportUrl,
          },
          importConfig: {
            url: getImportUrl,
            success: handleSuccess
          },
  })

  const [registerTable, {reload,setProps},{ rowSelection, selectedRowKeys }] = tableContext
  function getTagColor(index){
    let colorArr = ['red','orange','green','pink','cyan','blue','purple']
    let i = index%colorArr.length
    return colorArr[i]
  }
  watch(
    ()=>props.collectionInfo,
    (newVal,oldVla)=>{
    setProps({searchInfo:{collectionNo:newVal.collectionNo}});
    reload()
  })
   /**
    * 新增事件
    */
  function handleAdd() {
     openModal(true, {
       collectionNo: props.collectionInfo.collectionNo,
       isUpdate: false,
       showFooter: true,
     });
  }
  function goToCa(record){
  if(record.caName=="-"){
    return
  }
  router.push({path: '/standardlib/controlactivity/StCaMainList',query:{bizNo: record.caNo}})
}
   /**
    * 编辑事件
    */
  function handleEdit(record: Recordable) {
     openModal(true, {
       record,
       isUpdate: true,
       showFooter: true,
     });
   }
   /**
    * 详情
   */
  function handleDetail(record: Recordable) {
     openModal(true, {
       record,
       isUpdate: true,
       showFooter: false,
     });
   }
   /**
    * 删除事件
    */
  async function handleDelete(record) {
     await deleteOne({id: record.id}, handleSuccess);
   }
   /**
    * 批量删除事件
    */
  async function batchHandleDelete() {
     if(selectedRowKeys.value==null||selectedRowKeys.value.length==0){
      message.error("请选择需要移除的信息！")
      return;
     }
     await batchDelete({ids: selectedRowKeys.value}, handleSuccess);
   }
   /**
    * 成功回调
    */
  function handleSuccess() {
      (selectedRowKeys.value = []) && reload();
   }
   function returnJh() {
    emit('callBack',props.collectionInfo)
   }
   function returnUpper() {
    emit('callBack')
   }
   /**
      * 操作栏
      */
  function getTableAction(record){
       return [
        //  {
        //    label: '修改',
        //    onClick: handleEdit.bind(null, record),
        //  }
        {
           label: '详情',
           onClick: handleDetail.bind(null, record),
         }
       ]
   }
     /**
        * 下拉操作栏
        */
  function getDropDownAction(record){
       return [
        //  {
        //    label: '详情',
        //    onClick: handleDetail.bind(null, record),
        //  }, {
        //    label: '删除',
        //    popConfirm: {
        //      title: '是否确认删除',
        //      confirm: handleDelete.bind(null, record),
        //    }
        //  }
       ]
   }


</script>

<style scoped>
.st_coolection_cont {
  padding: 10px;
}
.st_coolection {
  padding: 12px 10px 6px 10px;
  margin-bottom: 8px;
  background-color: #fff;
  border-radius: 2px;
}
.table-col-detail {
    border-top: 10px solid #f0f2f5;
    background-color: #ffffff;
    padding-left: 20px;
    padding-right: 20px;
  }
</style>