import {
  MyButtons,
  MyColumns,
  MyPageContainer,
  MyProTableProps,
} from '@/common';
import { Apis } from '@/gen/Apis';
import { LeftOutlined } from '@ant-design/icons';
import { ProTable } from '@ant-design/pro-components';
import { useNavigate, useSearchParams } from '@umijs/max';
import { Space } from 'antd';
import Create from './modals/Create';
import EditPermissions from './modals/EditPermissions';
import Update from './modals/Update';

export default function Index({ title = '员工角色' }) {
  const [searchParams] = useSearchParams();
  const navigate = useNavigate();
  return (
    <MyPageContainer
      title={title}
      enableTabs={true}
      tabKey="company-employee-roles"
      tabLabel={title}
    >
      <ProTable
        {...MyProTableProps.props}
        search={false}
        bordered
        headerTitle={
          <Space>
            <MyButtons.Default
              title=""
              icon={<LeftOutlined />}
              onClick={() => navigate(-1)}
            />
            {title}
          </Space>
        }
        request={async (params, sort) =>
          MyProTableProps.request(
            {
              ...params,
              companies_id: Number(searchParams.get('id')),
            },
            sort,
            Apis.Company.EmployeeRoles.List,
          )
        }
        toolBarRender={(action) => [
          <Create
            key="Create"
            reload={action?.reload}
            item={{ companies_id: Number(searchParams.get('id')) }}
            title={title}
          />,
        ]}
        columns={[
          MyColumns.ID(),
          { dataIndex: 'name', title: '名称' },
          {
            title: '配置角色权限',
            hideInSearch: true,
            render: (_, item, index, action) => (
              <EditPermissions
                item={item}
                title="配置角色权限"
                reload={action?.reload}
              />
            ),
          },
          MyColumns.CreatedAt(),
          MyColumns.Option({
            render: (_, item: any, index, action) => (
              <Space key={index}>
                <Update item={item} reload={action?.reload} title={title} />
                <MyButtons.Delete
                  onConfirm={() =>
                    Apis.Company.EmployeeRoles.Delete({ id: item.id }).then(
                      () => action?.reload(),
                    )
                  }
                />
              </Space>
            ),
          }),
        ]}
      />
    </MyPageContainer>
  );
}
