import {Card} from 'antd';
import {FC} from 'react';
import {useHandle, useList} from './hooks';
import {pageContext, searchContext} from '../context';
import RoleModal from './modal';
import TreeModal from './tree-modal';
import {Table, TableTools} from '@components';
import PdaMenuModal from './pda-modal';

const TableList: FC = function() {
  const [{data, count}, refetch] = useList();
  const [
    {columns, visible, editId, roleVisible, roleId, pdaId, pdaVisible},
    {onAdd, onClose, onRoleClose, onPdaClose},
  ] = useHandle(data, refetch);

  return (
    <>
      <Card className='table-wrapper'>
        <TableTools onClick={onAdd} />

        <Table
          data-testid='role_table'
          columns={columns}
          data={data}
          pageContext={pageContext}
          searchContext={searchContext}
          count={count}
        />
      </Card>

      <RoleModal visible={visible} onClose={onClose} onFetch={refetch} id={editId} />
      <TreeModal visible={roleVisible} onClose={onRoleClose} onFetch={refetch} id={roleId} />
      <PdaMenuModal visible={pdaVisible} onClose={onPdaClose} onFetch={refetch} id={pdaId} />
    </>
  );
};

export default TableList;
