/*
* 确认挂号
* */

import React, { Component } from 'react';
import { Input, Form, Select, Modal, Button, DatePicker, Checkbox, message } from 'antd';
import { Util } from 'tools/index';
import { $http } from 'containers/config/https';
import '../style/index.less';
import moment from 'moment';

const Option = Select.Option;
const InputGroup = Input.Group;
const FormItem = Form.Item;

class ConfirmRegistrModal extends Component {

    constructor(props) {
        super(props);
        this.state = {
            payWayId: '',
            payWayMoney: '0',
            realPayMoney: '0'
        }
    }

    componentWillReceiveProps(nextProp) {
        if (!this.props.isShow && nextProp.isShow === true) {

        } else if (this.props.isShow === false) {
            this.cleanInput();
        }
    }

    // componentDidMount() {
    //     this.loadPayWay();
    // }

    // loadPayWay() {
    //     var params = {
    //         params: [{}]
    //     }
    //     $http.post('urlDeault', this, {
    //         code: "03040009",
    //         data: params,
    //         success: function (res) {
    //             if (res.errorCode === '0') {
    //                 this.setState({
    //                     payWay: res.data
    //                 })
    //             }
    //         }
    //     });
    // }

    closeModal() {
        let { onHide } = this.props;
        onHide && onHide();
        this.cleanInput();
    }

    //清空输入框
    cleanInput() {
        this.props.form.resetFields();
        this.setState({
            changeMoney: '',
            realPayMoney:0,
            payWayMoney:0,
        })
    }

    getNumElements(e) {
        if (e.keyCode == '13') {
            let { validateFields } = this.props.form;
            var { payWayId, payWayMoney, realPayMoney, hospId } = this.state;
            validateFields({ force: true }, (err, params) => {
                if (err) {
                    return;
                }
                if ((parseFloat(this.state.realPayMoney) + parseFloat(this.state.payWayMoney)) <
                    parseFloat(this.props.form.getFieldValue('shareFee'))) {
                    message.error('实收金额+其他支付金额不能小于应收金额')
                    return
                }
                let { onSubmit } = this.props;
                var payMoney = '';
                if (parseFloat(this.props.form.getFieldValue('changeMoney')) >= 0) {
                    payMoney = parseFloat(realPayMoney) - parseFloat(this.props.form.getFieldValue('changeMoney'))
                }
                params.accountBalance = this.props.registrSumData.accountBalance;
                onSubmit && onSubmit(params, payWayId, payWayMoney, payMoney, hospId);
            });
        }
    }

    //提交
    onSubmit() {
        let { validateFields } = this.props.form;
        var { payWayId, payWayMoney, realPayMoney, hospId } = this.state;
        validateFields({ force: true }, (err, params) => {
            if (err) {
                return;
            }
            if ((parseFloat(this.state.realPayMoney) + parseFloat(this.state.payWayMoney)) <
                parseFloat(this.props.form.getFieldValue('shareFee'))) {
                message.error('实收金额+其他支付金额不能小于应收金额')
                return
            }
            let { onSubmit } = this.props;
            var payMoney = '';
            if (parseFloat(this.props.form.getFieldValue('changeMoney')) >= 0) {
                payMoney = parseFloat(realPayMoney) - parseFloat(this.props.form.getFieldValue('changeMoney'))
            }
            params.accountBalance = this.props.registrSumData.accountBalance;
            onSubmit && onSubmit(params, payWayId, payWayMoney, payMoney, hospId);
        });
    }

    //其他支付方式金额，不能大于当前应收金额
    moneyInputChange(name, e) {
        this.state.payWayMoney = e.target.value;
        if (parseFloat(e.target.value) > parseFloat(Util.isEmpty(this.props.registrSumData) ? 0 : this.props.registrSumData.shareFee)) {
            message.error('支付金额不能大于应收金额，请重新输入！')
            return
        } else {
            this.setState({
                [name]: e.target.value,
                shareStatus: true
            })
        }
    }

