import {
	observable,
	action,
	computed
} from 'mobx';
import {
	WeaTableNew
} from 'comsMobx';
import {
	Button,
	message
} from 'antd';
import FormInfo from '../coms/FormInfo';
import HrmBaseStore from './baseStore';
import * as api from '../apis/annual';
import {
	i18n
} from '../public/i18n';

const {
	TableStore
} = WeaTableNew;

export class HrmAnnualPeriod extends HrmBaseStore {
	/********************* unobservable list *********************/
	topBtnAndMenu = () => {
		let btns = [
			<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@s1nide`} type='primary' op='create' onClick={() => this.create()}>{i18n.button.create()}</Button>,
			<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@87xjcb`} type='primary' op='remove' disabled={this.tableDataOperational} onClick={() => this.remove()}>{i18n.button.multiDelete()}</Button>,
			<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@0ei4ll`} type='primary' op='sync' onClick={() => this.sync()}>{i18n.button.syncToSubDept()}</Button>,
		]
		let menus = [];
		btns.map((btn, index) => {
			const {
				props
			} = btn;
			menus.push({
				key: index.toString(),
				content: props.children,
				icon: <i className={this.menuIconCollection[props.op]}/>,
				onClick: props.onClick,
				disabled: props.disabled || false
			});
		})
		menus.push({
			key: '11',
			content: i18n.button.columnVisibleSetting(),
			icon: <i className={this.menuIconCollection.setting}/>,
			onClick: () => this.definedColumn(this.table)
		})
		return {
			btns,
			menus: [...menus, ...this.getBasicMenus('HRM_ENGINE_HRM_ANNUAL_MANAGER_SET')]
		}
	}

	orgTreeProps = {
		needSearch: true,
		isLoadAllSub: true,
		topPrefix: 'hrmSearch',
		inputLeftDom: `<b>${i18n.label.organization()}</b>`,
		// needDropMenu: true,
		treeNodeClick: this.treeNodeClick,
		params: {
			rightStr: 'AnnualPeriod:All'
		}
	}

	selectedSubCompany;
	periodFormFields;

	editorDialogRightMenu = [];
	getDialogOpButtons = () => {
		let buttons = [];
		this.editorDialogRightMenu.length = 0;
		buttons.push(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@yckzb3`} type="primary" op="save" onClick={() => this.save()}>{i18n.button.save()}</Button>);
		buttons.map((btn, index) => {
			const {
				props
			} = btn;
			this.editorDialogRightMenu.push({
				key: index.toString(),
				content: props.children,
				icon: <i className={this.menuIconCollection[props.op]}/>,
				onClick: props.onClick
			});
		})

		this.dialogParams.moreBtn.datas = this.editorDialogRightMenu;
		return buttons;
	}
	/********************* unobservable list *********************/

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

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

	/********************* observable list *********************/
	@observable table = new TableStore();

	dialogParams = {
		...this.dialogPropsDef,
		opType: '',
		style: {
			width: 500,
			height: 165
		},
		onCancel: () => {
			this.dialogParams.visible = false;
			this.opId = null;
		}
	}
	/********************* observable list *********************/

	/********************* computed list *********************/
	@computed get tableDataOperational() {
		return this.table.selectedRowKeys.length === 0;
	}
	/********************* computed list *********************/

	/********************* action list *********************/
	initData = () => {
		this.selectedSubCompany = null;

		this.containerInitFinished = {
			...this.containerInitFinished,
			init: true
		}
		this.getTableData();
	}

	@action treeNodeClick = (event, ids, nodeids, nodes) => {
		const {
			data: nodeData
		} = event.node.props;
		this.selectedSubCompany = nodeData;
		this.getTableData();
	}

	getTableData = () => {
		const params = {}
		this.selectedSubCompany && Object.assign(params, {
			subcompanyid: this.selectedSubCompany.id
		});
		api.getAnnualManagerSetList(params).then(data => {
			if (data.status === '1') {
				this.table = new TableStore();
				this.table.getDatas(data.sessionkey, 1);
			} else {
				message.error(data.message);
			}
		}, error => {})
	}

	getDataForm = (params, showDialog = false, create) => {
		create && Object.assign(params, {
			subcompanyid: this.selectedSubCompany ? this.selectedSubCompany.id : '0',
		})
		api.getAnnualManagerSetForm(params).then(data => {
			if (data.status === '1') {
				data.formField.map(f => {
					f.items.map((item, i) => {
						if (i == 0) {
							item.length = 4;
						}
					})
				})
				this.setFormData('dataForm', data.formField);
				showDialog && this.showDialog(create);
			} else {
				message.error(data.message);
			}
		})
	}

	@action create = (create = true, params = {}) => {
		if (!create) {
			this.opId = params.id;
		}
		this.getDataForm(params, true, create);
	}

	showDialog = (create) => {
		if (this.formTarget.dataForm.isFormInit) {
			this.dialogParams.coms = (
				<FormInfo ecId={`${this && this.props && this.props.ecId || ''}_FormInfo@nf4bob`} center={false} menu={this.editorDialogRightMenu} 
					form={this.formTarget.dataForm} 
					formFields={this.formTarget.dataFormFields}/>
			)
			this.dialogParams.title = (create ? i18n.label.createAnnualPeriod() : i18n.label.editAnnualPeriod());
			this.dialogParams.visible = true;
		}
	}

	@action save = () => {
		this.formTarget.dataForm.validateForm().then(f => {
			if (f.isValid) {
				const formParams = this.formTarget.dataForm.getFormParams();
				this.valid = true;
				if (!/^\d+$/.test(formParams.annualyear)) {
					this.valid = false;
					f.showError('annualyear', i18n.message.mustIntVal());
				} else if (this.moment(formParams.startdate) > this.moment(formParams.enddate)) {
					this.valid = false;
					f.showError('enddate', i18n.message.expiryLessThanEffective());
				}
				if (this.valid) {
					const params = {
						id: this.opId != null ? this.opId : null,
						subcompanyid: this.selectedSubCompany ? this.selectedSubCompany.id : null,
						...formParams
					}
					this.dialogParams.onCancel();
					api.saveAnnualManagerSet(params).then(data => {
						if (data.status === '1') {
							this.table.getDatas(null, 1);
							message.success(i18n.message.saveSuccess());
						} else
							message.error(data.message);
					}, error => {});
				}
			} else {
				f.showErrors();
				this.showError = new Date().getTime();
			}
		});
	}

	@action remove = (params) => {
		let content;
		if (params == null) {
			params = {
				ids: this.table.selectedRowKeys
			}
			content = i18n.confirm.deleteSelected();
		} else {
			content = i18n.confirm.delete();
		}
		Object.assign(params, {
			subcompanyid: this.selectedSubCompany ? this.selectedSubCompany.id : null
		})
		this.confirmInfo({
			content,
			onOk: () => {
				api.deleteAnnualManagerSet(params).then(data => {
					if (data.status === '1') {
						message.success(i18n.message.deleteSuccess());
						this.table.getDatas(null, 1);
					}
				})
			}
		});
	}

	@action sync = () => {
		if (this.table.selectedRowKeys.length == 0) {
			message.error(i18n.message.syncDataNone());
			return;
		}
		this.confirmInfo({
			content: i18n.confirm.syncAnnualPeriod(),
			onOk: () => {
				const params = {
					ids: this.table.selectedRowKeys,
					subcompanyid: this.selectedSubCompany ? this.selectedSubCompany.id : null
				}
				api.synAnnualManagerSet(params).then(data => {
					if (data.status === '1') {
						message.success(i18n.message.syncSuccess());
						this.getTableData();
					}
				})
			}
		});
	}
	/********************* action list *********************/
}

export const hrmAnnualPeriod = new HrmAnnualPeriod();