import { MovableModalForm } from "@/components";
import ExamGradeController from "@/services/apis/ExamGradeController";
import { ProFormCheckbox, ProFormText, ProFormTextArea } from "@ant-design/pro-components";
import { App } from "antd";
import { useState } from "react";

/** 监测年级设置 */
const ExamGradeSettingModal: React.FC<{
    /** 监测年级信息 */
    data: API.ExamGradeOutput;
    /** 保存成功回调 */
    onOk: () => void;
    /** 关闭回调 */
    onClose: () => void;
}> = ({ data, onOk, onClose }) => {
    const [open, setOpen] = useState<boolean>(true);
    const handleClose = () => { setOpen(false); setTimeout(onClose, 300); };
    const { message } = App.useApp();
    return (
        <MovableModalForm<{
            isRequiredSample: boolean;
            isRequiredSelfExamNumber: boolean;
            selfExamNumberLength: number;
            remark: string;
        }>
            title="监测年级参数配置"
            width={480}
            open={open}
            modalProps={{
                centered: true,
                maskClosable: false,
                onCancel: handleClose,
            }}
            initialValues={data}
            onFinish={async (values) => {
                const p = { id: data.id, ...values };
                try {
                    await ExamGradeController.saveSetting(p);
                    message.success('已保存');
                    onOk();
                    handleClose();
                }
                catch { }
            }}
        >
            <ProFormCheckbox
                label="需要抽样监测"
                name="isRequiredSample"
            />
            <ProFormCheckbox
                label="需要自编监测号"
                name="isRequiredSelfExamNumber"
            />
            <ProFormText
                label="自编监测号长度"
                name="selfExamNumberLength"
                dependencies={['isRequiredSelfExamNumber']}
                fieldProps={{ defaultValue: 0 }}
                required
                rules={[
                    { required: true },
                    ({ getFieldValue }) => ({
                        validator: (_, value) => {
                            const irsen = getFieldValue("isRequiredSelfExamNumber");
                            if (irsen && value <= 0) {
                                return Promise.reject('需要自编监测号时，长度必须大于0');
                            }
                            return Promise.resolve();
                        },
                    }),
                ]}
            />
            <ProFormTextArea
                label="备注"
                name="remark"
            />
        </MovableModalForm>
    );
}

export default ExamGradeSettingModal;
