import { MyButtons, MyColumns, MyProTableProps } from '@/common';
import { Apis } from '@/gen/Apis';
import { HouseWorkOrdersTypeEnum } from '@/gen/Enums';
import { ProTable } from '@ant-design/pro-components';
import { Space } from 'antd';
import { useEffect, useRef } from 'react';
import ManagersCreate from '../modals/ManagersCreate';

export default function Index({ ...rest }) {
  const actionLooks = useRef<any>();
  useEffect(() => {
    actionLooks?.current.reloadAndRest();
  }, [rest.loadmore]);

  return (
    <>
      <ProTable<Record<any, any>>
        {...MyProTableProps.props}
        actionRef={actionLooks}
        request={async (params, sort) =>
          MyProTableProps.request(
            { ...params, asset_projects_id: rest.item?.id },
            sort,
            Apis.Asset.AssetProjectAdmins.List,
          )
        }
        headerTitle="工单管理员（配置后，对应人员可在员工端分配工单）"
        toolBarRender={(action) => [
          <ManagersCreate
            key="Create"
            item={rest.item}
            reload={action?.reload}
            title="工单管理员"
          />,
        ]}
        search={false}
        columns={[
          MyColumns.ID(),
          // {
          //   title: '项目',
          //   dataIndex: ['asset_project', 'name'],
          // },
          MyColumns.EnumTag({
            title: '工单类型',
            dataIndex: 'type',
            valueEnum: HouseWorkOrdersTypeEnum,
            // search: false,
          }),
          {
            title: '工单管理员',
            dataIndex: ['company_employee', 'name'],
            render: (_, item: any) =>
              `${item?.company_employee?.name || ''}-${
                item?.company_employee?.phone || ''
              }`,
            search: false,
          },
          // MyColumns.CreatedAt(),
          MyColumns.UpdatedAt(),
          MyColumns.Option({
            render: (_, item: any, index, action) => (
              <Space key={index}>
                <MyButtons.Delete
                  onConfirm={() =>
                    Apis.Asset.AssetProjectAdmins.Delete({ id: item.id }).then(
                      () => action?.reload(),
                    )
                  }
                />
              </Space>
            ),
          }),
        ]}
      />
    </>
  );
}
