import { MovableModalForm } from '@/components';
import ExamPaperQuestionMinorController from '@/services/apis/ExamPaperQuestionMinorController';
import { ProFormDigit, ProFormItem, ProFormSelect } from '@ant-design/pro-components';
import { useModel } from '@umijs/max';
import { App } from 'antd';
import { useState } from 'react';

/** 批量设置小题 */
const ExamPaperQuestionMinorSettingModal: React.FC<{
    ids: number[];
    examPaperQuestionMajorList: API.ExamPaperQuestionMajorOutput[];
    onFinish: () => void;
    onClose?: () => void;
}> = ({ ids, examPaperQuestionMajorList, onFinish, onClose }) => {
    const [open, setOpen] = useState<boolean>(true);
    const handleClose = () => { setOpen(false); setTimeout(() => onClose?.(), 300); };

    const { message } = App.useApp();
    const { getDictOptions } = useModel('useDict');

    return (
        <MovableModalForm<{
            examPaperQuestionMajorId?: number;
            score: number;
        }>
            title="批量更新小题"
            width={640}
            open={open}
            modalProps={{
                centered: true,
                maskClosable: false,
                onCancel: handleClose,
            }}
            onFinish={async (values: any) => {
                try {
                    const { questionCatalog, ...restValues } = values;
                    const p: API.BatchUpdateExamPaperQuestionMinorInput = {
                        ...restValues,
                        ids,
                        questionCatalog: questionCatalog ? JSON.parse(`${questionCatalog}`) : undefined,
                    };
                    await ExamPaperQuestionMinorController.batchUpdate(p);
                    message.success('操作成功！');
                    onFinish();
                    handleClose();
                    return true;
                } catch {
                    message.error('操作失败！');
                    return false;
                }
            }}
        >
            <ProFormItem label="批量设置小题数量">
                {ids.length}
            </ProFormItem>
            <ProFormSelect
                label="大题"
                name="examPaperQuestionMajorId"
                fieldProps={{
                    options: examPaperQuestionMajorList?.map(t => ({
                        label: `${t.number}${t.title !== '' ? '、' : ''}${t.title}`,
                        value: t.id,
                    })) ?? []
                }}
            />
            <ProFormSelect
                label="题型"
                name="questionCatalog"
                fieldProps={{
                    options: getDictOptions('question_catalog').filter(t => t.value > 0),
                }}
            />
            <ProFormDigit
                label="分值"
                name="score"
                fieldProps={{
                    min: 0,
                    max: 100,
                    precision: 2,
                    step: 1,
                }}
                rules={[
                    { required: true },
                    () => ({
                        validator: (_, value) => {
                            if (value === undefined || value === null || value === '' || (value > 0 && value <= 100)) {
                                return Promise.resolve();
                            }
                            return Promise.reject(`分值取值范围为：大于0，且小于等于${100}`);
                        },
                    }),
                ]}
            />
        </MovableModalForm>
    );
};

export default ExamPaperQuestionMinorSettingModal;
