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

const TableList: FC = function () {
  const params = useContextSection(context, state => state[0]);
  const [{data, count}, {refetch}] = useQueryTableList({
    queryFn: getRoleList,
    params,
    pageContext,
    searchContext,
  });
  const [
    {columns, visible, editId, roleVisible, roleId, pdaId, pdaVisible},
    {onAdd, onClose, onRoleClose, onPdaClose},
  ] = useHandle(data, refetch);
  const [isExporting, onExport] = useTableExportEvent({
    pageContext,
    context,
    fn: exportRole,
  });

  return (
    <>
      <Card className='table-wrapper'>
        <TableTools
          onAdd={onAdd}
          pageContext={pageContext}
          searchContext={searchContext}
          onExport={onExport}
          isExporting={isExporting}
        />

        <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;
