// 跳号
import React from 'react';
import { Input, Row, Col, Button, message } from 'antd';
import { $http } from 'containers/config/https';
import '../style/style.less'
import { Keup, ArraySome } from 'tools/index';
const { TextArea } = Input;

export default class HopNumber extends React.Component {
    constructor(props) {
        super(props)
        this.state = {
            currentInvNo: '', // 当前发票号
            preFix: '',// 当前发票号前缀
            currentNo: '',  // 当前发票号
            endNo: '',  // 当前最大
            jumpReason: '',  // 原因
            invTypeCode: '', // 类型
            copyCurrentNo: '',
        }
    }

    componentDidMount() {
        this.setState({
            invTypeCode: this.props.invTypeCode || ''
        }, () => {
            this.inv() // 获取发票
        })
    }

    // 实时更新
    componentWillReceiveProps(nextProps) {
        this.setState({
            invTypeCode: this.props.invTypeCode || ''
        }, () => {
            this.inv() // 获取发票
        })

    }

    // 获取发票
    inv = () => {
        let data = {
            params: [{
                "hospID": JSON.parse(window.sessionStorage.userData).hospID,
                "userID": JSON.parse(window.sessionStorage.userData).userID, //人员id,
                "invTypeCode": this.state.invTypeCode,
            }]
        }

        $http.post('urlADS', this, {
            code: "05100115",
            data: data,
            success: function (res) {
                this.setState({
                    disBtton: false, // 按钮点击许可
                })
                let errorCode = res.errorCode;
                if (errorCode === '0') {
                    this.setState({
                        currentInvNo: res.invUserInfo[0].currentInvNo,  // 当前发票号
                        copyCurrentNo: res.invUserInfo[0].currentNo,  // 当前发票号
                        currentNo: res.invUserInfo[0].currentNo,  // 当前发票号
                        preFix: res.invUserInfo[0].preFix,  // 当前发票号前缀
                        endNo: res.invUserInfo[0].endNo,  // 当前最大
                    });
                }
            }
        });
    }

    // sum
    sum = (type) => {
        let { currentNo, preFix, endNo, copyCurrentNo } = this.state;
        let currentNoNew = currentNo // 新值
        currentNoNew = currentNoNew.replace(/\b(0+)/g, "")
        if (currentNoNew === '') {
            currentNoNew = 0
        }
        if (type === 'add') {
            currentNoNew = parseFloat(currentNoNew) + 1;
        }
        if (type === 'red') {
            currentNoNew = parseFloat(currentNoNew) - 1;
        }
        currentNoNew = currentNoNew.toString()
        let length = Keup.getBLen(currentNo.toString()) - Keup.getBLen(currentNoNew.toString());
        if (length > 0) {
            currentNoNew = ArraySome.addPreZero(currentNoNew, Keup.getBLen(currentNo.toString()));
        }
        if (parseFloat(currentNoNew) > parseFloat(endNo) || parseFloat(currentNoNew) < parseFloat(copyCurrentNo)) {
            message.error('跳至号段不属于当前使用发票号段：' + preFix + copyCurrentNo + '~' + preFix + endNo);
        }
        this.setState({
            currentNo: currentNoNew
        });
    }

    // 减
    red = () => {
        this.sum('red');
    }

    // 加
    add = () => {
        this.sum('add');
    }

    currentNo = (e) => {
        this.setState({ currentNo: Keup.MoneyReplace(e.target.value) });
    }

    jumpReason = (e) => {
        this.setState({ jumpReason: e.target.value });
    }

    current = () => {
        let data = {
            params: [{
                "hospID": JSON.parse(window.sessionStorage.userData).hospID,
                "userID": JSON.parse(window.sessionStorage.userData).userID, // 人员id,
                "paytype": this.state.invTypeCode,
                "staInvNo": this.state.currentInvNo,
                "endInvNo": this.state.preFix + this.state.currentNo,
                "jumpReason": this.state.jumpReason
            }]
        }
        $http.post('urlADS', this, {
            code: "05210027",
            data: data,
            success: function (res) {
                this.setState({
                    disBtton: false, // 按钮点击许可
                })
                let errorCode = res.errorCode;
                if (errorCode === '0') {
                    message.success('成功')
                    this.props.handleCancel();
                    if (this.props && this.props.initJuris) {
                        this.props.initJuris && this.props.initJuris()
                    } else {
                        this.props && this.props.handelThis && this.props.handelThis?.initJuris();
                    }
                }
            }
        });
    }

    handleBlur = (e) => {
        console.log('e.target.value', e.target.value)
        let { preFix, endNo, copyCurrentNo } = this.state;
        if (parseFloat(e.target.value) > parseFloat(endNo) || parseFloat(e.target.value) < parseFloat(copyCurrentNo)) {
            message.error('跳至号段不属于当前使用发票号段：' + preFix + copyCurrentNo + '~' + preFix + endNo);
        }
    }

    render() {
        return (
            <div>
                <Row style={{ fontSize: '18px' }}>
                    <Col span={24} >
                        当前发票号：<span style={{ color: '#00ff99' }}>{this.state.currentInvNo}</span>
                    </Col>
                    <Col span={24} className='mT20'>
                        发票号码：<Button className='sumBox' onClick={() => this.red()} >-</Button>
                        <Input addonBefore={this.state.preFix} onChange={(event) => { this.currentNo(event) }} value={this.state.currentNo} style={{ width: '200px', margin: '0 20px' }} onBlur={this.handleBlur} />
                        <Button className='sumBox' onClick={() => this.add()}  >+</Button>
                    </Col>
                    <Col span={24} className='mT20'>
                        <Col span={4}>跳号原因： </Col>
                        <Col span={20} ><TextArea rows={3} onChange={(event) => { this.jumpReason(event) }} value={this.state.jumpReason} /> </Col>
                    </Col>
                    <Col span={24} className='mT20' style={{ color: 'rgb(240, 65, 52)' }}>
                        只能在当前分配发票序列号中跳号，点击确定后调节到该发票号
                    </Col>
                    <Col span={12} offset={6} className='mT20'  >
                        <Button style={{ background: 'rgba(66,185,131,1)', color: '#fff', width: '100%', }} onClick={() => { this.current() }}>确定</Button>
                    </Col>
                </Row>
            </div >

        )
    }
}