<template>
    <BasicTable @register="registerTable" :rowSelection="rowSelection" @selection-change="handlerRowClick">
        <!-- update-begin-author:taoyan date:2022-5-25 for: VUEN-1112一对多 用户选择 未显示选择条数，及清空 -->
        <template #tableTitle></template>
        <!-- update-end-author:taoyan date:2022-5-25 for: VUEN-1112一对多 用户选择 未显示选择条数，及清空 -->
    </BasicTable>
</template>
<script setup>
import { toRaw, watch } from 'vue';
import { listDuty } from '../../StProjectConfig.api';
import { BasicTable } from '/@/components/Table';
import { useListPage } from '/@/hooks/system/useListPage';

const props = defineProps({
    actCheckbox: {},
    depIds: []
});
const columns = [
   {
    title: '部门',
    align:"left",
    dataIndex: 'departName'
   },
   {
    title: '岗位',
    align:"left",
    dataIndex: 'postName'
   },
   {
    title: '人员',
    align:"left",
    dataIndex: 'uname'
   },
];
const { prefixCls,tableContext,onExportXls,onImportXls } = useListPage({
      tableProps:{
           title: '',
           api: listDuty,
           columns,
           canResize:false,
           immediate: false,
           useSearchForm: false,
           scroll: { x: false,y:250 },
           actionColumn: {ifShow: false},
           clickToRowSelect: true,
           rowKey: 'code',
           rowSelection: { 
                onSelect: handlerChecked,
            },
        //    actionColumn: {
        //        width: 160
        //     },
            showTableSetting: false,
      },
  })
  const [registerTable, {setProps,reload},{ rowSelection, selectedRowKeys }] = tableContext
  watch(()=>props.depIds,(nval,oval)=>{
    searchByDepIds(nval)
  })
  function handlerChecked(record, selected, selectedRows, nativeEvent){
    if(selected){
        let item = {}
        item[record.code] = true
        Object.assign(props.actCheckbox,item)
    }else{
        let item = {}
        item[record.code] = false
        Object.assign(props.actCheckbox,item)
    }
  }
  function initActCheckbox(){
    let actInfo = toRaw(props.actCheckbox)
    let item = {}
    for(let k in actInfo){
        if(!k){
            continue
        }
        item[k] = false
    }
    Object.assign(props.actCheckbox,item)
  }
  function handlerRowClick({keys,rows}){
    console.log(rows)
    initActCheckbox()
    if(keys.length>0){
        let item = {}
        for(var i=0;i<keys.length;i++){
            item[rows[i].code] = true
        }
        Object.assign(props.actCheckbox,item)
    }
  }
  function searchByDepIds(ids){
    // console.log('-调用-')
    // console.log(ids)
    setProps({searchInfo:{depIds:ids.join(",")}});
    reload()
  }
  defineExpose({searchByDepIds})
</script>