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

export default function QuestionCategoriesList({ title = '题目分类' }) {
  return (
    <ProTable
      {...MyProTableProps.props}
      search={false}
      headerTitle="分类列表"
      request={async (params, sort) =>
        MyProTableProps.request(
          params,
          sort,
          Apis.Survey.QuestionCategories.List,
        )
      }
      toolBarRender={(action) => [
        <Create key="Create" reload={action?.reload} title={title} />,
      ]}
      columns={[
        MyColumns.ID({
          search: false,
        }),
        { dataIndex: 'name', title: '名称' },
        { dataIndex: 'description', title: '描述' },
        MyColumns.Boolean({ dataIndex: 'is_enabled', title: '是否启用' }),
        MyColumns.Option({
          width: 120,

          render: (_, item: any, index, action) => (
            <Space key={`item_${index}`}>
              <Update item={item} reload={action?.reload} title={title} />
              <MyButtons.Delete
                onConfirm={() =>
                  Apis.Survey.QuestionCategories.Delete({ id: item.id }).then(
                    () => action?.reload(),
                  )
                }
              />
            </Space>
          ),
        }),
      ]}
    />
  );
}
