import {
  MyButtons,
  MyColumns,
  MyPageContainer,
  MyProTableProps,
  MyTableActions,
  MyToolBarActions,
  renderTextHelper,
} from '@/common';
import { Apis } from '@/gen/Apis';
import { ProTable } from '@ant-design/pro-components';
import { 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 = '员工管理' }) {
  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) => [
          <MyToolBarActions
            key="toolbar"
            actions={{
              sync: (
                <MyButtons.Default
                  key="sync_wechat_employees"
                  type="primary"
                  size="middle"
                  title="同步企微"
                  isConfirm={true}
                  description="确定要执行企微员工同步操作吗？"
                  onConfirm={async () => {
                    try {
                      await Apis.Company.CompanyEmployees.SyncWechatEmployees();
                      action?.reload?.();
                    } catch (error) {
                      console.error('同步企微信息失败:', error);
                    }
                  }}
                />
              ),
              add: (
                <EmployeeCreate
                  key="Create"
                  reload={action?.reload}
                  title="员工"
                />
              ),
            }}
          />,
        ]}
        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: 'employee_roles',
            renderText: renderTextHelper.TagList,
            hideInSearch: true,
          },
          {
            title: '后台角色',
            dataIndex: 'company_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}>
                <MyTableActions
                  actions={{
                    update: (
                      <EmployeeUpdate
                        item={item}
                        reload={action?.reload}
                        title={title}
                      />
                    ),
                    CompanyEmployees: (
                      <Change
                        item={item}
                        reload={action?.reload}
                        title={title}
                      />
                    ),
                    Role: (
                      <Role item={item} reload={action?.reload} title={title} />
                    ),
                    ResetPassword: (
                      <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: (
                      <MyButtons.Delete
                        onConfirm={() =>
                          Apis.Company.CompanyEmployees.Delete({
                            id: item.id,
                          }).then(() => action?.reload())
                        }
                      />
                    ),
                  }}
                />
              </Space>
            ),
          }),
        ]}
      />
    </MyPageContainer>
  );
}
