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('hrmRecruitPlan') //todo
@observer
export default class RecruitPlan extends React.Component { //todo
    constructor(props) {
        super(props);
    }

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

        if (id) {
            this.props.hrmRecruitPlan.setPropsFromParent(id);
        }

        if (window.location.href.indexOf('/spa/hrm/engine.html') > -1) {
            document.title = getLabel('6132', "招聘计划"); //todo
        }
    }

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

    componentDidMount() {
        this.init();
    }

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

    componentWillUnmount() {
        const {
            hrmRecruitPlan
        } = this.props, {
            main,
            setPropsFromParent,
        } = hrmRecruitPlan, {
            isSingle
        } = main;

        isSingle && setPropsFromParent('');
    }

    render() {
        const {
            hrmRecruitPlan
        } = this.props, {
            main,
        } = hrmRecruitPlan, {
            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@acs0hw`} spinning={loading} size='large'></Spin>
                                </div>
                             :    <div className="hrm-wea-alert-page">
                                    <WeaAlertPage ecId={`${this && this.props && this.props.ecId || ''}_WeaAlertPage@de5sb1`}>
                                        <div style={{ color: '#000' }}>
                                            {getLabel(2012, '对不起，您暂时没有权限！')}
                                        </div>
                                      </WeaAlertPage>
                                </div>
                    }    
                </div>
            )
        } else {
            return (
                <div>
                    <Top ecId={`${this && this.props && this.props.ecId || ''}_Top@0uss8e`} />
                    <MainDialog ecId={`${this && this.props && this.props.ecId || ''}_MainDialog@z5ubn8`} />
                    <SubDialog ecId={`${this && this.props && this.props.ecId || ''}_SubDialog@z09vs6`} />
                </div>
            )
        }
    }
}