/*
 * Create:      柿子
 * CreateDate:  2024/03/06
 * Describe：   配镜管理 - 处方交换类型
 * */
import React, { Component } from 'react';
import { Modal, message } from 'antd';
import DynamicRenderingForm from 'pages/common/DynamicRenderingForm';
import '../style/index.less';

class PrescriptionExchangeType extends Component {
    constructor(props) {
        super(props);
        this.state = {
            visible: false,
            rowData: {},
            selectData: {},
            okBtnLoading: false,
            formData: [],
        }
    };

    static getDerivedStateFromProps(nextProps, prevState) {
        if (JSON.stringify(nextProps.selectData) !== JSON.stringify(prevState.selectData)) {
            return {
                selectData: nextProps.selectData
            }
        }
        return null
    }

    componentDidMount() {
        this.props.onRef && this.props.onRef(this);
    };

    // 修改弹窗状态
    modifyVisible = (visible, record = {}) => {
        if (visible) {
            const { formData } = this.state;
            if (!(formData && formData.length > 0)) {
                this.getFormData();
            }
            this.state.rowData = record;
        }
        this.setState({ visible, okBtnLoading: false });
    };

    // 获取表单数据
    getFormData = async () => {
        try {
            let data = {
                params: [{
                    compontName: 'PrescriptionExchangeType',
                    type: 'C',
                }]
            }
            let res = await React.$asyncPost(this, '01040073', data);
            let formData = res.result?.formData || [];
            for (let i = 0; i < formData.length; i++) {
                if (formData[i]?.dataIndex === 'typeID') {
                    formData[i].callBackResult = 'Fn';
                    formData[i].changeCallBack = this.handleTypeChange;
                }
            }
            this.setState({ formData });
        } catch (error) {
            console.log(error);
        }
    };

    // 类型切换显示相对应的提醒
    handleTypeChange = (e, dataIndex) => {
        console.log('e, dataIndex', e, dataIndex)
        let { formData } = this.state;
        for (let i = 0; i < formData.length; i++) {
            if (formData[i]?.dataIndex === dataIndex) {
                if (e === '2') {
                    formData[i].validateStatus = 'warning';
                    formData[i].help = '请注意:不同系列的处方，新镜片到货后，需将原镜片进行退片和退费，再对新镜片入库、收费、出库';
                } else {
                    formData[i].validateStatus = '';
                    formData[i].help = '';
                }
            }
        }
        this.setState({ formData });
    }

    // 确认
    handleOk = async () => {
        let values = await this.formRef.handleSave();
        if (values && values.error) {
            message.error('请完善必填信息。');
            return;
        }
        console.log('values', values)
    };

    render() {
        const { visible, okBtnLoading, formData, rowData, selectData } = this.state;
        return (
            <Modal
                width="800px"
                title="处方交换类型"
                className="prescription-exchange-type-modal"
                bodyStyle={{ padding: '12px' }}
                visible={visible}
                okButtonProps={{
                    loading: okBtnLoading
                }}
                onOk={this.handleOk}
                onCancel={() => this.modifyVisible(false)}
            >
                <div className="mdm-left-header petm-header flex-align-items">
                    <span>
                        病人ID：
                    </span>
                    {rowData?.patNo || ''}
                    <span>
                        姓名：
                    </span>
                    {rowData?.patName || ''}
                    <span>
                        原订单号：
                    </span>
                    {rowData?.orderInfo || ''}
                </div>
                <DynamicRenderingForm
                    rowData={{}}
                    selectData={selectData}
                    formData={formData}
                    onRef={ref => this.formRef = ref}
                />
            </Modal>
        );
    }
};

export default PrescriptionExchangeType;