import React from 'react';
import Button from '../../CommonComponent/ButtonComponent/ButtonComponent'
import Header from '../../CommonComponent/HeaderComponent/HeaderComponent';
import PopModel from '../../CommonComponent/PopupComponent/PopupChildComponent'
require('./index.css')
export default class ReplenishScanPage extends React.Component{
    constructor(props){
        super(props);
        this.getOtherHeader = this.getOtherHeader.bind(this)
        this.getBarcodeInput = this.getBarcodeInput.bind(this)
        this.errorSku = this.errorSku.bind(this)
        this.forgetSku = this.forgetSku.bind(this)
        this.outReplenish = this.outReplenish.bind(this)
        this.getSkuInfo = this.getSkuInfo.bind(this)
        this.submitBarcode = this.submitBarcode.bind(this);
        this.failSubmit = this.failSubmit.bind(this);
        this.changeShowInput = this.changeShowInput.bind(this);
        this.getScanDom = this.getScanDom.bind(this);
        this.hidePop = this.hidePop.bind(this);
        this.showErrorInfo = this.showErrorInfo.bind(this);
        this.state = {
            barcodeText : '',
            barcodeErrorText : '',
            showInput : false
        }
    }

    shouldComponentUpdate(nextProps,nextState){
        return !!(nextProps.replenishInfo && nextProps.replenishInfo.errorInfo && (
            !nextProps.replenishInfo.errorInfo.errorMessage || !this.props.replenishInfo || !this.props.replenishInfo.errorInfo ||
            !this.props.replenishInfo.errorInfo.errorMessage || (this.props.replenishInfo.errorInfo.errorMessage !== nextProps.replenishInfo.errorInfo.errorMessage)
        ) || (JSON.stringify(nextState) !== JSON.stringify(this.state)))
    }

    componentWillReceiveProps(nextProps){
        console.log("ReplenishScanPage componentWillReceiveProps ",nextProps);

        let props = this.props;
        let {replenishInfo,barcodeInfo} = props;

        if(JSON.stringify(this.props) !== JSON.stringify(nextProps)){

            if(nextProps.replenishInfo && nextProps.replenishInfo.errorInfo && nextProps.replenishInfo.errorInfo.errorMessage){
                //有错误信息，再对应页面弹出错误信息弹窗，要区分错误信息
                // nextProps.hidePopup();
                if(!replenishInfo.errorInfo || !replenishInfo.errorInfo.errorMessage || (replenishInfo.errorInfo && replenishInfo.errorInfo.errorMessage && replenishInfo.errorInfo.errorMessage !== nextProps.replenishInfo.errorInfo.errorMessage)){
                    if(nextProps.replenishInfo.errorInfo.errorType){
                        switch (nextProps.replenishInfo.errorInfo.errorType) {
                            case "NT":
                            case "NA":
                                this.outReplenish();
                                break;
                            case "NS":
                                this.errorSku();
                                break;
                        }
                    }else{
                        //没有错误类型的错误
                        console.log("ReplenishScanPage ",nextProps.replenishInfo.errorInfo.errorMessage)
                        this.showErrorInfo(nextProps.replenishInfo.errorInfo.errorMessage);
                    }
                }
            }else if(nextProps.barcodeInfo && nextProps.barcodeInfo.product && nextProps.barcodeInfo.product.skuId && (
                !this.props.barcodeInfo || !this.props.barcodeInfo.product || !this.props.barcodeInfo.skuId ||
                this.props.barcodeInfo.skuId !== nextProps.barcodeInfo.product.skuId
            )){
                this.goBack.bind(this)(2)
            }
        }
    }

    componentDidMount(){
        this._startScan()
    }

    showErrorInfo(errMsg){
        let {showPopup,hidePopup,barcodeInfo} = this.props;
        let {product} = barcodeInfo;
        let btnDom = <Button btnContainer={"replenishScanPageErrorSkuBtn font32 backff775c colfff"} key={"replenishScanPageErrorSkuBtn"} text={"确定"} option={this.hidePop}/>
        let popDom =
            <PopModel popupTitle={"扫描出错"} classContainer={"replenishPopInfo replenishScanPageErrorInfo"}>
                <div className={"icon iconfont icon-exclamation colf5a623 font150"} />
                <div className={"text font40 col000"}>
                    <p>{errMsg}</p>
                </div>
                <div className={"btn"}>
                    {btnDom}
                </div>
            </PopModel>
        showPopup({popupChild:popDom});
    }

