/*
* 挂号
* */

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

const Option = Select.Option;
const FormItem = Form.Item;
const statusImg = require('../image/status.png')

class ShowRegistrModal extends Component {

    constructor(props) {
        super(props);
        this.state = {
            checked: false
        }
        this.inputModal = React.createRef();
        this.selectModal = React.createRef();
    }

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

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

    componentDidMount() {
        this.loadDepartHospital();
    }

    loadDepartHospital() {
        var params = {
            params: [{
            }]
        }
        $http.post('urlDeault', this, {
            code: "01040060",
            data: params,
            success: function (res) {
                if (res.errorCode === '0') {
                    this.setState({
                        contractUnitData: res.contractUnit
                    })
                }
            }
        });
    }

    closeInputModal() {
        this.setState({
            [name]: false,
            realPayMoney: '',
            payWayMoney: 0
        })
        this.cleanInput();
        let { onHide } = this.props;
        onHide && onHide();
    }

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

    handleSelectChange(name, e) {
        var id = '', code = '';
        if (e) {
            id = e.substring(0, e.indexOf('^'))
            code = e.substring(e.indexOf('^') + 1)
        }
        this.setState({
            [name]: id ? id : e,
            ccpCode: code,
            selectStatus: true
        }, () => {
            this.inputModal.current.buttonNode.focus()
        })
    }

    commonInputChange(name, e) {
        var value = e.target.value;
        var reg = /(^[1-9][0-9]*$)|(^[1-9][0-9]*\.\d{0,2}$)|^0(?!\d)\.?\d{0,2}$|^[1-9]?$/;
        if (!reg.test(value)) {
            message.error('只允许输入正整数或者两位小数！')
            return
        }
        this.state.realPayMoney = e.target.value;
        this.setState({
            [name]: e.target.value,
            realPayStatus: true
        })
    }

