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/CategoryCreate';
import Update from '../modals/CategoryUpdate';

export default function Index({ title = '分类' }) {
  return (
    <ProTable
      {...MyProTableProps.props}
      request={async (params, sort) =>
        MyProTableProps.request(
          params,
          sort,
          Apis.Emergency.EmergencyEventCategories.TreeList,
        )
      }
      headerTitle="突发事件分类"
      toolBarRender={(action) => [
        <Create key="Create" reload={action?.reload} title={title} />,
      ]}
      columns={[
        MyColumns.ID({
          search: false,
        }),
        {
          title: '名称',
          dataIndex: 'name',
        },
        {
          title: '分类描述',
          dataIndex: 'description',
          search: false,
        },
        MyColumns.Boolean({
          title: '是否启用',
          dataIndex: 'is_enabled',
          search: false,
        }),
        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>
          ),
        }),
      ]}
    />
  );
}
