import * as Api from '../apis/contractExpireWarning';
import {
	toJS
} from 'mobx';
import {
	observable,
	action,
	computed,
} from 'mobx';
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 HrmConExWarning {
	@observable main = {
		btnMenu: [],
		authorized: false,
		title: i18n.module.contractExpireWarning(),
		loading: true,
	}
	@observable mainDialog = {
		title: i18n.label.addWarningObject(),
		visible: false,
	}
	@observable mainForm = {
		form: new WeaForm(),
		conditions: [],
		loading: true,
		explain: '',
	}
	@observable subForm = {
		conditions: [],
		loading: true,
	}
	@observable table = {
		tableStore: new TableStore(),
	}

	@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
			} = 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
	grantAuth = () => {
		this.main.authorized = true;
	}

	@action
	getContentInfo = () => {
		this.table.tableStore = new TableStore();
		Api.getContentInfo().then(cb => {
			const {
				api_status,
				sessionkey,
				conditions,
				explain,
			} = cb;

			if (api_status) {
				this.adjustConditions(conditions);
				this.mainForm.conditions = conditions;
				this.mainForm.form.initFormFields(conditions);
				this.mainForm.explain = `${explain}<a href=${addContentPath('/spa/hrm/engine.html#/hrmengine/contractType')} target='_blank'>${i18n.label['527626']()}</a>`;
				this.table.tableStore.getDatas(sessionkey, 1);
				this.mainForm.loading = false;
			} else {
				message.error(cb.message);
			}
		}).catch(error => {
			message.error(error);
		});
	}

	@action
	adjustConditions = (conditions) => {
		conditions.map(c => {
			c.items.map(item => {
				if (item.conditionType == 'CHECKBOX') {
					item.otherParams = {
						display: 'switch'
					}
				}
				if (item.conditionType == 'INPUT') {
					item.conditionType = 'INPUTNUMBER'
				}
			})
		})
	}

	@action
	getFormInfo = () => {
		Api.getFormInfo().then(cb => {
			const {
				api_status,
				conditions,
			} = cb;

			if (api_status) {
				this.subForm.conditions = conditions;
				this.subForm.loading = false;
			} else {
				message.error(cb.message);
			}
		}).catch(error => {
			message.error(error);
		});
	}

	@action
	save = () => {
		let params;
		let func;

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

				if (api_status) {
					sign == '1' ? message.success(cb.message) : message.warning(cb.message)
					if (this.mainDialog.visible) {
						this.getContentInfo();
						this.closeDialog();
					}
				} else {
					message.warning();
				}
			}).catch(error => {
				message.error(error);
			})
		}

		if (!this.mainDialog.visible) {
			params = this.mainForm.form.getFormParams();
			func = 'saveMainForm';
			this.mainForm.form.validateForm().then(f => {
				if (f.isValid) {
					call();
				}else{
					f.showErrors();
				}
			})
		} else {
			params = this.subForm.params;
			func = 'saveSubForm';
			call();
		}


	}

	@action
	deleteTableInfo = () => {
		const params = {
			ids: this.table.tableStore.selectedRowKeys
		}
		Api.deleteTableInfo(params).then(cb => {
			const {
				api_status,
				sign,
			} = cb;

			if (api_status) {
				sign == '1' ? message.success(cb.message) : message.warning(cb.message);
				this.getContentInfo();
			} else {
				message.warning(i18n.message.actionError());
			}
		}).catch(error => {
			message.error(error);
		});
	}

	@action
	openDialog = () => {
		this.mainDialog.visible = true;
		this.getFormInfo();
	}

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

	@action
	refreshPage = () => { //刷新页面
		this.mainForm.loading = true
		this.getContentInfo();
	}

	@action
	refreshFormInfo = () => {
		this.subForm.conditions = [];
		this.subForm.loading = true;
	}

	@action
	setParams = (datas) => {
		this.subForm.params = datas;
	}

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

}