    //其他支付方式金额，不能大于当前应收金额
    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
            })
        }
    }

    checkAccount(e) {
        this.setState({
            checkAccount: e.target.checked
        })
    }

    getFocutBtn(e) {
        var payWayCount = 0;//其他支付方式金额
        var realPayCount = 0;//现金支付金额
        let { onSubmitRegastr } = this.props;
        var registrSumData = this.props.registrSumData;
        var { payWayMoney, realPayMoney } = this.state;


        //先计算院内账户的总额
        var realAccount = parseFloat(registrSumData.price) - parseFloat(registrSumData.discount) - parseFloat(registrSumData.insuSum) -
            parseFloat(registrSumData.insuBalance) - parseFloat(registrSumData.accountFee);
        if (parseInt(realPayMoney) >= parseFloat(realAccount)) {
            this.inputModal.current.buttonNode.focus();
        } else {
            this.selectModal.current.focus()
        }

        // realPayCount = parseFloat(realPayMoney) - parseFloat(registrSumData.discount) - parseFloat(registrSumData.insuSum) -
        //     parseFloat(registrSumData.insuBalance)
        // if (parseFloat(realPayCount) >= parseFloat(registrSumData.price) - parseFloat(registrSumData.discount) - parseFloat(registrSumData.insuSum) -
        //     parseFloat(registrSumData.insuBalance)) {
        //     this.inputModal.current.buttonNode.focus();
        // } else {
        //     this.selectModal.current.focus()
        // }
    }

    onSubmitRegastr(e) {
        var payWayCount = 0;//其他支付方式金额
        var realPayCount = 0;//现金支付金额
        let { onSubmitRegastr } = this.props;
        var registrSumData = this.props.registrSumData;
        var { payWayMoney, realPayMoney } = this.state;
        if (this.state.checkAccount) {//不使用院内账户
            //先计算金额
            var price = parseFloat(registrSumData.price) - parseFloat(registrSumData.discount) - parseFloat(registrSumData.insuSum) -
                parseFloat(registrSumData.insuBalance)
            if (parseInt(realPayMoney) >= parseFloat(price)) {
                realPayCount = price;
                payWayCount = 0;
            } else {
                realPayCount = parseInt(realPayMoney);
                payWayCount = parseFloat(registrSumData.price) - parseFloat(registrSumData.discount) - parseFloat(registrSumData.insuSum) -
                    parseFloat(registrSumData.insuBalance) - parseInt(realPayMoney);
            }
        } else {
            //先计算院内账户的总额
            var payCount = parseFloat(registrSumData.discount) + parseFloat(registrSumData.insuSum) +
                parseFloat(registrSumData.insuBalance) + parseFloat(registrSumData.accountFee)
            var realAccount = parseFloat(registrSumData.price) - parseFloat(registrSumData.discount) - parseFloat(registrSumData.insuSum) -
                parseFloat(registrSumData.insuBalance) - parseFloat(registrSumData.accountFee);
            if (payCount >= parseFloat(registrSumData.price)) {
                realPayCount = 0
                payWayCount = 0
            } else {
                if (parseInt(realPayMoney) >= parseFloat(realAccount)) {
                    realPayCount = realAccount
                    payWayCount = 0
                } else {
                    realPayCount = parseInt(realPayMoney);
                    payWayCount = parseFloat(registrSumData.price) - parseFloat(registrSumData.discount) - parseFloat(registrSumData.insuSum) -
                        parseFloat(registrSumData.insuBalance) - parseFloat(registrSumData.accountFee) - parseInt(realPayMoney);
                }
            }
        }

        onSubmitRegastr && onSubmitRegastr(this.props.registrSumData, this.state.payWayId,
            payWayCount, realPayCount, this.state.hospId, this.state.checkAccount);
    }

    render() {
        let { getFieldDecorator, getFieldValue } = this.props.form;

        var contractUnitOption = []
        this.state.contractUnitData && this.state.contractUnitData.map((item, index) => {
            contractUnitOption.push(<Option key={index} value={item.descripts + '^' + item.id}>{item.descripts}</Option>)
        })

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

        return <div>
            {/* <ConfirmRegistrModal isShow={this.state.isConfirmRegistrModal}
                hospitalId={this.state.hospitalId}
                newPayWay={this.props.newPayWay}
                contractUnitData={this.state.contractUnitData}
                registrSumData={this.props.registrSumData}
                onSubmit={this.onSubmitRegastr.bind(this)}
                onHide={this.closeModal.bind(this, 'isConfirmRegistrModal')}
            ></ConfirmRegistrModal> */}
            <Modal className='registr-confirm-modal'
                title={'挂号收费'}
                maskClosable={false}
                visible={this.props.isShow}
                confirmLoading={this.props.confirmLoading}
                onCancel={this.closeInputModal.bind(this)}
                // onOk={this.onSubmitRegastr.bind(this)}
                footer={<div><Button onClick={this.closeInputModal.bind(this)}>取消</Button>
                    <Button className='time-save-button' ref={this.inputModal} type='primary'
                        disabled={this.props.confirmLoading}
                        // onkeyDown={this.onkeySubmit.bind(this)}
                        onClick={this.onSubmitRegastr.bind(this)}>确定</Button></div>}
            ><Spin spinning={this.props.confirmLoading}>
                <div>
                    <div style={{ display: 'flex' }}><div className='border-radius-registr'></div>
                        <div style={{ marginLeft: 10, fontWeight: 'bold' }}>挂号信息</div></div>
                    <span>{this.props.patientMsg ? this.props.patientMsg.patNo : undefined}</span><Divider type="vertical" />
                    <span>{this.props.patientMsg ? this.props.patientMsg.patName : undefined}</span><Divider type="vertical" />
                    <span>{this.props.patientMsg ? this.props.patientMsg.patSexDesc : undefined}</span><Divider type="vertical" />
                    <span>{this.props.patientMsg ? this.props.patientMsg.patAge : undefined}</span><Divider type="vertical" />
                    <span>居民身份证&nbsp;&nbsp;{this.props.patientMsg ? this.props.patientMsg.patCredNo : undefined}</span><Divider type="vertical" />
                    <span>{this.props.patientMsg ? this.props.patientMsg.patMobile : undefined}</span>
                </div>
                <div style={{ marginTop: 20 }}>
                    <div style={{ float: 'left', marginLeft: 30 }}>
                        <span><span style={{ background: '#F5F4F4' }}>挂号科室：</span>
                            <span>{Util.isEmpty(this.props.registrMsg) ? undefined : this.props.registrMsg.locDescripts}</span>
                        </span><br />

                        <span><span style={{ background: '#F5F4F4' }}>挂号医生：</span>
                            <span>{Util.isEmpty(this.props.registrMsg) ? undefined : this.props.registrMsg.docName}</span>
                        </span><br />
                        <span><span style={{ background: '#F5F4F4' }}>挂号类型：</span>
                            <span>{Util.isEmpty(this.props.registrMsg) ? undefined : this.props.registrMsg.regTitle}</span>
                        </span>
                    </div>

                    <div style={{ float: 'right', marginRight: '22%' }}>
                        <span><span style={{ background: '#F5F4F4' }}>挂号费(元)：</span>
                            <span style={{ color: '#FA8C16' }}>{Util.isEmpty(this.props.registrMsg) ? undefined :
                                this.props.registrMsg.regFee}</span>
                        </span><br />
                        <span><span style={{ background: '#F5F4F4' }}>诊疗费(元)：</span>
                            <span style={{ color: '#FA8C16' }}>{Util.isEmpty(this.props.registrMsg) ? undefined :
                                this.props.registrMsg.examinFee}</span>
                        </span>
                    </div>
                </div><br />

                <div style={{ marginTop: 55 }}>
                    <div style={{ display: 'flex' }}><div className='border-radius-fee'></div>
                        <div style={{ marginLeft: 10, fontWeight: 'bold' }}>收费信息</div></div>
                    <div style={{ float: 'left', marginLeft: 30 }}>
                        <span><span style={{ background: '#F5F4F4' }}>实际金额(元)：</span>
                            <span style={{ color: '#FA8C16' }}>{Util.isEmpty(this.props.registrSumData) ? undefined :
                                this.props.registrSumData.price}</span>
                        </span><br />

                        <span><span style={{ background: '#F5F4F4' }}>统筹金额(元)：</span>
                            <span style={{ color: '#FA8C16' }}>{Util.isEmpty(this.props.registrSumData) ? undefined :
                                this.props.registrSumData.insuSum}</span>
                        </span><br />
                        <span><span style={{ background: '#F5F4F4' }}>院内账户(元)：</span>
                            <span style={{ color: '#FA8C16' }}>{Util.isEmpty(this.props.registrSumData) ? undefined :
                                this.props.registrSumData.accountFee}</span>
                        </span>
                        <span style={{ marginLeft: 10 }}>账户余额(元):<span style={{ color: '#FA8C16' }}>{Util.isEmpty(this.props.registrSumData) ?
                            undefined : this.props.registrSumData.accountBalance}</span>
                            {this.props.registrSumData && this.props.registrSumData.accountBalanceCode == 'N' ? <img src={statusImg} style={{ marginLeft: 5 }} /> : null}
                            <Checkbox style={{ marginLeft: 10 }} onChange={this.checkAccount.bind(this)} /><span>不使用账户</span>
                        </span>
                        <br />
                        <span><span style={{ background: '#F5F4F4' }}>社保账户(元)：</span>
                            <span style={{ color: '#FA8C16' }}>{Util.isEmpty(this.props.registrSumData) ? undefined : this.props.registrSumData.insuBalance}</span>
                            <span style={{ marginLeft: 10 }}>账户余额(元)：<span style={{ color: '#FA8C16' }}>{Util.isEmpty(this.props.registrSumData) ?
                                undefined : this.props.registrSumData.insuBalance}</span></span>
                        </span>
                    </div>
                    <div style={{ float: 'right', marginRight: '10%' }}>
                        <span><span style={{ background: '#F5F4F4' }}>减免金额(元)：</span>
                            <span style={{ color: '#FA8C16' }}>{Util.isEmpty(this.props.registrSumData) ? undefined :
                                this.props.registrSumData.discount}</span>
                        </span>
                    </div>
                </div>
                <div style={{ marginTop: '15%', textAlign: 'center' }}>
                    {/* <Card> */}
                    <Card.Grid style={{ marginLeft: '12%', display: 'flex' }}>
                        <div className='registr-modal-fee'></div><div>剩余应收(元)</div><div style={{ color: '#FA8C16', marginLeft: 10 }}>
                            {/**实收金额-统筹金额-减免-院内金额 */}
                            {this.state.checkAccount ? (this.props.registrSumData ? ((parseFloat(this.props.registrSumData.price) -
                                parseFloat(this.props.registrSumData.discount) - parseFloat(this.props.registrSumData.insuSum) -
                                0)).toFixed(2) : 0)
                                : (this.props.registrSumData ? (parseFloat(this.props.registrSumData.accountFee) -
                                    ((parseFloat(this.props.registrSumData.price) -
                                        parseFloat(this.props.registrSumData.discount) - parseFloat(this.props.registrSumData.insuSum) -
                                        0)) > 0 ? 0 : Math.abs(parseFloat(this.props.registrSumData.accountFee) -
                                            ((parseFloat(this.props.registrSumData.price) -
                                                parseFloat(this.props.registrSumData.discount) - parseFloat(this.props.registrSumData.insuSum) -
                                                0)))).toFixed(2) : 0)}
                        </div></Card.Grid>
                    <Card.Grid style={{ marginLeft: 20, display: 'flex' }}>
                        <div className='registr-fee-change'></div><div>找零(元)</div><div style={{ color: '#FA8C16', marginLeft: 10 }}>
                            {/** */}
                            {(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.state.checkAccount ? (this.props.registrSumData ? ((parseFloat(this.props.registrSumData.price) -
                                        parseFloat(this.props.registrSumData.discount) - parseFloat(this.props.registrSumData.insuSum) -
                                        0)) : 0)
                                        : (this.props.registrSumData ? (parseFloat(this.props.registrSumData.accountFee) -
                                            ((parseFloat(this.props.registrSumData.price) -
                                                parseFloat(this.props.registrSumData.discount) - parseFloat(this.props.registrSumData.insuSum) -
                                                0)) > 0 ? 0 : Math.abs(parseFloat(this.props.registrSumData.accountFee) -
                                                    ((parseFloat(this.props.registrSumData.price) -
                                                        parseFloat(this.props.registrSumData.discount) - parseFloat(this.props.registrSumData.insuSum) -
                                                        0)))) : 0))) > 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.state.checkAccount ? (this.props.registrSumData ? ((parseFloat(this.props.registrSumData.price) -
                                        parseFloat(this.props.registrSumData.discount) - parseFloat(this.props.registrSumData.insuSum) -
                                        0)) : 0)
                                        : (this.props.registrSumData ? (parseFloat(this.props.registrSumData.accountFee) -
                                            ((parseFloat(this.props.registrSumData.price) -
                                                parseFloat(this.props.registrSumData.discount) - parseFloat(this.props.registrSumData.insuSum) -
                                                0)) > 0 ? 0 : Math.abs(parseFloat(this.props.registrSumData.accountFee) -
                                                    ((parseFloat(this.props.registrSumData.price) -
                                                        parseFloat(this.props.registrSumData.discount) - parseFloat(this.props.registrSumData.insuSum) -
                                                        0)))) : 0))) : 0).toFixed(2) : 0}
                        </div></Card.Grid>
                    {/* </Card> */}
                </div>
                <div style={{ marginTop: '30%' }}>
                    <div style={{ marginLeft: '4%' }}>支付方式：<Select placeholder='请选择支付方式' style={{ width: 200 }} getPopupContainer={triggerNode => triggerNode.parentNode}
                        value='现金' disabled={true}
                        onChange={this.handleSelectChange.bind(this, 'payWayId')}>
                        {payWayOption}</Select>
                        <Input style={{ width: 200, marginLeft: 10 }} addonBefore='¥' value={this.state.realPayMoney}
                            autoFocus onPressEnter={this.getFocutBtn.bind(this)}
                            onChange={this.commonInputChange.bind(this, 'realPayMoney')} /></div>
                    <div style={{ marginTop: 5 }}>
                        其他支付方式：<Select placeholder='请选择支付方式' style={{ width: 200 }} getPopupContainer={triggerNode => triggerNode.parentNode}
                            ref={this.selectModal} value={this.state.payWayId}
                            onChange={this.handleSelectChange.bind(this, 'payWayId')}>
                            {payWayOption}</Select>
                        <Input disabled={true} style={{ width: 200, marginLeft: 10 }} value={((this.state.checkAccount ?
                            (this.props.registrSumData ? ((parseFloat(this.props.registrSumData.price) -
                                parseFloat(this.props.registrSumData.discount) - parseFloat(this.props.registrSumData.insuSum) -
                                0)).toFixed(2) : 0)
                            : (this.props.registrSumData ? (parseFloat(this.props.registrSumData.accountFee) -
                                ((parseFloat(this.props.registrSumData.price) -
                                    parseFloat(this.props.registrSumData.discount) - parseFloat(this.props.registrSumData.insuSum) -
                                    0)) > 0 ? 0 : Math.abs(parseFloat(this.props.registrSumData.accountFee) -
                                        ((parseFloat(this.props.registrSumData.price) -
                                            parseFloat(this.props.registrSumData.discount) - parseFloat(this.props.registrSumData.insuSum) -
                                            0)))).toFixed(2) : 0)) - parseFloat(this.state.realPayMoney) > 0 ?
                            (this.state.checkAccount ?
                                (this.props.registrSumData ? ((parseFloat(this.props.registrSumData.price) -
                                    parseFloat(this.props.registrSumData.discount) - parseFloat(this.props.registrSumData.insuSum) -
                                    0)).toFixed(2) : 0)
                                : (this.props.registrSumData ? (parseFloat(this.props.registrSumData.accountFee) -
                                    ((parseFloat(this.props.registrSumData.price) -
                                        parseFloat(this.props.registrSumData.discount) - parseFloat(this.props.registrSumData.insuSum) -
                                        0)) > 0 ? 0 : Math.abs(parseFloat(this.props.registrSumData.accountFee) -
                                            ((parseFloat(this.props.registrSumData.price) -
                                                parseFloat(this.props.registrSumData.discount) - parseFloat(this.props.registrSumData.insuSum) -
                                                0)))).toFixed(2) : 0)) - parseFloat(this.state.realPayMoney) : 0).toFixed(2)}

                            addonBefore='¥' />
                    </div>
                    {this.state.ccpCode == 'CCP' ? <div style={{ marginTop: 5, marginLeft: '4%' }}>
                        合作单位：<Select style={{ width: 150 }}
                            onChange={Util.handleSelectChange.bind(this, 'hospId')}>
                            {contractUnitOption}
                        </Select>
                    </div> : null}
                </div></Spin>
            </Modal></div>
    }
}

export default Form.create()(ShowRegistrModal);