/**
 * 设置发票号
 * ShowInvoiceNumModal
 */

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

const { TextArea } = Input;

class ShowInvoiceNumModal extends Component {
    constructor(props) {
        super(props);
        var userData = JSON.parse(sessionStorage.getItem('userData'))
        this.state = {
            userData: userData,
            currentNo: 0
        }
    }

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

    // componentDidMount() {
    // }

    loadData() {
        var params = {
            params: [{
                hospID: this.state.userData.hospID,
                userID: this.state.userData.userID,
                invTypeCode: 'R',
            }]
        }
        $http.post('urlDeault', this, {
            code: "05100115",
            data: params,
            success: function (res) {
                if (res.errorCode == '0') {
                    this.setState({
                        invUserInfo: res.invUserInfo,
                        currentNo: res.invUserInfo[0].currentNo,
                        endNo: res.invUserInfo[0].endNo,
                    })
                }
            }
        });
    }

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

    //清空输入框
    cleanInput() {
        this.setState({
            jumpReason:'',
            currentNo:this.state.currentNo
        })
    }

    //减
    reduceInvoiceNum() {
        var { invUserInfo } = this.state;
        // this.setState({
        //     currentNo: parseInt(this.state.currentNo) - 1
        // },()=>{
        if (parseInt(this.state.currentNo) - 1 < parseInt(invUserInfo[0].currentNo)) {
            this.setState({
                currentNo: parseInt(invUserInfo[0].currentNo)
            })
        } else {
            this.setState({
                currentNo: parseInt(this.state.currentNo) - 1
            })
        }
    }

    addInvoiceNum() {
        if (parseInt(this.state.currentNo) < this.state.endNo) {
            this.setState({
                currentNo: parseInt(this.state.currentNo) + 1
            })
        } else {
            return
        }
    }

    //提交
    onSubmit() {
        let { onSubmit } = this.props;

        onSubmit && onSubmit(this.state.currentNo,this.state.jumpReason,this.state.invUserInfo);
    }

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

        return <Modal className='registr-modal'
            title={'设置发票号'}
            maskClosable={false}
            visible={this.props.isShow}
            onCancel={this.closeModal.bind(this)}
            onOk={this.onSubmit.bind(this)}>
            <div>
                <h4><span style={{ color: '#666666' }}>当前发票号：</span>
                    <span style={{ color: '#1999db' }}>{this.props.currentInvNo ? this.props.currentInvNo : ''}</span></h4>
                <Icon type='minus-circle' disabled={true} onClick={this.reduceInvoiceNum.bind(this)} />
                <Input disabled={true} value={this.state.currentNo} style={{ width: 120, marginLeft: 5 }} />
                <Icon type='plus-circle' style={{ marginLeft: 5 }} onClick={this.addInvoiceNum.bind(this)} />
            </div>
            <div style={{ marginTop: 10 }}>跳号理由：<TextArea rows={4} placeholder='请输入跳号理由' value={this.state.jumpReason}
                onChange={Util.commonInputChange.bind(this, 'jumpReason')}
                style={{ marginTop: 5 }} maxLength={100} /></div>
            <h4 style={{ color: 'red' }}>只能在当前分配发票序列中跳号，点击确定后调节到该发票号</h4>
        </Modal>
    }
}

export default Form.create()(ShowInvoiceNumModal);