import {
  MyButtons,
  MyColumns,
  MyImportModal,
  MyPageContainer,
  MyProTableProps,
} from '@/common';
import { Apis } from '@/gen/Apis';
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 = '员工管理' }) {
  console.log('title', btoa('#LY0123'));
  return (
    <MyPageContainer
      enableTabs
      tabKey="employees-list"
      tabLabel={title}
      title={title}
    >
      <ProTable
        {...MyProTableProps.props}
        // search={false}
        request={async (params, sort) =>
          MyProTableProps.request(
            params,
            sort,
            Apis.Company.CompanyEmployees.List,
          )
        }
        toolBarRender={(action) => [
          <MyImportModal
            key="ImportHouse"
            title="批量导入"
            type="danger"
            size="middle"
            templateApi={Apis.Company.CompanyEmployees.DownloadTemplate}
            importApi={Apis.Company.CompanyEmployees.Import}
            reload={action?.reload}
          />,
          <EmployeeCreate key="Create" reload={action?.reload} title="员工" />,
        ]}
        columns={[
          MyColumns.ID(),
          {
            title: '机构',
            dataIndex: ['company', 'name'],
            search: false,
          },
          {
            title: '所在组织',
            dataIndex: 'organization_path',
            search: {
              transform: (value) => {
                return { organization_name: value };
              },
            },
          },
          {
            title: '姓名',
            dataIndex: 'name',
          },
          {
            title: '手机号',
            dataIndex: 'phone',
          },
          {
            title: '岗位',
            dataIndex: ['position', 'name'],
            search: false,
          },
          {
            title: '是否管理员',
            dataIndex: 'is_company_admin',
            render: (_, item: any) => {
              return item?.is_company_admin ? '是' : '否';
            },
          },
          MyColumns.UpdatedAt(),
          // MyColumns.CreatedAt(),
          MyColumns.Option({
            render: (_, item: any, index, action) => (
              <Space key={index}>
                <EmployeeUpdate
                  item={item}
                  reload={action?.reload}
                  title={title}
                />
                <Change item={item} reload={action?.reload} title={title} />
                <MyButtons.Delete
                  onConfirm={() =>
                    Apis.Company.CompanyEmployees.Delete({ id: item.id }).then(
                      () => action?.reload(),
                    )
                  }
                />
              </Space>
            ),
          }),
        ]}
      />
    </MyPageContainer>
  );
}
