import {
	observable,
	action,
	computed
} from 'mobx';
import {
	WeaForm,
	WeaTableNew
} from 'comsMobx';
import {
	Button,
	message,
	Menu,
	Dropdown,
	Modal
} from 'antd';
import HrmBaseStore from './baseStore';
import * as api from '../apis/batchMaintenance';
import upperFirst from 'lodash/upperFirst';
import {
	i18n
} from '../public/i18n';
import { WeaLocaleProvider } from 'ecCom';
import LoginLog from '../components/loginLog';
const getLabel = WeaLocaleProvider.getLabel;

export class HrmBatchMaintenance extends HrmBaseStore {
	/********************* unobservable list *********************/
	routeProps = {}

	front = '';

	groupDef = [{
		icon: '',
		imgUrl: '/hrm/hrm_e9/image/batchMaintenance/adjust.png',
		label: i18n.label.batchAdjust,
		btns: [{
			label: i18n.module.batchAdjustSubCompany,
			icon: '',
			tips: i18n.module.batchAdjustSubCompanyTips,
			linkTo: `${this.front}hrmengine/batchMaintenance/adjust/subCompany`
		}, {
			label: i18n.module.batchAdjustDepartment,
			icon: '',
			tips: i18n.module.batchAdjustDepartmentTips,
			linkTo: `${this.front}hrmengine/batchMaintenance/adjust/department`
		}, {
			label: i18n.module.batchAdjustResource,
			icon: '',
			tips: i18n.module.batchAdjustResourceTips,
			linkTo: `${this.front}hrmengine/batchMaintenance/adjust/resource`
		}]
	}, {
		icon: '',
		imgUrl: '/hrm/hrm_e9/image/batchMaintenance/edit.png',
		label: i18n.label.batchEdit,
		btns: [{
			label: i18n.module.batchEditSubCompany,
			icon: '',
			tips: i18n.module.batchEditSubCompanyTips,
			linkTo: `${this.front}hrmengine/batchMaintenance/edit/subCompany`
		}, {
			label: i18n.module.batchEditDepartment,
			icon: '',
			tips: i18n.module.batchEditDepartmentTips,
			linkTo: `${this.front}hrmengine/batchMaintenance/edit/department`
		}, {
			label: i18n.module.batchEditResource,
			icon: '',
			tips: i18n.module.batchEditResourceTips,
			linkTo: `${this.front}hrmengine/batchMaintenance/edit/resource`
		}]
	}, {
		icon: '',
		imgUrl: '/hrm/hrm_e9/image/batchMaintenance/upload.png',
		label: i18n.label.batchUpload,
		btns: [{
			label: i18n.module.batchUploadPhoto,
			icon: '',
			tips: i18n.module.batchUploadPhotoTips,
			linkTo: `${this.front}hrmengine/batchMaintenance/uploadPhoto`
		}]
	}]

