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

export default function Index({ title = '应急小组' }) {
  return (
    <ProTable
      {...MyProTableProps.props}
      request={async (params, sort) =>
        MyProTableProps.request(
          params,
          sort,
          Apis.Emergency.EmergencyEventTeams.List,
        )
      }
      headerTitle="应急小组"
      toolBarRender={(action) => [
        <Create key="Create" reload={action?.reload} title="应急小组" />,
      ]}
      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}>
              <Update item={item} reload={action?.reload} title={title} />
              <MyButtons.Delete
                onConfirm={() =>
                  Apis.Emergency.EmergencyEventCategories.Delete({
                    id: item.id,
                  }).then(() => action?.reload())
                }
              />
            </Space>
          ),
        }),
      ]}
    />
  );
}
