import React from 'react';
import Header from '../../components/CommonComponent/HeaderComponent/HeaderComponent';
import LeftContainer from '../LeftContainer/LeftContainer';
import RightContainer from '../RightContainer/RightContainer';
import RecoverySecondPage from '../../components/RecoveryComponent/RecoverySecondPage/RecoverySecondPage'
import RecoveryScanPage from '../../components/RecoveryComponent/RecoveryScanPage/RecoveryScanPage'
require('./index.css');

const recoveryPageIndex = {
    2 : "getSecondPage",
    3 : "getScanPage"
}

export default class RecoveryContainer extends React.Component{
    constructor(props){
        super(props);
        this.state = {
            showPage : 2 //展示回收箱页面
        };
        this.getSecondPage = this.getSecondPage.bind(this); //展示第二页
        this.getPage = this.getPage.bind(this); //获取要展示的页面
        this.handleFinishBoxRecovery = this.handleFinishBoxRecovery.bind(this); //完成回收箱商品回收
        this.setPage = this.setPage.bind(this); //完成回收箱商品回收
    }

    componentWillMount() {
    }

    componentWillReceiveProps(nextProps){
        let {hidePopup,headerInfo} = nextProps;
        if(this.props.headerInfo && this.props.headerInfo.taskList && headerInfo && headerInfo.taskList){
            let oldTaskList = this.props.headerInfo.taskList;
            let taskList = headerInfo.taskList;
            if(taskList.skuId !== oldTaskList.skuId || (taskList.skuId === oldTaskList.skuId && taskList.recoverNum !== oldTaskList.recoverNum)){
                hidePopup();
                this.handleFinishBoxRecovery();
            }
        }

    }

    handleFinishBoxRecovery(){
        //完成回收箱物品回收
        this.setState({
            showPage : 2
        })
    }

    getSecondPage(props){
        return (
            <RecoverySecondPage {...props} beginScan={this.setPage}/>
        )
    }

    getScanPage(props){
        return (
            <RecoveryScanPage {...props} goBack={this.setPage}/>
        )
    }

    setPage(page){
        this.setState({
            showPage : page
        })
    }

    getPage(type){
        return (props)=>this[recoveryPageIndex[type]](props)
    }

    render(){
        let props = this.props;
        let dom = this.getPage(this.state.showPage)(props);
        return(
            <div className={"recoveryContainer page"}>
                {
                    this.state.showPage === 3 ?
                        this.getScanPage(props) :
                        <div style={{width:'100%',height:'100%'}}>
                            {
                                props.headerInfo && props.headerInfo.storeInfo ?
                                    <Header headerInfo={props.headerInfo}/> : null
                            }

                            <LeftContainer leftInfo={props.headerInfo}/>
                            <RightContainer>
                                {dom}
                            </RightContainer>
                        </div>
                }
            </div>
        )
    }
}
