import { MyButtons, MyColumns, MyProTableProps } from '@/common';
import { Apis } from '@/gen/Apis';
import { PlusOutlined } from '@ant-design/icons';
import { ProTable } from '@ant-design/pro-components';
import { useNavigate } from '@umijs/max';
import { Space } from 'antd';
import SurveysListShow from '../modals/SurveysListShow';

export default function SurveysList({ title = '问卷设置' }) {
  const navigate = useNavigate();

  return (
    <ProTable
      {...MyProTableProps.props}
      headerTitle={`${title}`}
      request={async (params, sort) =>
        MyProTableProps.request(params, sort, Apis.Survey.Surveys.List)
      }
      toolBarRender={() => [
        <MyButtons.Default
          key="Create"
          size="middle"
          type="primary"
          icon={<PlusOutlined />}
          onClick={() => {
            navigate('/quality/survey/pages/create_surveys');
          }}
          title="新增问卷"
        />,
      ]}
      columns={[
        MyColumns.ID({
          search: false,
        }),
        { dataIndex: 'name', title: '问卷' },
        {
          title: '问题数',
          dataIndex: 'questions',
          render: (_, item: any) => {
            const questions = item?.questions || [];
            const questionCount = questions.length;
            return <div>{questionCount}</div>;
          },
        },
        MyColumns.Boolean({
          dataIndex: 'is_enabled',
          title: '是否启用',
          search: false,
        }),
        MyColumns.CreatedAt(),
        MyColumns.Option({
          width: 120,
          render: (_, item: any, index, action) => (
            <Space key={`item_${index}`}>
              <SurveysListShow
                key="queryInfo"
                item={item}
                title="查看"
                reload={action?.reload}
              />
              <MyButtons.Default
                key="update"
                type="primary"
                onClick={() => {
                  navigate(
                    `/quality/marketing/survey/pages/update_surveys?id=${item.id}`,
                  );
                }}
                title="编辑"
              />
              <MyButtons.Delete
                onConfirm={() =>
                  Apis.Survey.Surveys.Delete({ id: item.id }).then(() =>
                    action?.reload(),
                  )
                }
              />
            </Space>
          ),
        }),
      ]}
    />
  );
}
