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

export default function Index({ title = '应急小组' }) {
  const getCurrentPermissions = useCurrentPermissions();

  let toolBarRender = (action: any) => {
    return getCurrentPermissions(
      {
        Create: (
          <Create key="Create" reload={action?.reload} title="应急小组" />
        ),
      },
      'MyLevels',
    );
  };
  let tableRender = (item: any, action: any) => {
    return getCurrentPermissions(
      {
        update: <Update item={item} reload={action?.reload} title={title} />,
        delete: (
          <MyButtons.Delete
            onConfirm={() =>
              Apis.Emergency.EmergencyEventCategories.Delete({
                id: item.id,
              }).then(() => action?.reload())
            }
          />
        ),
      },
      'MyLevels',
    );
  };
  return (
    <ProTable
      {...MyProTableProps.props}
      request={async (params, sort) =>
        MyProTableProps.request(
          params,
          sort,
          Apis.Emergency.EmergencyEventTeams.List,
        )
      }
      headerTitle="应急小组"
      toolBarRender={(action) => [toolBarRender(action)]}
      columns={[
        MyColumns.ID({
          search: false,
        }),
        {
          title: '项目',
          dataIndex: ['asset_project', 'name'],
          search: false,
        },
        {
          title: '名称',
          dataIndex: 'name',
        },
        {
          title: '队长',
          dataIndex: ['company_employee', 'name'],
          search: false,
        },
        MyColumns.Boolean({
          title: '是否启用',
          dataIndex: 'is_enabled',
          search: false,
        }),
        {
          title: '成员',
          search: false,
          render: (_, item: any) => {
            return (
              <Space>
                {item?.emergency_team_members?.map(
                  (res: any, index: number) => {
                    return (
                      <div key={`item_${index}`}>{res?.employee_name}</div>
                    );
                  },
                )}
              </Space>
            );
          },
        },
        MyColumns.UpdatedAt(),
        MyColumns.CreatedAt(),
        MyColumns.Option({
          render: (_, item: any, index, action) => (
            <Space key={index}>{tableRender(item, action)}</Space>
          ),
        }),
      ]}
    />
  );
}