    goBack(type){
        let {emptyErrorInfo,goBack,hidePopup} = this.props;
        emptyErrorInfo();
        hidePopup();
        goBack(type)
    }

    hidePop(){
        let {emptyErrorInfo,hidePopup} = this.props;
        emptyErrorInfo();
        hidePopup();
        this.setState({
            barcodeText : ''
        });
        if(!this.state.showInput){
            this._startScan()
        }
    }

    getOtherHeader(){
        let {headerInfo} = this.props;
        let {taskInfo={}} = headerInfo;
        return <div className={"otherHeader replenishScanQrcodeHeader font32 clearfix colfff "}>
            <i className={"iconfont middle icon font50 icon-circle-left circleIcon fl"} onClick={()=>this.goBack.bind(this)(1)}/>
            <span className={"middle"}>任务目标：将{taskInfo.area || ""}区补货箱中的货品扫码后放到货架上</span>
        </div>
    }

    handleInputChange(e){
        this.setState({
            barcodeText:e.target.value
        })
    }

    getBarcodeInput(){
        return (
            <div className={"barcodeInputDom"}>
                <div className={"barcodeInputText font30"}>{this.state.barcodeErrorText}</div>
                <input value={this.state.barcodeText} className={"barcodeInput font30 col000"} type="text" onChange={this.handleInputChange.bind(this)}/>
                <div className={"barcodeInputTip font30 colff775c"} onClick={()=>this.changeShowInput()}><span className={"colfff"}>or</span>条形码扫描</div>
                <Button btnContainer={"barcodeInputBarcode font32 backff775c colfff"} key={"barcodeInputBarcode"} text={"确定"} option={this.submitBarcode}/>
            </div>
        )
    }


    //扫描货品异常
    errorSku(){
        //货道已满解决办法
        let {showPopup,hidePopup,headerInfo} = this.props;
        let {taskList={}} = headerInfo;
        let btnDom = <Button btnContainer={"replenishScanPageErrorSkuBtn font32 backff775c colfff"} key={"replenishScanPageErrorSkuBtn"} text={"确认扫描完毕"} option={this.hidePop}/>
        let popDom =
            <PopModel popupTitle={"扫描货品异常"} classContainer={"replenishPopInfo replenishScanPageErrorSku"}>
                <div className={"icon iconfont icon-exclamation colf5a623 font150"} />
                <div className={"text font40 col000"}>
                    <p>您需要补完【{taskList.skuName || ""}】，才能补其他货品</p>
                </div>
                <div className={"btn"}>
                    {btnDom}
                    <div className={"colff775c font30 handleForgetSku"} onClick={()=>this.forgetSku()}>忘记该货品的样子</div>
                </div>
            </PopModel>
        showPopup({popupChild:popDom});
    }

    //忘记样子
    forgetSku(){
        let {showPopup,hidePopup} = this.props;
        let btnDom = <Button btnContainer={"replenishScanPageForgetSkuBtn font32 backff775c colfff"} style={{marginTop:"0"}} key={"replenishScanPageForgetSkuBtn"} text={"继续扫描"} option={this.hidePop}/>
        let popDom =
            <PopModel popupTitle={"扫描货品异常"} classContainer={"replenishPopInfo replenishScanPageForgetSku"}>
                {this.getSkuInfo()}
                <div className={"btn"}>
                    {btnDom}
                </div>
            </PopModel>
        showPopup({popupChild:popDom});
    }

