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';
const getLabel = WeaLocaleProvider.getLabel;

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

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

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

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

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

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

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

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

        isSingle && setPropsFromParent('');
    }

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