    handleSelectChange(name, e) {
        this.setState({
            [name]: e,
            selectStatus: true
        })
    }

    commonInputChange(name, e) {
        this.state.realPayMoney = e.target.value;
        this.setState({
            [name]: e.target.value,
            realPayStatus: true
        })
    }

    handleMoney = (rule, val, callback) => {
        if (!val) {
            callback();
        }
        let validateResult = (parseFloat(this.state.realPayMoney) + parseFloat(this.state.payWayMoney)) < parseFloat(this.props.form.getFieldValue('shareFee'))
        if (validateResult) {
            callback('实收金额+其他支付金额不得小于应收金额');
        }
        callback();
    }

    render() {
        let { getFieldDecorator, getFieldValue } = this.props.form;
        let formItemLayout = {
            labelCol: { span: 9 },
            wrapperCol: { span: 15 }
        };

        let formItemMoneyLayout = {
            labelCol: { span: 5 },
            wrapperCol: { span: 15 }
        };

        //支付方式
        var payWayOption = []
        this.props.newPayWay && this.props.newPayWay.map((item, index) => {
            payWayOption.push(<Option key={index} value={item.paywID}>{item.paywDesc}</Option>)
        })

        //合作方式
        var contractUnitOption = []
        this.props.contractUnitData && this.props.contractUnitData.map((item, index) => {
            contractUnitOption.push(<Option key={index} value={item.id}>{item.descripts}</Option>)
        })

        return <div>
            <Modal className='registr-modal'
                title={'挂号'}
                maskClosable={false}
                visible={this.props.isShow} autoFocus
                onCancel={this.closeModal.bind(this)}
                // onOk={this.onSubmit.bind(this)}
                footer={<div><Button onClick={this.closeModal.bind(this)}>取消</Button>
                    <Button className='time-save-button' autoFocus
                        onkeyDown={this.getNumElements.bind(this)}
                        onClick={this.onSubmit.bind(this)}>确定</Button></div>}>
                <div style={{ display: 'flex' }}>
                    <div style={{ width: 350 }}>
                        <Form>
                            <FormItem {...formItemLayout} label='实际金额'>
                                {getFieldDecorator('price', {
                                    initialValue: Util.isEmpty(this.props.registrSumData) ? undefined : this.props.registrSumData.price
                                })(
                                    <Input placeholder='请输入实际金额' disabled={true} style={{ width: 150 }} />
                                )}
                            </FormItem>

                            <div style={{ display: 'flex' }}><div className='registr-modal-label'>院内账户余额：</div>
                                <div style={{ marginTop: 5 }}><InputGroup compact>
                                    <Input placeholder='院内账户余额' defaultValue={Util.isEmpty(this.props.registrSumData) ? undefined : this.props.registrSumData.accountBalance} disabled={true} style={{ width: 80 }} />
                                    <Input placeholder='余额：' disabled={true} style={{ width: 55, marginLeft: 5 }} />
                                    <Input placeholder='账户余额' disabled={true} style={{ width: 80 }} />
                                </InputGroup></div></div>
                            <div style={{ display: 'flex', marginTop: 10 }}><div className='registr-modal-label-div'>社保卡账户余额：</div>
                                <div style={{ marginTop: 5 }}><InputGroup compact>
                                    <Input placeholder='社保账户余额' defaultValue={Util.isEmpty(this.props.registrSumData) ? undefined : this.props.registrSumData.insuBalance} disabled={true} style={{ width: 80 }} />
                                    <Input placeholder='余额：' disabled={true} style={{ width: 55, marginLeft: 5 }} />
                                    <Input placeholder='账户余额' disabled={true} style={{ width: 80 }} />
                                </InputGroup></div></div>
                        </Form>
                    </div>
                    <div>
                        <Form>
                            <FormItem {...formItemLayout} label='减免金额'>
                                {getFieldDecorator('discount', {
                                    initialValue: Util.isEmpty(this.props.registrSumData) ? undefined : this.props.registrSumData.discount
                                })(
                                    <Input placeholder='请输入减免金额' disabled={true} style={{ width: 150 }} />
                                )}
                            </FormItem>

                            <FormItem {...formItemLayout} label='统筹金额'>
                                {getFieldDecorator('insuSum', {
                                    initialValue: Util.isEmpty(this.props.registrSumData) ? undefined : this.props.registrSumData.insuSum
                                })(
                                    <Input placeholder='请输入统筹金额' disabled={true} style={{ width: 150 }} />
                                )}
                            </FormItem>

                            <FormItem {...formItemLayout} label='应收金额'>
                                {getFieldDecorator('shareFee', {
                                    initialValue: Util.isEmpty(this.props.registrSumData) ? undefined : this.props.registrSumData.shareFee
                                })(
                                    <Input placeholder='请输入应收金额' disabled={true} style={{ width: 150 }} />
                                )}
                            </FormItem>
                        </Form></div>
                </div>
                <Form>
                    <FormItem {...formItemMoneyLayout} label='实收现金'>
                        {getFieldDecorator('realPayMoney', {
                            initialValue: Util.isEmpty(this.props.registrMsg) ? undefined : '',
                            rules: [{ validator: this.handleMoney }]
                        })(
                            <Input placeholder='请输入实收现金' style={{ width: 300 }} onChange={this.commonInputChange.bind(this, 'realPayMoney')} />
                        )}
                    </FormItem>
                    <FormItem {...formItemMoneyLayout} label='其他付款方式'>
                        {getFieldDecorator('realMoney', {
                            initialValue: Util.isEmpty(this.props.registrMsg) ? undefined : '',
                            rules: [{ validator: this.handleMoney }]
                        })(
                            <div style={{ display: 'flex' }}>
                                <Select placeholder='请选择支付方式' style={{ width: 150 }} getPopupContainer={triggerNode => triggerNode.parentNode}
                                    onChange={this.handleSelectChange.bind(this, 'payWayId')}>
                                    {payWayOption}
                                </Select>
                                <Input placeholder='请输入支付金额' style={{ width: 150, marginLeft: 10 }}
                                    disabled={!this.state.selectStatus}
                                    onChange={this.moneyInputChange.bind(this, 'payWayMoney')} />
                            </div>
                        )}
                    </FormItem>
                    <div style={{ display: 'flex' }}>
                        <FormItem {...formItemLayout} label='找零'>
                            {getFieldDecorator('changeMoney', {//找零算法：实收金额+其他支付方式金额-应收金额， 判断小于零时，找零为零，防止显示负数
                                initialValue: (this.state.realPayStatus || this.state.shareStatus) ?
                                    ((parseFloat(this.state.realPayMoney ? this.state.realPayMoney : 0) +
                                        parseFloat(this.state.payWayMoney ? this.state.payWayMoney : 0)) -
                                        parseFloat(Util.isEmpty(this.props.registrSumData) ? 0 : this.props.registrSumData.shareFee) > 0 ?
                                        (parseFloat(this.state.realPayMoney ? this.state.realPayMoney : 0) +
                                            parseFloat(this.state.payWayMoney ? this.state.payWayMoney : 0)) -
                                        parseFloat(Util.isEmpty(this.props.registrSumData) ? 0 : this.props.registrSumData.shareFee) : 0) : 0
                            })(
                                <Input placeholder='请输入找零数' disabled={true} style={{ width: 150 }} />
                            )}
                        </FormItem>
                        {/* <Checkbox style={{ marginTop: '10px', marginLeft: 40 }}></Checkbox> */}
                        <FormItem {...formItemLayout} label='合作医院' style={{ marginLeft: 40 }}>
                            {getFieldDecorator('hospitalId', {
                                initialValue: this.props.hospitalId
                            })(
                                <Select style={{ width: 150 }} disabled={true}
                                    onChange={Util.handleSelectChange.bind(this, 'hospId')}>
                                    {contractUnitOption}
                                </Select>
                            )}
                        </FormItem>
                    </div>
                </Form>
            </Modal></div>
    }
}

export default Form.create()(ConfirmRegistrModal);