/*
* 退号
* */

import React, { Component } from 'react';
import { Input, Form, Select, Modal, Button, Divider, Checkbox } 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';

const Option = Select.Option;
const FormItem = Form.Item;

class ShowBackNumModal extends Component {

    constructor(props) {
        super(props);
        var ipConfig = React.$getSessionData('ipConfig');
        var userData = React.$getSessionData();
        this.state = {
            ipConfig: ipConfig,
            userData: userData
        }
    }

    componentWillReceiveProps(nextProp) {
        if (nextProp.isShow === true) {
            this.loadOtherPayWay()
        } else if (this.props.isShow === false) {
            this.cleanInput();
        }
    }

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

    componentWillUnmount() {
		// 组件销毁前将静止让setState修改state的状态
		this.setState = (state, callback) => { return; }
    }

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

    loadOtherPayWay() {
        var params = {
            params: [{
                userID: this.state.userData.userID,
                groupID: this.state.userData.groupID,
                hospID: this.state.userData.hospID,
                locID: this.state.userData.locID,
                ip: this.state.ipConfig.ipv4,
                recordinvID: this.props.recordData.recordinvID,
                invtype: this.props.recordData.invtype,
                payType: 'R',

            }]
        }
        $http.post('urlDeault', this, {
            code: "05210028",
            data: params,
            success: function (res) {
                if (res.errorCode === '0') {
                    this.setState({
                        payModeInfo: res.payWData ? res.payWData[0].parkPaywinfo : [],
                        payWData: res.payWData
                    })
                }
            }
        });
    }

    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(name) {
        let { onHide } = this.props;
        onHide && onHide();
        this.cleanInput();
    }

    //清空输入框
    cleanInput() {
        this.props.form.resetFields();
    }
    handleChangeBackWay(name, e, code) {
        if (Util.isEmpty(e)) {
            this.setState({
                [name]: e,
                borderStyle: false,
                labelName: ''
            })
        } else {
            this.setState({
                [name]: e,
                borderStyle: true,
                labelName: code ? code.props.children : ''
            })
        }
    }
    //提交
    onSubmit() {
        let { validateFields } = this.props.form;
        validateFields({ force: true }, (err, params) => {
            if (err) {
                return;
            }
            let { onSubmit } = this.props;

            onSubmit && onSubmit(this.state.repaywinfo);
        });
    }

    changeHopsitalId(name, e) {
        var id = '';
        if (e) {
            id = e.substring(e.indexOf('^') + 1)
        }
        this.setState({
            [name]: id
        })
    }
    /**
     * 
                            {Util.isEmpty(this.state.labelName) ? <div style={{ marginTop: 5, lineHeight: '30px' }}>
                                院内账户：<Input style={{ width: 200 }}
                                    value={this.state.repaywinfo ? Util.isEmpty(this.props.registrFee) ? 0 :
                                        this.props.registrFee.recordTotalAmt : (Util.isEmpty(this.props.registrFee) ? 0 :
                                            this.props.registrFee.paywCashAmt)} addonBefore='¥'
                                    placeholder='院内账户' disabled={true} />
                            </div> : <div style={{ marginTop: 5, lineHeight: '30px' }}>
                                    <span>{this.state.labelName}：</span>
                                    <Input style={{ width: 200 }}
                                        value={this.state.repaywinfo ? Util.isEmpty(this.props.registrFee) ? 0 :
                                            this.props.registrFee.recordTotalAmt : (Util.isEmpty(this.props.registrFee) ? 0 :
                                                this.props.registrFee.paywCashAmt)} addonBefore='¥'
                                        placeholder='院内账户' disabled={true} />
                                </div>}
     */
    loadBackNumElement() {
        var { payWData } = this.state;
        var payModelOption = []
        payWData && payWData.map((item, index) => {
            payModelOption.push(<div style={{ marginTop: 5, lineHeight: '30px' }} key={index}>
                {item.oldPaywDesc}：<Input style={{ width: 200 }}
                    value={item.oldPaywAmt} addonBefore='¥'
                    placeholder='院内账户' disabled={true} />
            </div>
            )
        })
        return payModelOption
    }

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

        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.state.payWay && this.state.payWay.map((item, index) => {
            payWayOption.push(<Option key={index} value={item.id}>{item.descripts}</Option>)
        })
        //其他方式
        var otherPayWayOption = []
        this.state.payModeInfo && this.state.payModeInfo.map((item, index) => {
            otherPayWayOption.push(<Option key={index} value={item.paywID}>{item.paywDesc}</Option>)
        })

        return <div>
            <Modal className='registr-back-modal'
                title={'退号'}
                maskClosable={false}
                visible={this.props.isShow}
                onCancel={this.closeModal.bind(this)}
                onOk={this.onSubmit.bind(this)}>
                <div>
                    <div><Divider dashed={false}>退回方式及金额</Divider></div>
                    <div>退回方式：<Select placeholder='默认原路退回' style={{ width: 200 }} getPopupContainer={triggerNode => triggerNode.parentNode}
                        allowClear
                        onChange={this.handleChangeBackWay.bind(this, 'repaywinfo')}>
                        {otherPayWayOption}
                    </Select>
                        <span style={{ marginLeft: 30 }}>应退金额：<Input style={{ width: 200 }}
                            value={this.state.repaywinfo ? Util.isEmpty(this.props.registrFee) ? undefined :
                                this.props.registrFee.recordTotalAmt : (Util.isEmpty(this.props.registrFee) ? undefined :
                                    this.props.registrFee.paywCashAmt)} addonBefore='¥'
                            placeholder='请输入可退金额' disabled={true} /></span>
                    </div>

                    {this.state.repaywinfo ? null : <div>
                        <div><Divider dashed={false}>应退明细</Divider></div>
                        {this.loadBackNumElement()}
                    </div>}
                </div>
            </Modal></div>
    }
}

export default Form.create()(ShowBackNumModal);