import * as Api from '../apis/assType';
import {
	toJS
} from 'mobx';
import {
	observable,
	action,
	computed,
} from 'mobx';
import {
	WeaForm,
	WeaTableNew
} from 'comsMobx';
import {
	message
} from 'antd';
import {
	i18n
} from '../public/i18n';
const {
	TableStore
} = WeaTableNew;

export class HrmAssType {
	@observable main = {
		btnMenu: [],
		authorized: false,
		loading: true,
		title: i18n.module.assType(),
	}
	@observable formInfo = {
		conditions: [],
		form: new WeaForm(),
		loading: true,
		date: '',
	}
	@observable mainTable = {
		tableStore: new TableStore(),
		selectedRowKeys: '',
	}
	@observable mainDialog = {
		title: '',
		visible: false,
	}
	@observable mainTab = {
		isPanelShow: false,
		form: new WeaForm(),
		conditions: [],
		loading: true,
		conditionCount: observable.ref(2),
	}
	@observable tableEdit = {
		postDatas: [],
		itemDatas: [],
		personDatas: [],
	}

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

		return {
			topMenu,
			rightMenu
		}
	}

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

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

	@action
	getButtonsMenu = () => {
		Api.getButtonsMenu().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 = () => {
		const params = this.mainTab.form.getFormParams();

		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';
			params = {};
		} else {
			func = 'getForm';
			params = {};
			if (this.mainTable.selectedRowKeys) {
				Object.assign(params, {
					checkTypeId: this.mainTable.selectedRowKeys
				});
			}
		}

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

			if (api_status) {
				if (!this.mainDialog.visible) {
					this.mainTab.conditions = conditions;
					this.mainTab.form.initFormFields(conditions);
					this.mainTab.loading = false;
				} else {
					this.mainTable.selectedRowKeys && this.distributeDatas(tableData);
					this.formInfo.conditions = conditions;
					this.formInfo.form.initFormFields(conditions);
					this.formInfo.loading = false;
				}
			} else {
				message.error(cb.message);
			}
		}).catch(error => {
			message.error(error);
		});
	}

	@action
	distributeDatas = (tableData) => {
		tableData.map(td => {
			if (td.group == 'jobtitle') this.tableEdit.postDatas = td.datas;
			if (td.group == 'checkItem') this.tableEdit.itemDatas = td.datas;
			if (td.group == 'reward') this.tableEdit.personDatas = td.datas;
		});
	}

	@action
	deleteTableInfo = () => {
		const params = {
			checkTypeIds: this.mainTable.selectedRowKeys
		}
		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, {
						checkTypeId: this.mainTable.selectedRowKeys
					});
				} else {
					func = 'add';
				}

				params = Object.assign(params, this.jsonTranslation());

				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
	jsonTranslation = () => {
		const {
			postDatas,
			itemDatas,
			personDatas
		} = this.tableEdit;

		let obj = {};
		postDatas.map((data, index) => {
			obj[`jobtitleId_${index}`] = data.jobtitleId;
			obj.jobtitleRowNum = postDatas.length;
		});

		itemDatas.map((data, index) => {
			obj[`checkItemId_${index}`] = data.checkItemId;
			obj[`checkItemProportion_${index}`] = data.checkItemProportion;
			obj.checkItemRowNum = itemDatas.length;
		});

		personDatas.map((data, index) => {
			obj[`typeId_${index}`] = data.cascader[0];
			if (data.cascader.length > 1) obj[`resourceId_${index}`] = data.cascader[1].value;
			obj[`checkProportion_${index}`] = data.checkProportion;
			obj.rewardRowNum = personDatas.length;
		});

		return obj
	}

	@action
	openDialog = () => {
		this.mainDialog.visible = true;
		this.mainDialog.title = (!this.mainTable.selectedRowKeys) ? i18n.label.newAssType() : i18n.label.editAssType();
	}

	@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.mainTab.isPanelShow = false;
		this.mainTab.form.reset();
		this.getTableInfo();
	}

	@action
	resetSearch = (type) => {
		this.mainTab.isPanelShow = false;
		this.mainTab.form = new WeaForm();
	}

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

	@action
	resetTableEdit = () => {
		this.tableEdit.postDatas = [];
		this.tableEdit.itemDatas = [];
		this.tableEdit.personDatas = [];
	}

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

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

	@action
	setTableEditDatas = (cb, type) => {
		if (type == 'post') this.tableEdit.postDatas = cb;
		if (type == 'item') this.tableEdit.itemDatas = cb;
		if (type == 'person') this.tableEdit.personDatas = cb;
	}

}