	@computed get dropDownMenuDef () {
		return (this.batchOpenStatus == true ? {
			subCompany: [
				{
					key: 'SupSubcomid',
					onClickHandle: () => {
						this.getForm('SupSubcomid', i18n.label.batchAdjustSupSubCompany())
					},
					content: i18n.label.batchAdjustSupSubCompany
				}
			],
			department: [
				{
					key: 'Subcomid',
					onClickHandle: () => {
						this.getForm('Subcomid', i18n.label.batchAdjustSubCompany())
					},
					content: i18n.label.batchAdjustSubCompany
				}, {
					key: 'Supdepid',
					onClickHandle: () => this.getForm('Supdepid', i18n.label.batchAdjustSupDepartment()),
					content: i18n.label.batchAdjustSupDepartment
				}
			],
			resource: [
				{
					key: 'Managerid',
					onClickHandle: () => {
						this.getForm('Managerid', i18n.label.batchAdjustSup())
					},
					content: i18n.label.batchAdjustSup
				}, {
					key: 'Jobtitle',
					onClickHandle: () => this.getForm('Jobtitle', i18n.label.batchAdjustStation()),
					content: i18n.label.batchAdjustStation
				}, {
					key: 'ResourceDeptid',
					onClickHandle: () => this.getForm('ResourceDeptid', i18n.label.batchAdjustDepartment()),
					content: i18n.label.batchAdjustDepartment
				}, {
					key: 'Accounttype',
					onClickHandle: () => this.getForm('Accounttype', i18n.label.batchAdjustAccount()),
					content: i18n.label.batchAdjustAccount
				},
				{
					key: "unblock",
					onClickHandle: () =>

						this.getForm('unblock', i18n.label.confirmBatchUnblock())
					,
					content: i18n.label.batchUnblock
				},
				{
					key: 'Password',
					onClickHandle: () => this.getForm('Password', i18n.label.batchAdjustPassword()),
					content: i18n.label.batchAdjustPassword
				}, {
					key: "Reset",
					onClickHandle: () => this.getForm('Reset'),
					content: () => getLabel(515425, "批量重置密码"),
				}
			]
		} : {
				subCompany: [
					{
						key: 'SupSubcomid',
						onClickHandle: () => {
							this.getForm('SupSubcomid', i18n.label.batchAdjustSupSubCompany())
						},
						content: i18n.label.batchAdjustSupSubCompany
					}
				],
				department: [
					{
						key: 'Subcomid',
						onClickHandle: () => {
							this.getForm('Subcomid', i18n.label.batchAdjustSubCompany())
						},
						content: i18n.label.batchAdjustSubCompany
					}, {
						key: 'Supdepid',
						onClickHandle: () => this.getForm('Supdepid', i18n.label.batchAdjustSupDepartment()),
						content: i18n.label.batchAdjustSupDepartment
					}
				],
				resource: [
					{
						key: 'Managerid',
						onClickHandle: () => {
							this.getForm('Managerid', i18n.label.batchAdjustSup())
						},
						content: i18n.label.batchAdjustSup
					}, {
						key: 'Jobtitle',
						onClickHandle: () => this.getForm('Jobtitle', i18n.label.batchAdjustStation()),
						content: i18n.label.batchAdjustStation
					}, {
						key: 'ResourceDeptid',
						onClickHandle: () => this.getForm('ResourceDeptid', i18n.label.batchAdjustDepartment()),
						content: i18n.label.batchAdjustDepartment
					}, {
						key: 'Accounttype',
						onClickHandle: () => this.getForm('Accounttype', i18n.label.batchAdjustAccount()),
						content: i18n.label.batchAdjustAccount
					},
					{
						key: 'Password',
						onClickHandle: () => this.getForm('Password', i18n.label.batchAdjustPassword()),
						content: i18n.label.batchAdjustPassword
					}, {
						key: "Reset",
						onClickHandle: () => this.getForm('Reset'),
						content: () => getLabel(515425, "批量重置密码"),
					}
				]
			})
	}



	getDropDownMenu = (type) => (
		<Menu ecId={`${this && this.props && this.props.ecId || ''}_Menu@i41pzz`}>
			{
				this.dropDownMenuDef[type].map((m, index) => {
					return (
						<Menu.Item ecId={`${this && this.props && this.props.ecId || ''}_undefined@tp9h08@${index}`} key={m.key}>
							<div onClick={m.onClickHandle}>{m.content()}</div>
						</Menu.Item>
					)
				})
			}
		</Menu>
	)
	getBatchUnblock = () => {
		return (
			this.batchOpenStatus == true ?
				<Menu ecId={`${this && this.props && this.props.ecId || ''}_Menu@i41pzz`}>
					<Menu.Item ecId={`${this && this.props && this.props.ecId || ''}_undefined@tp9h08`} key={'1'} >
						<div onClick={() => this.batchOpen(true)} >{i18n.label.batchUnblock()}</div>
					</Menu.Item>
					<Menu.Item ecId={`${this && this.props && this.props.ecId || ''}_undefined@tp9h08`} key={'1'}>
						<div onClick={() => this.batchOpen(false)}>{i18n.button.otherBatchOp()}</div>
					</Menu.Item>
				</Menu> : <Menu ecId={`${this && this.props && this.props.ecId || ''}_Menu@i41pzz`} >
					<Menu.Item ecId={`${this && this.props && this.props.ecId || ''}_undefined@tp9h08`} key={'1'}  >
						<div onClick={() => this.batchOpen(true)}>{i18n.label.batchUnblock()}</div>
					</Menu.Item>
				</Menu>)
	}

