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

import Top from './Top';
import MainDialog from './MainDialog';
import SubDialog from './SubDialog';
const getLabel = WeaLocaleProvider.getLabel;

@inject('hrmRecruitLibrary') //todo
@observer
export default class RecruitLibrary extends React.Component { //todo
    constructor(props) {
        super(props);
    }

    componentWillMount() {
        const inviteInfoId = this.props.inviteInfoId || this.props.location.query.inviteInfoId;

        const needTab = this.props.inviteInfoId ? false : true;

        if (inviteInfoId) {
            this.props.hrmRecruitLibrary.setProps(true, inviteInfoId, needTab);
        }

        if (window.location.href.indexOf('/spa/hrm/engine.html') > -1) {
            document.title = getLabel(16251, '应聘库'); //todo
        }
    }

    componentWillReceiveProps(nextProps) {
        const {
            hrmRecruitLibrary
        } = this.props, {
            refresh,
            getTableInfo,
        } = hrmRecruitLibrary;

        if (this.props.location && (this.props.location.key !== nextProps.location.key)) {
            refresh();
        }
        if (this.props.searchParams && (this.props.searchParams !== nextProps.searchParams)) {
            getTableInfo(nextProps.searchParams);
        }
    }

    componentWillUnmount() {
        const {
            hrmRecruitLibrary
        } = this.props, {
            main
        } = hrmRecruitLibrary, {
            isShowInDialog
        } = main;

        isShowInDialog && this.props.hrmRecruitLibrary.setProps(false, '', true);
    }

    componentDidMount() {
        this.props.onRef && this.props.onRef(this.props.hrmRecruitLibrary);
        this.init();
    }

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

    render() {
        const {
            hrmRecruitLibrary
        } = this.props, {
            main,
        } = hrmRecruitLibrary, {
            loading,
            authorized
        } = main;

        if (!authorized) {
            return (
                <div style={{height: '100%'}}>
                    {loading ?  <div className='hrm-loading-center'>
                                    <Spin ecId={`${this && this.props && this.props.ecId || ''}_Spin@hr17l2`} spinning={loading} size='large'></Spin>
                                </div>
                             :    <div className="hrm-wea-alert-page">
                                    <WeaAlertPage ecId={`${this && this.props && this.props.ecId || ''}_WeaAlertPage@13fd1q`}>
                                        <div style={{ color: '#000' }}>
                                            {getLabel(2012, '对不起，您暂时没有权限！')}
                                        </div>
                                      </WeaAlertPage>
                                </div>
                    }    
                </div>
            )
        } else {
            return (
                <div>
                    <Top ecId={`${this && this.props && this.props.ecId || ''}_Top@xxfiwo`} {...this.props}/>
                    <MainDialog ecId={`${this && this.props && this.props.ecId || ''}_MainDialog@c79wmj`} />
                    <SubDialog ecId={`${this && this.props && this.props.ecId || ''}_SubDialog@etnof9`} /> 
                </div>
            )
        }
    }
}