import React, { Component } from 'react';
import { inject, observer } from 'mobx-react';
import { WeaAlertPage, WeaDialogFooter } from 'ecCom';
import FormInfo from '../../coms/FormInfo';
import { i18n } from '../../public/i18n';

@inject('hrmRoleSetting')
@observer
export default class AddRole extends Component{
    constructor(props) {
		super(props);
    }
    
    componentDidMount(){
        this.init();
    }

    componentWillReceiveProps(nextProps){
        this.init();
    }

	init = () => {
		const {
			hrmRoleSetting: store
		} = this.props
		const {
			checkAuthorized,
			initAddRole
		} = store;
		checkAuthorized('role', null, () => initAddRole(this.saveCallBack), null, true);
    }
    
    saveCallBack = (datas, setting) => {
        let dialog = top.window.getParentDialog();
        dialog.callback(datas, setting);
        dialog.close();
    }

    render(){
        const { hrmRoleSetting: store } = this.props;
        const { containerInitFinished, formTarget, formItemRender, getDialogOpButtons, editorDialogRightMenu } = store;
        const { roleInfoForm, roleInfoFormFields } = formTarget;
        
        return (
            <div>
                {
                    containerInitFinished.init && containerInitFinished.authorized && 
                    <FormInfo ecId={`${this && this.props && this.props.ecId || ''}_FormInfo@blbgks`} 
                        form={roleInfoForm}
                        formFields={roleInfoFormFields}
                        itemRender={formItemRender}
                        menu={editorDialogRightMenu}/>
                }
                {
                    containerInitFinished.init && containerInitFinished.authorized && 
                    <WeaDialogFooter ecId={`${this && this.props && this.props.ecId || ''}_WeaDialogFooter@vyw0w3`} 
                        buttons={getDialogOpButtons()}
                        moreBtn={{datas: editorDialogRightMenu}}
                    />
                }
                {
					containerInitFinished.init && !containerInitFinished.authorized && 
	              	<WeaAlertPage ecId={`${this && this.props && this.props.ecId || ''}_WeaAlertPage@9oy095`}>
	                  	<div style={{ color: '#000' }}>{i18n.message.authFailed()}</div>
	              	</WeaAlertPage>
				}
            </div>
        )
    }
}