	transDropDownMenu = (type) => {
		let rightMenus = [];
		this.dropDownMenuDef[type].map(m => {
			rightMenus.push({
				key: m.key,
				content: m.content(),
				icon: <i className=' icon-coms-Batch-sharing' />,
				onClick: m.onClickHandle,
				disabled: this.multiOp
			})
		});
		return rightMenus;
	}

	batchOpen = (status) => {
		this.changeBatchOpenStatus(status);
		this.getListDatas(this.generateTableReqParams(), false, status)
	}

	getBtnsAndMenus = (type) => {
		let logType;
		switch (type) {
			case 'subCompany':
				logType = 'HRM_ENGINE_Personnel_ORG_EDIT_SUB';
				break;
			case 'department':
				logType = 'HRM_ENGINE_Personnel_ORG_EDIT_DEPT';
				break;
			case 'resource':
				logType = 'HRM_ENGINE_Personnel_ORG_EDIT_RES';
				break;
		}
		const buttons = [
			<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@qfvcyq`} type='primary' op="save" onClick={() => this.saveEdit && this.saveEdit()}>{i18n.button.save()}</Button>,
			<Dropdown ecId={`${this && this.props && this.props.ecId || ''}_Dropdown@w1yrw7`} overlay={this.getDropDownMenu(type)} trigger={['click']} disabled={this.multiOp}>
				<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@m2oqrp`} type='primary' disabled={this.multiOp}>{i18n.button.batchOp()}</Button>
			</Dropdown>
			,
			<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@42796n`} type='primary' onClick={() => this.definedColumnVisible && this.definedColumnVisible()} style={type=='resource'? {marginLeft:"20px"}:{marginLeft:"0px"}}>{i18n.button.columnVisibleSetting()}</Button>,
		];
		if (type == 'resource') {
			buttons.splice(1, 1, <Dropdown.Button ecId={`${this && this.props && this.props.ecId || ''}_Dropdown@w1yrw7`} overlay={this.getBatchUnblock()} trigger={['click']} disabled={this.multiOp} >
				<Dropdown ecId={`${this && this.props && this.props.ecId || ''}_Dropdown@w1yrw7`} overlay={this.batchOpenStatus == true ? <Menu ecId={`${this && this.props && this.props.ecId || ''}_Menu@olgd3p`} /> : this.getDropDownMenu(type)} trigger={['click']} >
					{this.batchOpenStatus == true ? <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@2edy0r`} type="primary" onClick={() => {
						this.getForm('unblock', i18n.label.confirmBatchUnblock())
					}} disabled={this.getUnblockStatus}>{i18n.label.batchUnblock()}</Button> : <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@m2oqrp`} type='primary' disabled={this.multiOp}>{i18n.button.batchOp()}</Button>}
				</Dropdown>
			</Dropdown.Button>)
		}
		const menus = [{
			key: '1',
			content: i18n.button.save(),
			icon: <i className={this.menuIconCollection.save} />,
			onClick: () => {
				this.saveEdit && this.saveEdit()
			}
		},
		...this.transDropDownMenu(type),
		{
			key: '2',
			content: i18n.button.columnVisibleSetting(),
			icon: <i className={this.menuIconCollection.setting} />,
			onClick: () => {
				this.definedColumnVisible && this.definedColumnVisible()
			}
		},
		];
		if (type != 'resource') {
			buttons.push(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@42796nExport`} type='primary' onClick={() => this.exportData && this.exportData()}>{i18n.button.export()}</Button>);
			menus.push({
				key: 'export',
				content: i18n.button.export(),
				icon: <i className={this.menuIconCollection.export} />,
				onClick: () => this.exportData && this.exportData()
			})
		}
		menus.push(...this.getBasicMenus(logType));

		return { buttons, menus }
	}

	topMenuDef = () => {
		return {
			adjust: {
				buttons: [
					<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@e6sz8j`} type='primary' onClick={() => this.saveAdjust && this.saveAdjust()}>{i18n.button.saveSelectedData()}({this.selectedFormItemCount})</Button>
				],
				menus: [{
					key: '1',
					content: i18n.button.saveSelectedData(),
					icon: <i className={this.menuIconCollection.save} />,
					onClick: () => {
						this.saveAdjust && this.saveAdjust()
					}
				}, {
					key: '2',
					content: i18n.button.log(),
					icon: <i className={this.menuIconCollection.log} />,
					onClick: () => this.showOpLog()
				}, ...this.getBasicMenus()]
			},
			edit: {
				subCompany: this.getBtnsAndMenus('subCompany'),
				department: this.getBtnsAndMenus('department'),
				resource: this.getBtnsAndMenus('resource')
			},
			uploadPhoto: {
				buttons: [
					<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@j04zto`} type='primary' onClick={() => this.multiUpload && this.multiUpload()}>{i18n.label.batchUpload()}</Button>,
					<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@wneax5`} type='primary' onClick={() => this.multiDelete && this.multiDelete()} disabled={this.multiOp}>{i18n.button.multiDelete()}</Button>,
				],
				menus: [{
					key: '1',
					content: i18n.label.batchUpload(),
					icon: <i className={this.menuIconCollection.save} />,
					onClick: () => {
						this.multiUpload && this.multiUpload()
					}
				}, {
					key: '2',
					content: i18n.button.multiDelete(),
					icon: <i className={this.menuIconCollection.log} />,
					onClick: () => {
						this.multiDelete && this.multiDelete()
					},
					disabled: this.multiOp
				}, {
					key: '3',
					content: i18n.button.columnVisibleSetting(),
					icon: <i className={this.menuIconCollection.setting} />,
					onClick: () => {
						this.definedColumnVisible && this.definedColumnVisible()
					}
				}, ...this.getBasicMenus('HRM_ENGINE_BATCH_PHOTO_UPLOAD')]
			}
		}
	}

	/********************* unobservable list *********************/

	/********************* dialog info setting *********************/

	/********************* dialog info setting *********************/

	/********************* observable list *********************/
	@observable routeChanged = new Date().getTime();
	@observable selectedFormItemCount = 0;
	@observable multiOpDisable = true;
	@observable UnblockStatus = true;
	@observable spinning = false;
	@observable batchOpenStatus = false;
	/********************* observable list *********************/

	/********************* computed list *********************/
	@computed get multiOp () {
		return this.multiOpDisable;
	}
	
	@computed get getUnblockStatus () {
		if(this.UnblockStatus&&this.multiOpDisable==false){
				return false
		}else{
			return true
		}
	
	}
	/********************* computed list *********************/

	/********************* action list *********************/
	initData = () => {
		this.containerInitFinished = {
			...this.containerInitFinished,
			init: true
		}
		this.batchOpenStatus = false;
	}

	onRouteChange = (props) => {
		this.routeProps = {
			...this.routeProps,
			...props
		}
		this.routeChanged = new Date().getTime();
	}

	translateLocation = () => {
		let key = `batch${upperFirst(this.routeProps.location)}${upperFirst(this.routeProps.params)}`;
		return i18n.module[key] && i18n.module[key]();
	}

	setSelectedFormItemCount = (v) => {
		this.selectedFormItemCount = v;
	}

	setSpinning = (val) => {
		this.spinning = val;
	}

	setMultiOpDisable = (v) => {
		this.multiOpDisable = v;
	}

	setUnblockStatus = (v) => {
		this.UnblockStatus = v;
	}

	@action saveAdjust = () => { }
	@action saveEdit = () => { }
	@action getForm = () => { }
	@action saveForm = () => { }
	@action showOpLog = () => { }
	@action definedColumnVisible = () => { }
	@action multiUpload = () => { }
	@action multiDelete = () => { }
	@action exportData = () => { }
	@action getListDatas = () => { }
	@action generateTableReqParams = () => { }
	@action updateForm=()=>{}
	@action getFormStatus=()=>{}
	@action changeBatchOpenStatus = (status) => {
		this.batchOpenStatus = status;
		if(status==false){
			this.updateForm('')
		}else{
				this.updateForm('1')
		}
	
	}


	/********************* action list *********************/
}