import {
  MyButtons,
  MyColumns,
  MyPageContainer,
  MyProTableProps,
  MyTableActions,
  MyToolBarActions,
} from '@/common';
import { Selects } from '@/components/Select';
import { Apis } from '@/gen/Apis';
import { HouseWorkOrdersTypeEnum } from '@/gen/Enums';
import { ProTable } from '@ant-design/pro-components';
import ManagersCreate from './modals/ManagersCreate';

export default function Index({ title = '工单管理员' }) {
  return (
    <MyPageContainer
      title={title}
      enableTabs={true}
      tabKey="work_order_managers"
      tabLabel={title}
    >
      <ProTable<Record<any, any>>
        {...MyProTableProps.props}
        request={async (params, sort) =>
          MyProTableProps.request(
            params,
            sort,
            Apis.Asset.AssetProjectAdmins.List,
          )
        }
        headerTitle="工单管理员（配置后，对应人员可在员工端分配工单）"
        toolBarRender={(action) => [
          <MyToolBarActions
            key="toolbar"
            actions={{
              create: (
                <ManagersCreate
                  key="Create"
                  reload={action?.reload}
                  title={title}
                />
              ),
            }}
          />,
        ]}
        columns={[
          MyColumns.ID(),
          Selects?.AssetProjects({
            title: '选择项目',
            key: 'asset_projects_id',
            hidden: true,
          }),
          {
            title: '项目',
            dataIndex: ['asset_project', 'name'],
            search: false,
          },
          MyColumns.EnumTag({
            title: '工单类型',
            dataIndex: 'type',
            valueEnum: HouseWorkOrdersTypeEnum,
          }),
          {
            title: '工单管理员',
            dataIndex: ['company_employee', 'name'],
            render: (_, item: any) =>
              `${item?.company_employee?.name || ''}-${
                item?.company_employee?.phone || ''
              }`,
            search: false,
          },
          MyColumns.UpdatedAt(),
          MyColumns.Option({
            render: (_, item: any, __, action) => (
              <MyTableActions
                actions={{
                  delete: (
                    <MyButtons.Delete
                      onConfirm={() =>
                        Apis.Asset.AssetProjectAdmins.Delete({
                          id: item.id,
                        }).then(() => action?.reload())
                      }
                    />
                  ),
                }}
              />
            ),
          }),
        ]}
      />
    </MyPageContainer>
  );
}
