import { toSelectOptions } from '@/common/converter';
import { SuperTable } from '@/components';
import ExamOrgDataReportController from '@/services/apis/ExamOrgDataReportController';
import { DataReportType, ExamStatus } from '@/services/enums';
import { ArrowDownOutlined } from '@ant-design/icons';
import { ActionType, PageContainer, ProColumns } from '@ant-design/pro-components';
import { history, useModel } from '@umijs/max';
import { useRef } from 'react';

/** 学生家长问卷监测计划列表 */
const OrgExamPatriarchQuestionnaireList: React.FC = () => {
    const actionRef = useRef<ActionType>();

    const { getDictValueEnum } = useModel('useDict');
    const { baseData } = useModel('useBaseData');

    const columns: ProColumns<API.ExamPlanOrgDataReportOutput>[] = [
        {
            title: '监测名称',
            dataIndex: 'examPlanFullName',
            renderText: (v, r) => <a onClick={() => history.push(`/exam-s/questionnaire/patriarch/progress/${r.examPlanId}`)}>{v}</a>,
            hideInDescriptions: true,
            search: {
                transform: (v) => ({ name: v }),
            },
        },
        {
            title: '监测学期',
            dataIndex: 'semester_id',
            width: 256,
            align: 'center',
            hideInSearch: true,
            render: (_, r) => {
                const s = baseData?.semesters?.find(t => t.id === r.semesterId);
                if (s) {
                    return `${s.name}（${s.nickShortName}）`;
                }
                return null;
            },
        },
        {
            title: '监测状态',
            dataIndex: 'examStatus',
            valueEnum: getDictValueEnum('exam_status', true, [ExamStatus.READY, ExamStatus.CANCELLED]),
            width: 80,
            align: 'center',
            hideInDescriptions: true,
        },
        {
            title: '监测学期',
            dataIndex: ['semester', 'nickShortName'],
            search: {
                transform: (v) => v ? ({ semesterId: JSON.parse(v) }) : v,
            },
            valueType: 'select',
            fieldProps: {
                options: toSelectOptions(baseData?.semesters ?? [], (item) => ({ key: item.id, label: item.nickShortName, value: item.id })),
            },
            hideInTable: true,
        },
    ];

    return (
        <PageContainer title={false}>
            <SuperTable<API.ExamPlanOrgDataReportOutput>
                headerTitle="学生家长问卷监测计划"
                actionRef={actionRef}
                columns={columns}
                scroll={{ x: 'max-content' }}
                rowKey="rowNumber"
                toolbar={{
                    title: '学生家长问卷监测计划',
                    subTitle: (
                        <>
                            <ArrowDownOutlined />
                            <span>点击下方列表监测名称查看详情</span>
                            <ArrowDownOutlined />
                        </>
                    ),
                }}
                request={async (params, sort) => {
                    return SuperTable.requestPageAgent({ params, sort }, async (p) => {
                        const res = await ExamOrgDataReportController.queryPageList({ ...p, type: DataReportType.STUDENT });
                        return res;
                    });
                }}
            />
        </PageContainer>
    );
};

export default OrgExamPatriarchQuestionnaireList;
