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

/** 添加和修改监测数据上报类型 */
const ExamDataReportEditModal: React.FC<{
    data: Partial<API.ExamDataReportOutput>;
    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 } = useModel('useDict');

    return (
        <>
            <MovableModalForm
                title={`${data.id !== 0 ? '修改' : '添加'}监测数据上报类型`}
                width={1080}
                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.AddExamDataReportInput = {
                            ...restValues,
                            type: JSON.parse(type),
                            examPlanId: data.examPlanId,
                        };
                        if (data.id === 0) {
                            await ExamDataReportController.add(p);
                        } else {
                            let updateParams = { ...p, id: data.id } as API.UpdateExamDataReportInput;
                            await ExamDataReportController.update(updateParams);
                        }
                        message.success('操作成功！');
                        formRef?.current?.resetFields();
                        onFinish();
                        handleClose();
                        return true;
                    } catch {
                        message.error('操作失败！');
                        return false;
                    }
                }}
            >
                <Row gutter={[24, 0]}>
                    <Col span={8}>
                        <ProFormSelect
                            label="上报类型"
                            name="type"
                            disabled={data.id !== 0}
                            rules={[{ required: true }]}
                            valueEnum={getDictValueEnum('data_report_type')}
                        />
                    </Col>
                    <Col span={8}>
                        <ProFormDateTimePicker
                            label="开始时间"
                            name="beginTime"
                            fieldProps={{ style: { width: '100%' } }}
                            rules={[{ required: true }]}
                        />
                    </Col>
                    <Col span={8}>
                        <ProFormDateTimePicker
                            label="结束时间"
                            name="endTime"
                            fieldProps={{ style: { width: '100%' } }}
                            rules={[{ required: true }]}
                        />
                    </Col>
                </Row>
                <ProFormTextArea
                    label="上报说明"
                    name="remark"
                    fieldProps={{
                        maxLength: 2000,
                        rows: 22,
                    }}
                />
            </MovableModalForm >
        </>
    );
};

export default ExamDataReportEditModal;
