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

import Editor from './Editor';

@inject('hrmSalaryBank')
@observer
export default class SalaryBank 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.salaryBank();
		}
	}

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

	componentDidMount() {
		this.init();
	}

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

		mobxDatasReset();
		getRight();
	}

	getDropMenuDatas = () => {
		const {
			hrmSalaryBank
		} = this.props, {
			menu
		} = hrmSalaryBank, {
			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 == 'collection' || item.menuFun == 'help' || item.menuFun == 'pageAddress') {
				obj.disabled = true;
			}
			menus.push(obj);
		})

		return menus;
	}

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

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

	getTopMenuBtns = () => {
		const {
			hrmSalaryBank
		} = this.props, {
			menu,
			table,
		} = hrmSalaryBank, {
			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@lbw7pk@${index}`} type='primary' onClick={() => this.handleClick(item)}>{item.menuName}</Button>);
			} else {
				btns.push(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@7bfiqw@${index}`} type='primary' onClick={() => this.handleClick(item)} disabled={tableStore.selectedRowKeys.length > 0 ? false : true} >{item.menuName}</Button>);
			}
		});
		return btns;
	}

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

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

	log = () => {
		window.setLogViewProps({
			logSmallType: '3032',
		});
	}

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

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


	new() {
		const {
			hrmSalaryBank
		} = this.props, {
			setEditorDialogVisible
		} = hrmSalaryBank

		setEditorDialogVisible(true, 'new');
	}

	batchDelete() {
		const {
			hrmSalaryBank
		} = this.props, {
			table,
			setSelectedRowsKeys,
		} = hrmSalaryBank, {
			tableStore,
		} = table;

		let keys = toJS(tableStore.selectedRowKeys).toString();
		setSelectedRowsKeys(keys);
		this.deleteRows();
	}

	deleteRows(id) {
		const {
			hrmSalaryBank
		} = this.props, {
			setSelectedRowsKeys
		} = hrmSalaryBank;

		id && setSelectedRowsKeys(id);
		let v = id ? 'del' : 'batchDel';
		this.showConfirm(v);
	}

	showConfirm(v) {
		let _this = this;
		confirm({
			title: i18n.confirm.defaultTitle(),
			content: (v == 'del') ? i18n.confirm.delete() : i18n.confirm.batchDeleteConfirm(),
			okText: i18n.button.ok(),
			cancelText: i18n.button.cancel(),
			onOk() {
				_this.onOk()
			},
			onCancel() {
				return false
			},
		});
	}

	onOk() {
		const {
			hrmSalaryBank
		} = this.props, {
			deleteTableRows
		} = hrmSalaryBank;

		deleteTableRows();
	}

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

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

	getBaseValueOfSearch() {
		const {
			hrmSalaryBank
		} = this.props, {
			searchPanel
		} = hrmSalaryBank, {
			form,
			value
		} = searchPanel;

		let val;
		if (form.isFormInit) {
			val = form.getFormParams().bankname;
		} else {
			val = value;
		}
		return val;
	}

	getPanelComponents() {
		const {
			hrmSalaryBank
		} = this.props, {
			searchPanel
		} = hrmSalaryBank, {
			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@zc5los@${index}`} span={10} offset={1}>
                              <div style={{marginTop: 20}}>
                                  <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@x1nauk@${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@ukbfbl@${index}`} fieldConfig={field} form={form} formParams={form.getFormParams()} />}
                                  </WeaFormItem>
                              </div>
                  	     </Col>)
			})
		})
		return <Spin ecId={`${this && this.props && this.props.ecId || ''}_Spin@hib7el`} spinning={loading}><Row ecId={`${this && this.props && this.props.ecId || ''}_Row@wvsgft`}
			onKeyDown={(e) =>( e.keyCode == 13 && e.target.tagName === "INPUT")  && this.handleSearch()}
		>{arr}</Row></Spin>
	}

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

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

	handleSearchChange(val) {
		const {
			hrmSalaryBank
		} = this.props, {
			searchPanel,
			updateFields,
			setBaseValueOfSearch
		} = hrmSalaryBank, {
			form
		} = searchPanel

		if (form.isFormInit) {
			updateFields(val);
		}
		setBaseValueOfSearch(val);
	}

	onOperatesClick(record, index, operate) {
		const {
			hrmSalaryBank
		} = this.props, {
			setEditorDialogVisible,
			setSelectedRowsKeys
		} = hrmSalaryBank;

		let i = operate.index,
			id = record.randomFieldId;

		switch (i) {
			case '0': //编辑
				this.doEdit(id)
				break;
			case '1': //删除
				this.deleteRows(id)
				break;
			case '2':
				window.setLogViewProps({
					logSmallType: '3032',
					targetId: id,
				});
				break;
		}
	}

	doEdit = (id) => {
		const {
			hrmSalaryBank
		} = this.props, {
			setSelectedRowsKeys,
			setEditorDialogVisible,
		} = hrmSalaryBank;

		setSelectedRowsKeys(id);
		setEditorDialogVisible(true, 'edit');
	}

	render() {
		const {
			hrmSalaryBank
		} = this.props, {
			authorized,
			loading,
			title,
			isPanelShow,
			table,
			searchPanel,
			editor,
			setPanelStatus,
			setEditorDialogVisible,
		} = hrmSalaryBank, {
			tableStore
		} = table, {
			conditionCount
		} = searchPanel;

		if (!authorized) {
			return (
				<div className="hrm-wea-alert-page">
          			<Spin ecId={`${this && this.props && this.props.ecId || ''}_Spin@m348ga`} spinning={loading}>
                		{!loading  &&   <WeaAlertPage ecId={`${this && this.props && this.props.ecId || ''}_WeaAlertPage@bhvbzt`}>
				                            <div style={{ color: '#000' }}>
				                                {i18n.message.authFailed()}
				                            </div>
                        				</WeaAlertPage>
                		}
                    </Spin>
                </div>
			)
		} else {
			return (
				<WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@46wwk3`} 
					datas={this.getDropMenuDatas()} 
					onClick={ key => this.handleMenuClick(key)}
					collectParams={{favname: title,favouritetype:5}}
				>
				        <WeaTop ecId={`${this && this.props && this.props.ecId || ''}_WeaTop@cbe4qo`} 
				            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@fdwaoe`}
					            searchType={['base','advanced']}
					            showSearchAd={isPanelShow}
					            hasMask={false}
					            advanceHeight={Math.ceil(conditionCount / 2)*52+20}
					            buttonsAd={this.getDropPanelButtons()}
					            searchsBaseValue={this.getBaseValueOfSearch()}
					            setShowSearchAd={  b => setPanelStatus(b)}
					            hideSearchAd={ () => setPanelStatus(false)}
					            searchsAd= {isPanelShow ? this.getPanelComponents():<div></div>}
					            onSearch={() => this.handleSearch()}
					            onSearchChange={val => this.handleSearchChange(val)}
					        />
				        	<WeaTable ecId={`${this && this.props && this.props.ecId || ''}_WeaTable@222cpc`} 
					            comsWeaTableStore={tableStore}
					            hasOrder={true}
					            needScroll={true}
					            onOperatesClick={(record, index, operate) => this.onOperatesClick(record, index, operate)}
					        />
					        {editor.visible && <Editor ecId={`${this && this.props && this.props.ecId || ''}_Editor@8r7n39`} />}
				        </WeaTop>
				    </WeaRightMenu>
			)
		}
	}


}