import {
  MyColumns,
  MyImportModal,
  MyPageContainer,
  MyProTableProps,
  MyTableActions,
  MyToolBarActions,
  renderTextHelper,
  useCurrentPermissions,
} from '@/common';
import { Apis } from '@/gen/Apis';
import { CompanyEmployeesTypeEnum } from '@/gen/Enums';
import { ProTable } from '@ant-design/pro-components';
import { Space } from 'antd';
import Change from './modals/Change';
import EmployeeCreate from './modals/EmployeeCreate';
import EmployeeUpdate from './modals/EmployeeUpdate';

export default function Index({ title = '外包人员' }) {
  const getCurrentPermissions = useCurrentPermissions();
  return (
    <MyPageContainer
      title={title}
      enableTabs={true}
      tabKey="attendance_employees"
      tabLabel={title}
    >
      <ProTable
        {...MyProTableProps.props}
        // search={false}
        headerTitle="外包人员列表"
        tooltip="通过企微同步的员工信息的修改，需在企微修改后，同步到系统，才能生效。"
        request={async (params, sort) =>
          MyProTableProps.request(
            {
              type: 'External',
              ...params,
            },
            sort,
            Apis.Company.CompanyEmployees.List,
          )
        }
        toolBarRender={(action) => [
          <MyToolBarActions
            key="toolbar"
            actions={{
              import: (
                <MyImportModal
                  key="ImportHouse"
                  title="导入外部人员"
                  type="default"
                  size="middle"
                  templateApi={Apis.Company.CompanyEmployees.DownloadTemplate}
                  importApi={Apis.Company.CompanyEmployees.Import}
                  reload={action?.reload}
                />
              ),
              add: (
                <EmployeeCreate
                  key="Create"
                  reload={action?.reload}
                  title="员工"
                />
              ),
            }}
          />,
        ]}
        columns={[
          MyColumns.ID({ search: false }),
          {
            title: '所在组织',
            dataIndex: 'organization_path',
            search: {
              transform: (value) => {
                return { organization_name: value };
              },
            },
          },
          {
            title: '姓名',
            dataIndex: 'name',
          },
          {
            title: '手机号',
            dataIndex: 'phone',
          },
          {
            title: '角色',
            dataIndex: 'roles',
            renderText: renderTextHelper.TagList,
            hideInSearch: true,
          },
          {
            title: '岗位',
            dataIndex: ['position', 'name'],
            search: false,
          },
          MyColumns.EnumTag({
            title: '来源',
            dataIndex: 'type',
            valueEnum: CompanyEmployeesTypeEnum,
            search: false,
          }),
          MyColumns.SoftDelete({
            title: '启/禁用',
            onRestore: Apis.Company.CompanyEmployees.Restore,
            onSoftDelete: Apis.Company.CompanyEmployees.SoftDelete,
            search: false,
            setPermissions: getCurrentPermissions({
              enableDisable: true,
            }),
          }),
          MyColumns.UpdatedAt(),
          MyColumns.Option({
            render: (_, item: any, index, action) => (
              <Space key={index}>
                <MyTableActions
                  actions={{
                    update: (
                      <EmployeeUpdate
                        item={item}
                        reload={action?.reload}
                        title={title}
                      />
                    ),
                    change: (
                      <Change
                        item={item}
                        reload={action?.reload}
                        title={title}
                      />
                    ),
                  }}
                />
              </Space>
            ),
          }),
        ]}
      />
    </MyPageContainer>
  );
}
