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={title} />,
      },
      'MyLevels',
    );
  };
  let tableRender = (item: any, action: any) => {
    return getCurrentPermissions(
      {
        update: <Update item={item} reload={action?.reload} title={title} />,
        delete: (
          <MyButtons.Delete
            key="delete"
            onConfirm={() =>
              Apis.Emergency.EmergencyEventLevels.Delete({
                id: item.id,
              }).then(() => action?.reload())
            }
          />
        ),
      },
      'MyLevels',
    );
  };
  return (
    <ProTable
      {...MyProTableProps.props}
      request={async (params, sort) =>
        MyProTableProps.request(
          params,
          sort,
          Apis.Emergency.EmergencyEventLevels.List,
        )
      }
      headerTitle="突发事件等级"
      toolBarRender={(action) => [toolBarRender(action)]}
      columns={[
        MyColumns.ID({
          search: false,
        }),
        {
          title: '名称',
          dataIndex: 'name',
        },
        MyColumns.Boolean({
          title: '是否启用',
          dataIndex: 'is_enabled',
          search: false,
        }),
        MyColumns.UpdatedAt(),
        MyColumns.CreatedAt(),
        MyColumns.Option({
          render: (_, item: any, index, action) => (
            <Space key={index}>{tableRender(item, action)}</Space>
          ),
        }),
      ]}
    />
  );
}
