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

export class HrmContractMaintain {
	@observable main = {
		btnMenu: [],
		authorized: false,
		loading: true,
		hasTree: false,
		title: i18n.module.contractMaintain(),
	}
	@observable formInfo = {
		conditions: [],
		form: new WeaForm(),
		loading: true,
		date: '',
		isHireContract: false,
		ids: [],
		names: [],
	}
	@observable mainTable = {
		tableStore: new TableStore(),
		selectedRowKeys: '',
	}
	@observable mainDialog = {
		title: '',
		visible: false,
	}
	@observable mainTab = {
		isPanelShow: false,
		form: new WeaForm(),
		conditions: [],
		loading: true,
		conditionCount: observable.ref(8),
	}
	@observable leftTree = {
		nodeId: '',
		triggerRefresh: false,
	}
	@observable richText = {
		value: '',
		loading: true,
		accessorynum: 0,
	}

	@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 = this.mainTab.form.getFormParams();

		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
	getFormInfo = (type) => {
		let func;
		let params = {};

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

		Api[func](params).then(cb => {
			const {
				api_status,
				conditions,
				doccontent,
				isHireContract,
				doc,
			} = cb;

			if (api_status) {
				if (!this.mainDialog.visible) {
					this.mainTab.conditions = conditions;
					this.mainTab.form.initFormFields(conditions);
					this.mainTab.loading = false;
				} else {
					this.adjustCondition(conditions);
					this.formInfo.conditions = conditions;
					this.formInfo.form.initFormFields(conditions);
					this.formInfo.loading = false;
					this.richText.value = doccontent;
					this.formInfo.isHireContract = isHireContract;
					if (doc) {
						let ids = Object.keys(doc),
							names = Object.values(doc);

						this.formInfo.ids = ids;
						this.formInfo.names = names;
					}
				}
			} else {
				message.error(cb.message);
			}
		}).catch(error => {
			message.error(error);
		});
	}

	@action
	adjustCondition = (conditions) => {
		conditions.map(c => {
			c.items.map(item => {
				const key = item.domkey[0];
				if (key == 'contractStartDate') {
					item.domkey.map((key, index) => {
						item[key] = item.value[index]
					})
				}
			});
		});
	}

	@action
	deleteTableInfo = () => {
		const params = {
			contractIds: this.mainTable.selectedRowKeys
		}
		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) {
				const {
					form,
					ids,
				} = this.formInfo, {
					value,
					attachId,
					accessorynum,
				} = this.richText;

				let params = form.getFormParams();

				Object.assign(params, {
					doccontent: value,
					fileids: attachId && ids.length > 0 ? `${attachId},${ids.toString()}` : attachId && ids.length == 0 ? attachId : ids.toString(),
					accessorynum: (accessorynum == 0 || accessorynum == 1) ? 1 : accessorynum,
				});

				if (this.mainTable.selectedRowKeys) {
					Object.assign(params, {
						contractId: this.mainTable.selectedRowKeys,
						operation: 'editsave'
					});
				} else {
					Object.assign(params, {
						operation: 'addsave'
					});
				}

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


				Api.save(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(new Date());
			}
		});
	}

	@action
	getContractTypeInfo = (id) => {
		const params = {
			contractTypeId: id
		}

		Api.getContractTypeInfo(params).then(cb => {
			const {
				api_status,
				doccontent,
				isHireContract,
			} = cb;

			if (api_status) {
				this.formInfo.isHireContract = isHireContract;
				this.richText.value = doccontent;
			} else {
				message.error(cb.message);
			}
		}).catch(error => {
			message.error(error);
		});
	}

	@action
	delAttach = (index) => {
		this.formInfo.ids.splice(index, 1);
		this.formInfo.names.splice(index, 1);
	}

	@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) => {
		id && this.setSelectedRowKeys(id);
		this.mainDialog.visible = true;
		this.mainDialog.title = (!this.mainTable.selectedRowKeys) ? i18n.label.newContractMaintain() : i18n.label.editContractMaintain();
	}

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

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

	@action
	changeSearchPanelStatus = (bool) => {
		this.mainTab.isPanelShow = bool;
	}

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

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

	@action
	refreshSearch = () => {
		this.mainTab.isPanelShow = false;
		this.mainTab.form.reset();
	}

	@action
	refreshFormInfo = () => {
		this.formInfo.form = new WeaForm();
		this.formInfo.conditions = [];
		this.formInfo.loading = true;
		this.formInfo.ids = [];
		this.formInfo.names = [];
	}

	@action
	refreshRichText = () => {
		this.richText.loading = true;
		this.richText.value = '';
		this.richText.attachId = '';
	}

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

	@action
	setDate = (date) => {
		this.formInfo.date = date;
	}

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

	@action
	handleRichTextChange = (val) => {
		this.richText.value = val;
	}

	@action
	setRichTextStatus = (status) => {
		if (status == 'ready') this.richText.loading = false;
	}

	@action
	exportAttachId = (ids, list) => {
		this.richText.attachId = ids;
		this.richText.accessorynum = list.length;
	}

	openContractType = (id) => {
		const dialog = WeaTools.createDialog({
			title: i18n.module.contractType(),
			moduleName: 'hrm',
			url: addContentPath(`/spa/hrm/engine.html#/hrmengine/ctFormInfo?id=${id}`),
			style: {
				width: 820,
				height: 450
			},
			callback: (datas) => {},
			onCancel: () => {}
		});
		dialog.show();
	}


}