import {
  MyButtons,
  MyColumns,
  MyPageContainer,
  MyProTableProps,
  renderTextHelper,
  useCurrentPermissions,
} from '@/common';
import { Apis } from '@/gen/Apis';
import { ProTable } from '@ant-design/pro-components';
import { Dropdown, Space } from 'antd';
import Change from './modals/Change';

import { Selects } from '@/components/Select';
import EmployeeCreate from './modals/EmployeeCreate';
import EmployeeUpdate from './modals/EmployeeUpdate';
import Role from './modals/Role';

export default function Index({ title = '员工管理' }) {
  const getCurrentPermissions = useCurrentPermissions();
  let toolBarRender = (action: any) => {
    return getCurrentPermissions({
      add: <EmployeeCreate key="Create" reload={action?.reload} title="员工" />,
    });
  };
  let tableRender = (item: any, action: any) => {
    let permissions = getCurrentPermissions({
      update: (
        <EmployeeUpdate item={item} reload={action?.reload} title={title} />
      ),
      CompanyEmployees: (
        <Change item={item} reload={action?.reload} title={title} />
      ),
    });
    let permissionsSpace = getCurrentPermissions({
      Role: {
        key: '1',
        label: <Role item={item} reload={action?.reload} title={title} />,
      },
      ResetPassword: {
        key: '2',
        label: (
          <MyButtons.Default
            title="重置"
            type="default"
            isConfirm={true}
            description={`确定要重置用户「${item.name}」的密码为「ly#123」吗？`}
            onConfirm={() =>
              Apis.Company.CompanyEmployees.ResetPassword({
                id: item.id,
                password: 'ly#123',
              }).then(() => action?.reload())
            }
          />
        ),
      },
      delete: {
        key: '3',
        label: (
          <MyButtons.Delete
            onConfirm={() =>
              Apis.Company.CompanyEmployees.Delete({ id: item.id }).then(() =>
                action?.reload(),
              )
            }
          />
        ),
      },
    });
    let Others = (
      <Dropdown menu={{ items: permissionsSpace }} trigger={['click']}>
        <MyButtons.Default title="更多" />
      </Dropdown>
    );
    return [...permissions, ...[Others]];
  };
  return (
    <MyPageContainer
      enableTabs
      tabKey="employees-list"
      tabLabel={title}
      title={title}
    >
      <ProTable
        {...MyProTableProps.props}
        // search={false}
        headerTitle="员工列表"
        request={async (params, sort) =>
          MyProTableProps.request(
            params,
            sort,
            Apis.Company.CompanyEmployees.List,
          )
        }
        toolBarRender={(action) => [toolBarRender(action)]}
        columns={[
          MyColumns.ID({
            search: false,
          }),
          Selects?.OrganizationsTree({
            title: '选择组织',
            key: 'organizations_id',
            search: {
              transform: (value) => {
                return { organizations_id: value[value.length - 1] };
              },
            },
          }),
          {
            title: '所在组织',
            dataIndex: 'organization_path',
            search: false,
            // search: {
            //   transform: (value) => {
            //     return { organization_name: value };
            //   },
            // },
          },
          {
            title: '姓名',
            dataIndex: 'name',
          },
          {
            title: '手机号',
            dataIndex: 'phone',
          },
          {
            title: '岗位',
            dataIndex: ['position', 'name'],
            search: false,
          },
          {
            title: '系统角色',
            dataIndex: 'roles',
            renderText: renderTextHelper.TagList,
            hideInSearch: true,
          },

          // MyColumns.EnumTag({
          //   title: '来源',
          //   dataIndex: 'type',
          //   // valueEnum: CompanyEmployeesTypeEnum,
          // }),
          // MyColumns.SoftDelete({
          //   title: '启/禁用',
          //   onRestore: Apis.Company.CompanyEmployees.Restore,
          //   onSoftDelete: Apis.Company.CompanyEmployees.SoftDelete,
          //   search: false,
          //   setPermissions: getCurrentPermissions({
          //     enableDisable: true,
          //   }),
          // }),
          // MyColumns.UpdatedAt(),
          // MyColumns.CreatedAt(),
          MyColumns.Option({
            render: (_, item: any, index, action) => (
              <Space key={index}>
                <>{tableRender(item, action)}</>
              </Space>
            ),
          }),
        ]}
      />
    </MyPageContainer>
  );
}
