import React from 'react'
import {
    inject,
    observer
} from 'mobx-react'
import {
    WeaLocaleProvider
} from 'ecCom'
import {
    Spin
} from 'antd'

import Top from './Top';
import MainTable from './MainTable';
import NoRight from "../../public/NoRight";
import NonStand from "../../public/NonStand";

const getLabel = WeaLocaleProvider.getLabel;

import '../../style/login.less';

@inject('intelogin')
@observer
export default class Login extends React.Component {
    constructor(props) {
        super(props);
    }

    componentWillMount() {
        let paramsIndex = -1;
        paramsIndex = location.href.trim() && location.href.trim().indexOf('?');
        if(this.props.source) {
            this.props.intelogin.setSource(this.props.source);
        }else if((paramsIndex != -1) && (location.href.trim().indexOf('typename') > paramsIndex)) {
          let url = location.href.split('?')[1];
          if(url) {
            let params = url.split('&');
            let query = {};
            for(let i = 0; i < params.length; i++) {
                query[params[i].split('=')[0]] = params[i].split('=')[1];
            }
            if(query.typename == '1') {
                this.props.intelogin.setSource('NC');
            }else if(query.typename == '2') {
                this.props.intelogin.setSource('EAS');  
            }else if(query.typename == '3') {
                this.props.intelogin.setSource('U8');  
            }else if(query.typename == '4') {
                this.props.intelogin.setSource('K3');  
            }
          }
        }else {
            this.props.intelogin.setSource('');
        }
        if (window.location.href.indexOf((window.ecologyContentPath || "") + '/spa/integration/static4engine/engine.html') > -1) {
            document.title = getLabel(20960, "集成登录");
        }
    }

    componentWillReceiveProps(nextProps) {
        if (this.props.location && (this.props.location.key !== nextProps.location.key)) {
            this.init();
        }
    }

    componentDidMount() {
        const {
            intelogin
        } = this.props;
        const {
            source,
            getMenus,
            getSearchCondition,
          search
        } = intelogin;
        this.init();        
        if (source != '') {
            if(source != "tencentmail") {
                getMenus();
                getSearchCondition();
                search();
            }
        }
    }

    init() {
        this.props.intelogin.getAuth();
    }

    render() {
        const {
            intelogin
        } = this.props, {
            main,
            source,
        } = intelogin, {
            loading,
            authorized,
            hasNonStandard,
            nonStandLoading,
        } = main;
        if(!nonStandLoading) {
            if(hasNonStandard) {
                if (!authorized) {
                    return (
                        <div style={{height: '100%'}}>
                            {loading ?  <div className='integration-loading-center'>
                                            <Spin ecId={`${this && this.props && this.props.ecId || ''}_Spin@f9a9x8`} spinning={loading} size='large'></Spin>
                                        </div>
                                     :  <NoRight ecId={`${this && this.props && this.props.ecId || ''}_NoRight@l71x03`} />
                            }
                        </div>
                    )
                } else {
                    return (
                        <div>
                            {
                                source !== 'coremail' && source !== 'tencentmail' 
                                  ? <Top ecId={`${this && this.props && this.props.ecId || ''}_Top@ufrfla`}/> 
                                  : <MainTable ecId={`${this && this.props && this.props.ecId || ''}_MainTable@g0nmi6`} typename={source === "tencentmail" ? "6" : ""} />
                            }
                        </div>
                    )
                }
            }else {
                return <NonStand ecId={`${this && this.props && this.props.ecId || ''}_NonStand@vpafau`} />
            }
        }else {
            return null;
        }

    }
}