    //不在本次补货范围
    outReplenish(){
        //货道已满解决办法
        let {showPopup,hidePopup} = this.props;
        let btnDom = <Button btnContainer={"replenishContainerQrcode font32 backff775c colfff"} key={"replenishContainerWrongQrcode"} text={"关闭"} option={this.hidePop}/>
        let popDom =
            <PopModel popupTitle={"扫描货品异常"} classContainer={"replenishPopInfo"}>
                <div className={"icon iconfont icon-wrong font150"} />
                <div className={"text font40 col000"}>
                    <p>此货品不在本次补货范围内，请放入物流回收箱！</p>
                </div>
                <div className={"btn"}>
                    {btnDom}
                </div>
            </PopModel>
        showPopup({popupChild:popDom});
    }

    getSkuInfo(){
        let {headerInfo} = this.props;
        let {taskList={}} = headerInfo;
        return(
            <div className={"replenishSkuInfo"}>
                <div className={"skuName font30 col333"}>商品名称：{taskList.skuName || ""}</div>
                <div className={"skuInfo"}>
                    <div className={"skuImg"}>
                        <img src={taskList.skuImage || UTILPATH.localImg.defaultImg} alt=""/>
                    </div>
                    <div className={"areaImg"}>
                        <img src={taskList.areaImage || UTILPATH.localImg.defaultImg} alt=""/>
                    </div>
                    <div className={"skuPos col333 font30"}>
                        <ul>
                            <li><span className={"skuPosTitle fl"}>所在区域：</span>{taskList.area || ""}区域<span className={"colff775c showAreaImg"}>区域照片</span></li>
                            <li className={""}>
                                <span className={"skuPosTitle"}>货道信息：</span>
                                <span className={""}>{taskList.position || ""}</span>
                            </li>
                            <li><span className={"skuPosTitle fl"}>货道编号：</span>{taskList.positionNum || ""}</li>
                        </ul>
                    </div>
                </div>
            </div>
        )
    }


    failSubmit(){
        this.setState({
            barcodeText : ''
        },()=>{
            this.props.hidePopup()
        })
    }

    submitBarcode(){
        //提交条形码信息
        let barcodeText = this.state.barcodeText;
        let {getBarCodeProductInfo} = this.props;
        if(barcodeText){
            barcodeText = barcodeText.trim();
            if(this.checkNumber(barcodeText)){
                console.log("submitBarcode ",barcodeText);
                //获取条形码信息
                getBarCodeProductInfo(barcodeText);
            }else{
                //提示条形码不正确
                this.setState({
                    barcodeErrorText : '条形码输入错误，请重新输入'
                })
            }

        }else{
            //提示输入条形码
            this.setState({
                barcodeErrorText : '请输入条形码'
            })
        }

    }

    /**
     * 验证条形码结果是否为纯数字
     * @param str
     * @returns {boolean}
     */
    checkNumber(str){
        let reg = /^[0-9]*$/;
        return reg.test(str);
    }

    _onBarCodeRead = (e) => {
        // console.log(`e.nativeEvent.data.type = ${e.nativeEvent.data.type}, e.nativeEvent.data.code = ${e.nativeEvent.data.code}`)
        this._stopScan();
        this.setState({
            barcodeText : e.nativeEvent.data.code
        },()=>{
            this.submitBarcode()
        });


    };

    _startScan = (e) => {
        // this._barCode.startScan()
    };

    _stopScan = (e) => {
        // this._barCode.stopScan()
    }

    /**
     * 调用扫描条形码
     * @returns {*}
     */
    getScanDom(){
        return (
            <div className={"replenishScanDom"}>
                <div className={"colff775c scanTips font30"} onClick={()=>this.changeShowInput()}><span className={"colfff font26"}>or</span> 手动输入条形码</div>
            </div>
        )
    }

    /**
     * 条形码扫描和输入框切换
     */
    changeShowInput(){
        this.setState({
            showInput : !this.state.showInput,
            barcodeText :"",
            barcodeErrorText:""
        },()=>{
            if(this.state.showInput){
                this._stopScan()
            }else{
                this._startScan()
            }
        })
    }


    render(){
        return(
            <div className={"replenishScanPage"} style={{width:'100%',height:'100%',backgroundColor:'#000'}}>
                <Header
                    showHeader={CONFIG.headerStatus[2]}
                >
                    {this.getOtherHeader()}
                </Header>
                {
                    this.state.showInput ? this.getBarcodeInput() : this.getScanDom()
                }
            </div>
        )
    }
}
