<template>
  <BasicModal
      v-bind="$attrs"
      @register="registerImportModal"
      :canFullscreen="false"
      :defaultFullscreen="false"
      :maskClosable="false"
      :width="800"
      title="数据表选择"
      @ok="dataBaseClick"
    >
    <BasicTable @register="registerImportTable"></BasicTable>
  </BasicModal>
 
  <BasicModal
      v-bind="$attrs"
      @register="registerModal"
      :canFullscreen="false"
      :defaultFullscreen="false"
      :maskClosable="false"
      :width="800"
      title="数据对象选择"
      @ok="modalSuReClick"
    >
    <BasicTable @register="registerDataTable"></BasicTable>
    <template #centerFooter>
      <a-button type="success" @click="handleAddForm"> 导入表 </a-button>
    </template>
    
  </BasicModal>

  

</template>
<script lang="ts" setup>
   import {  ref,defineProps,watch } from 'vue';
  import { columns , searchFormSchema,importColumns  } from './dataobject.data';
  import { BasicTable, useTable } from '@/components/Table';
  import { getDataBaseTableList,getImportBaseTableList,importDataBaseTable } from '@/api/formdesign/index';
  import { BasicModal,useModal, useModalInner } from '/@/components/Modal';
  import { message } from 'ant-design-vue';
  const emit = defineEmits(['backrows']);
  const receiceDbCode = ref()
 
  const [registerModal, { closeModal:closeBaseModal }] = useModalInner((data: any) => {
    receiceDbCode.value = data.dbCode
    clearBaseSelectedRowKeys()
  })

  const [registerImportModal, {openModal:openImportTableModal , closeModal:closeImportModal }] = useModal()


  
  const [registerDataTable, { reload,clearSelectedRowKeys:clearBaseSelectedRowKeys, getSelectRows:getBaseSelectRows }] = useTable({
    title: '',
    rowKey: 'id',
    api: getDataBaseTableList,
    formConfig: {
      labelWidth: 80,
      schemas: searchFormSchema,
    },
    columns,
    size: 'small', 
    rowSelection: {//多选框
      type: 'checkbox',
      // type: 'radio',
    },
    useSearchForm: true,
    showTableSetting: false,
    canResize: false,
    bordered: true,
    pagination:{
      pageSize: 10
    },
    beforeFetch: (data) => {
      // 接口请求前 参数处理
      var temp = {
        pageIndex: data.page,
        pageSize: data.limit,
        keyWord: data.key,
        dbCode:  receiceDbCode.value
      };
      return temp;
    },
    afterFetch: (data) => {
     
     
    }
    
  });
  const [registerImportTable, { clearSelectedRowKeys:clearImportSelectedRowKeys, getSelectRows:getImportSelectRows }] = useTable({
    title: '',
    rowKey: 'name',
    api: getImportBaseTableList,
    formConfig: {
      labelWidth: 80,
    },
    columns:importColumns,
    size: 'small', 
    rowSelection: {//多选框
      type: 'checkbox',
      // type: 'radio',
    },
    useSearchForm: false,
    showTableSetting: false,
    canResize: false,
    bordered: true,
    pagination:{
      pageSize: 10
    },
    beforeFetch: (data) => {
      var temp = {
        dbCode:  receiceDbCode.value
      };
      return temp;
    }
  })
  
  function modalSuReClick(){
    let rows = getBaseSelectRows();
    emit('backrows', rows);
    closeBaseModal()
  }

  function handleAddForm(){
    openImportTableModal(true)
    setTimeout(()=>{
      clearImportSelectedRowKeys()
    },200)
  }
  function dataBaseClick(){
    let rows = getImportSelectRows()
    console.log('rowww',rows)
    let param = {
      dbCode: receiceDbCode.value,
      tableList: rows
    }
    importDataBaseTable( param ).then((res: AreaRespVO[]) =>{
      console.log('res',)
      message.success('导入成功',2);
      closeImportModal()
      reload()
    })
  }
   

</script>
<style lang="less">
 
</style>