import React, { useState, useEffect } from 'react';
import { Modal, Form } from 'antd';
import GForm from '../GForm';
// @ts-ignore
import { handleModelFormDetail } from "../../utils/common.js"

export interface GModelFormProps {
    [propsName: string]: any
}

const GModelForm: React.FC<GModelFormProps> = (props) => {
    console.log('GModelForm组件的props', props)
    const { visible, onVisible, tableRrn, formDetail, parentFormDetail,formFinish, outsideValidate } = props //
    const [addVisible, setAddVisible] = useState<any>(false);
    const [addForm] = Form.useForm();

    useEffect(() => {
        setAddVisible(visible)
    }, [visible])

    const handleOk = () => {
        addForm.validateFields().then((values: any) => {
            values.ADTABLERRN = tableRrn;
            const data = handleModelFormDetail(values)
            // console.log('转换过布尔值的data', data);
            const formData = { ...formDetail, ...data }
            const validateResult = outsideValidate(values)
            if (validateResult) {
                formFinish(formData)
                setAddVisible(false)
                // 更新父组件的visible
                onVisible(false)
            }
        }).catch(err => console.log('校验失败', err))
    }
    const handleCancel = () => {
        setAddVisible(false)
        onVisible(false)
    }

    return (
        <Modal
            width="50%"
            title={formDetail.OBJECTRRN ? '编辑' : '添加'}
            visible={addVisible}
            onOk={handleOk}
            onCancel={handleCancel}
            destroyOnClose={true}
        >
            <GForm
                tableRrn={tableRrn}
                formDetail={formDetail}
                parentFormDetail={parentFormDetail}
                form={addForm}
                formType="addForm" />
        </Modal>
    )
}

export default GModelForm;