import * as Api from '../apis/contractTemplate';
import {
	toJS
} from 'mobx';
import {
	observable,
	action,
	computed,
} from 'mobx';
import {
	WeaForm,
	WeaTableNew,
} from 'comsMobx';
import {
	WeaTools,
} from 'ecCom';
import {
	message
} from 'antd';
import {
	i18n
} from '../public/i18n';
import {addContentPath} from '../util/index.js'
const {
	TableStore
} = WeaTableNew;

export class HrmContractTemplate {
	@observable main = {
		btnMenu: [],
		authorized: false,
		loading: true,
		hasTree: false,
		title: i18n.module.contractTemplate(),
	}
	@observable mainTable = {
		tableStore: new TableStore(),
		selectedRowKeys: '',
	}
	@observable mainDialog = {
		title: '',
		visible: false,
	}
	@observable mainTab = {
		baseSearchValue: ''
	}
	@observable leftTree = {
		nodeId: '',
		triggerRefresh: false,
	}

	@computed get menu() {
		let topMenu = this.main.btnMenu.filter(item => item.isTop == '1' || item.isBatch == '1');
		let rightMenu = this.main.btnMenu.filter(item => item);

		return {
			topMenu,
			rightMenu
		}
	}

	@action
	getAuth = () => {
		Api.getAuth().then(cb => {
			const {
				status,
				hasRight,
				hasTree,
			} = cb;

			if (status == '1') {
				if (hasRight) {
					this.grantAuth();
					this.main.hasTree = hasTree;
					this.clickTreeNode('0', '0');
				}
				this.main.loading = false;
			} else {
				message.error(cb.message);
			}
		}).catch(error => {
			message.error(error);
		});
	}

	@action
	getButtonsMenu = () => {
		let params = {};
		if (this.main.hasTree && this.leftTree.nodeType !== '0')
			Object.assign(params, {
				subcompanyId: this.leftTree.nodeId
			});
		Api.getButtonsMenu(params).then(cb => {
			const {
				api_status,
				btnMenu,
			} = cb;

			if (api_status) {
				this.main.btnMenu = btnMenu;
			} else {
				message.error(cb.message);
			}
		}).catch(error => {
			message.error(error);
		});
	}

	@action
	getTableInfo = () => {
		this.mainTable.tableStore = new TableStore(); //主要为了清除选中项

		const params = {
			moduleName: this.mainTab.baseSearchValue
		};

		if (this.main.hasTree && this.leftTree.nodeType !== '0')
			Object.assign(params, {
				subcompanyId: this.leftTree.nodeId
			});

		Api.getTableInfo(params).then(cb => {
			const {
				api_status,
				sessionkey,
			} = cb;

			if (api_status) {
				sessionkey && this.mainTable.tableStore.getDatas(sessionkey, 1);
			} else {
				message.error(cb.message);
			}
		}).catch(error => {
			message.error(error);
		});
	}

	@action
	deleteTableInfo = () => {
		const params = {
			contractIds: this.mainTable.selectedRowKeys //todo
		}
		if (this.main.hasTree && this.leftTree.nodeType !== '0')
			Object.assign(params, {
				subcompanyId: this.leftTree.nodeId
			});
		Api.deleteTableInfo(params).then(cb => {
			const {
				api_status,
				sign
			} = cb;

			if (api_status) {
				if (sign == '1') {
					message.success(cb.message);
					this.getTableInfo();
					this.resetSelectedRowKeys();
				} else {
					message.warning(cb.message)
				}
			}
		}).catch(error => {
			message.error(error);
		});
	}

	@action
	save = () => {
		this.formInfo.form.validateForm().then(f => {
			if (f.isValid) {
				let func;
				let params = this.formInfo.form.getFormParams();

				if (this.mainTable.selectedRowKeys) {
					func = 'edit';
					Object.assign(params, {
						contractId: this.mainTable.selectedRowKeys
					});
				} else {
					func = 'add';
				}
				if (this.main.hasTree && this.leftTree.nodeType !== '0')
					Object.assign(params, {
						subcompanyId: this.leftTree.nodeId
					});

				Api[func](params).then(cb => {
					const {
						api_status,
						sign
					} = cb;

					if (api_status) {
						if (sign == '1') {
							message.success(cb.message);
							this.closeDialog();
							this.getTableInfo();
						} else {
							message.warning(cb.message)
						}
						this.formInfo.loading = false;
					}
				}).catch(error => {
					message.error(error);
				});
			} else {
				f.showErrors();
				this.setDate({
					date: new Date()
				});
			}
		});
	}

	@action
	clickTreeNode = (id, type, name) => {
		this.setNodeInfo(id, type);
		this.refreshSearch();
		this.getTableInfo();
		this.getButtonsMenu();
	}

	@action
	setNodeInfo = (id, type) => {
		this.leftTree.nodeId = id;
		this.leftTree.nodeType = type;
	}

	@action
	openDialog = (id) => {
		const dialog = WeaTools.createDialog({
			title: id ? i18n.label.editContractTemplate() : i18n.label.newContractTemplate(),
			icon: "icon-coms-hrm",
			iconBgcolor: "#217346",
			url: addContentPath('/spa/document/engine.html#/docengine/mould/contract'),
			style: {
				width: 600,
				height: 600
			},
			callback: () => {
				this.getTableInfo()
			},
		});
		dialog.show();

	}

	@action
	closeDialog = () => {
		this.mainDialog.visible = false;
		this.resetSelectedRowKeys();
	}

	@action
	resetSelectedRowKeys = () => {
		this.mainTable.selectedRowKeys = '';
	}

	@action
	grantAuth = () => {
		this.main.authorized = true;
	}

	@action
	refreshPage = () => {
		this.forceLeftTreeRefresh();
		this.clickTreeNode('0', '0');
	}

	@action
	refreshSearch = () => {
		this.mainTable.baseSearchValue = '';
	}

	@action
	forceLeftTreeRefresh = () => {
		this.leftTree.triggerRefresh = !this.leftTree.triggerRefresh;
	}

	@action
	setSelectedRowKeys = (keys) => {
		this.mainTable.selectedRowKeys = keys;
	}

	@action
	setSearchBaseValue = (val) => {
		this.mainTab.baseSearchValue = val;
	}

}