import React from 'react'
import {
	toJS
} from 'mobx'
import {
	inject,
	observer,
} from 'mobx-react'
import {
	WeaTop,
	WeaTab,
	WeaFormItem,
	WeaRightMenu,
	WeaAlertPage,
} from 'ecCom'
import {
	WeaSwitch,
} from 'comsMobx'
import {
	Row,
	Col,
	Spin,
	Button,
	message,
} from 'antd';
import {
	i18n
} from '../../public/i18n';

import Editor from './Editor'
import Table from './Table'
import '../../style/personalSalaryAdjustment.less'

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

	componentWillMount() {
		let bool = window.location.href.indexOf('/spa/hrm/engine.html') > -1;
		if (bool) {
			document.title = i18n.module.personalSalaryAdjustment();
		}
	}

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

	componentDidMount() {
		this.init();
	}

	init() {
		const {
			hrmPersonalSalaryAdjustment
		} = this.props, {
			getRight,
			mobxDatasReset,
		} = hrmPersonalSalaryAdjustment

		mobxDatasReset();
		getRight();
	}

	getDropMenuDatas = () => {
		const {
			hrmPersonalSalaryAdjustment
		} = this.props, {
			menu
		} = hrmPersonalSalaryAdjustment, {
			rightMenu
		} = menu;

		let menus = [];
		toJS(rightMenu).map((item, index) => {
			let obj = {
				key: item.menuFun,
				icon: <i className={`${item.menuIcon}`}/>,
				content: item.menuName,
			}
			if (item.menuFun == 'log' || item.menuFun == 'collection' || item.menuFun == 'help' || item.menuFun == 'pageAddress') {
				obj.disabled = true;
			}
			menus.push(obj);
		})

		return menus;
	}

	handleMenuClick = (key) => {
		const {
			hrmPersonalSalaryAdjustment
		} = this.props, {
			isPanelShow,
			setPanelStatus
		} = hrmPersonalSalaryAdjustment;

		isPanelShow && setPanelStatus(false);
		this[key] && this[key]();
	}

	getTopMenuBtns = () => {
		const {
			hrmPersonalSalaryAdjustment
		} = this.props, {
			menu,
			table,
		} = hrmPersonalSalaryAdjustment, {
			topMenu
		} = menu, {
			tableStore
		} = table;

		let btns = [];
		toJS(topMenu).map((item, index) => {
			if (item.menuFun !== 'batchDelete') {
				btns.push(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@o8jg0o@${index}`} type='primary' onClick={() => this.handleClick(item)}>{item.menuName}</Button>);
			} else {
				btns.push(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@1zspuy@${index}`} type='primary' onClick={() => this.handleClick(item)} disabled={tableStore.selectedRowKeys.length > 0 ? false : true} >{item.menuName}</Button>);
			}
		});
		return btns;
	}

	handleClick = (item) => {
		const {
			hrmPersonalSalaryAdjustment
		} = this.props, {
			isPanelShow,
			setPanelStatus
		} = hrmPersonalSalaryAdjustment;

		isPanelShow && setPanelStatus(false);
		this[item.menuFun] && this[item.menuFun]()
	}

	save = () => {
		const {
			hrmPersonalSalaryAdjustment
		} = this.props, {
			editorSave
		} = hrmPersonalSalaryAdjustment;

		editorSave();
	}

	custom = () => {
		const {
			hrmPersonalSalaryAdjustment
		} = this.props, {
			table,
		} = hrmPersonalSalaryAdjustment, {
			tableStore
		} = table;

		tableStore.setColSetVisible(true);
		tableStore.tableColSet(true);
	}

	getDropPanelButtons = () => {
		const {
			hrmPersonalSalaryAdjustment
		} = this.props, {
			searchPanel,
			setPanelStatus,
		} = hrmPersonalSalaryAdjustment, {
			form
		} = searchPanel;

		const btn = [
			(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@9naftn`} type="primary" onClick={() => this.handleSearch()}>{i18n.button.search()}</Button>),
			(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@yuigwl`} onClick={() => {form.resetForm();form.resetConditionValue()} }>{i18n.button.reset()}</Button>),
			(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@ym373i`} onClick={() => setPanelStatus(false)}>{i18n.button.cancel()}</Button>),
		];
		return btn;
	}

	getPanelComponents = () => {
		const {
			hrmPersonalSalaryAdjustment
		} = this.props, {
			searchPanel
		} = hrmPersonalSalaryAdjustment, {
			condition,
			form,
			loading
		} = searchPanel, {
			isFormInit
		} = form;

		let arr = [];
		isFormInit && condition.map(c => {
			c.items.map((field, index) => {
				arr.push(<Col ecId={`${this && this.props && this.props.ecId || ''}_Col@ml2e0a@${index}`} span={10} offset={1}>
                              <div style={{marginTop: 20}}>
                                  <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@vt8mkj@${index}`}
                                      label={`${field.label}`}
                                      labelCol={{span: (index % 2 == 0) ? 7 : 6}}
                                      wrapperCol={{span: (index % 2 == 0) ? 17 : 18}}>
                                          {<WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@px72ob@${index}`} fieldConfig={field} form={form} formParams={form.getFormParams()} />}
                                  </WeaFormItem>
                              </div>
                  	     </Col>)
			})
		})
		if (loading) {
			return (
				<div className='hrm-loading-center-small' style={{top:'30%'}}>
                    <Spin ecId={`${this && this.props && this.props.ecId || ''}_Spin@74dlfp`} spinning={loading}></Spin>
                </div>
			)
		} else {
			return <Row ecId={`${this && this.props && this.props.ecId || ''}_Row@zbcu2e`}
				onKeyDown={(e) =>( e.keyCode == 13 && e.target.tagName === "INPUT")  && this.handleSearch()}
			>{arr}</Row>
		}
	}

	handleSearch = () => {
		const {
			hrmPersonalSalaryAdjustment
		} = this.props, {
			isPanelShow,
			setPanelStatus,
			getTableInfo
		} = hrmPersonalSalaryAdjustment;

		getTableInfo();
		isPanelShow && setPanelStatus(false);
	}

	getTabName = () => {
		const tabDatas = [{
			key: '0',
			title: i18n.label.salaryAdjustment()
		}, {
			key: '1',
			title: i18n.label.adjustmentHistory()
		}]

		return tabDatas;
	}

	render() {
		const {
			hrmPersonalSalaryAdjustment
		} = this.props, {
			authorized,
			hasAddSalaryIndexRight,
			loading,
			title,
			key,
			isPanelShow,
			searchPanel,
			editor,
			setPanelStatus,
			setBaseValueOfSearch,
			switchKey,
		} = hrmPersonalSalaryAdjustment, {
			conditionCount,
			value
		} = searchPanel;

		if (!authorized) {
			return (
				<div style={{height: '100%'}}>
					{loading ?  <div className='hrm-loading-center'>
									<Spin ecId={`${this && this.props && this.props.ecId || ''}_Spin@tb7tv0`} spinning={loading} size='large'></Spin>
								</div>
						     :	<div className="hrm-wea-alert-page">
		                			<WeaAlertPage ecId={`${this && this.props && this.props.ecId || ''}_WeaAlertPage@46rmmg`}>
						                <div style={{ color: '#000' }}>
						                    {i18n.message.authFailed()}
						                </div>
		                      		</WeaAlertPage>
	                        	</div>
	                }	
                </div>
			)
		} else {
			return (
				<WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@s9odyz`} 
					datas={this.getDropMenuDatas()} 
					onClick={ key => this.handleMenuClick(key)}
					collectParams={{favname:title,favouritetype:5}}
				>
					<WeaTop ecId={`${this && this.props && this.props.ecId || ''}_WeaTop@zzqr9a`} 
					    title={title}
					    icon={<i className='icon-coms-hrm'/>}
					    iconBgcolor='#217346'
					    loading={true}
					    buttons={this.getTopMenuBtns()}
					    showDropIcon={true}
					    dropMenuDatas={this.getDropMenuDatas()}
					    onDropMenuClick={e => this.handleMenuClick(e)}
					>
						<WeaTab ecId={`${this && this.props && this.props.ecId || ''}_WeaTab@yzw1z8`}
						    keyParam={'key'}
					   		selectedKey={key}
					   		onChange={key => switchKey(key)}
					   		datas={hasAddSalaryIndexRight ? this.getTabName() : []}
					   		searchType={ ( (hasAddSalaryIndexRight && key == '1') || !hasAddSalaryIndexRight) ? ['base','advanced'] : []}
					   		showSearchAd={isPanelShow}
					   		hasMask={false}
					   		advanceHeight={Math.ceil(conditionCount / 2)*52+20}
					   		buttonsAd={this.getDropPanelButtons()}
					   		searchsBaseValue={value}
					   		setShowSearchAd={  b => setPanelStatus(b)}
					   		hideSearchAd={ () => setPanelStatus(false)}
					   		searchsAd= {isPanelShow ? this.getPanelComponents():<div></div>}
					   		onSearch={() => this.handleSearch()}
					   		onSearchChange={val => setBaseValueOfSearch(val)}
						/>
						{ (key == '0' && hasAddSalaryIndexRight)  ? <Editor ecId={`${this && this.props && this.props.ecId || ''}_Editor@67y46m`} /> : <Table ecId={`${this && this.props && this.props.ecId || ''}_Table@b9kwr3`} />}
					</WeaTop>
				</WeaRightMenu>
			)
		}
	}
}