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

export default function QuestionsList({ title = '题目' }) {
  return (
    <ProTable
      {...MyProTableProps.props}
      headerTitle={`${title}列表`}
      request={async (params, sort) =>
        MyProTableProps.request(params, sort, Apis.Survey.Questions.List)
      }
      toolBarRender={(action) => [
        <Create key="Create" reload={action?.reload} title={title} />,
      ]}
      columns={[
        MyColumns.ID({
          search: false,
        }),
        { dataIndex: 'title', title: '题目' },
        MyColumns.EnumTag({
          title: '类型',
          dataIndex: 'type',
          valueEnum: QuestionsTypeEnum,
        }),
        { dataIndex: 'placeholder', title: '提示', search: false },
        MyColumns.Boolean({
          dataIndex: 'required',
          title: '是否必答',
          search: false,
        }),
        MyColumns.Boolean({
          dataIndex: 'is_enabled',
          title: '是否启用',
          search: false,
        }),
        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.Questions.Delete({ id: item.id }).then(() =>
                    action?.reload(),
                  )
                }
              />
            </Space>
          ),
        }),
      ]}
    />
  );
}
