import { MovableModalForm } from '@/components';
import ExamDataPublishController from '@/services/apis/ExamDataPublishController';
import { FormInstance, ProFormSelect, ProFormText, ProFormTextArea } from '@ant-design/pro-components';
import { useModel } from '@umijs/max';
import { App } from 'antd';
import { useRef, useState } from 'react';

/** 添加或修改监测发布内容 */
const ExamDataPublishEditModal: React.FC<{
    data: Partial<API.ExamDataPublishOutput>;
    onFinish: () => void;
    onClose?: () => void;
}> = ({ data, onFinish, onClose }) => {
    const [open, setOpen] = useState<boolean>(true);
    const handleClose = () => { setOpen(false); setTimeout(() => onClose?.(), 300); };

    const formRef = useRef<FormInstance>();
    const { message } = App.useApp();

    const { getDictValueEnum, getKeyDict } = useModel('useDict');
    const dataPublishTypeDict = getKeyDict('data_publish_type');

    return (
        <>
            <MovableModalForm
                title={`${data.id !== 0 ? '修改' : '添加'}监测发布内容`}
                width={640}
                open={open}
                formRef={formRef}
                initialValues={{
                    ...data,
                    type: data.type !== undefined ? `${data.type}` : undefined,
                }}
                modalProps={{
                    centered: true,
                    maskClosable: false,
                    onCancel: () => {
                        formRef?.current?.resetFields();
                        handleClose();
                    },
                }}
                onFinish={async (values: any) => {
                    try {
                        const { type, ...restValues } = values;
                        const p: API.AddExamDataPublishInput = {
                            ...restValues,
                            type: JSON.parse(type),
                            examPlanId: data.examPlanId,
                        };
                        if (data.id === 0) {
                            await ExamDataPublishController.add(p);
                        } else {
                            let updateParams = { ...p, id: data.id } as API.UpdateExamDataPublishInput;
                            await ExamDataPublishController.update(updateParams);
                        }
                        message.success('操作成功！');
                        formRef?.current?.resetFields();
                        onFinish();
                        handleClose();
                        return true;
                    } catch {
                        message.error('操作失败！');
                        return false;
                    }
                }}
            >
                <ProFormSelect
                    label="发布类型"
                    name="type"
                    disabled={data.id !== 0}
                    rules={[{ required: true }]}
                    valueEnum={getDictValueEnum('data_publish_type')}
                    onChange={(v) => {
                        const dpt = dataPublishTypeDict[v as number];
                        formRef.current?.setFieldValue('name', dpt?.name ?? '');
                    }}
                />
                <ProFormText
                    label="发布名称"
                    name="name"
                    required
                    rules={[{ required: true }]}
                />
                <ProFormTextArea
                    label="备注说明"
                    name="remark"
                    fieldProps={{
                        maxLength: 2000,
                        rows: 10,
                    }}
                />
            </MovableModalForm >
        </>
    );
};

export default ExamDataPublishEditModal;
