import * as mobx from 'mobx';
import * as Api from '../apis/singlePost';

import {
	observable,
	action,
} from 'mobx';

import {
	WeaForm,
} from 'comsMobx';

import {
	message
} from 'antd';

export class HrmSinglePostFormStore {
	@observable auth = false;
	@observable postId = '';
	@observable date = '';
	@observable post = {
		condition: [],
		form: new WeaForm(),
		loading: true,
		authorized: false,
	};
	@observable job = {
		title: '新建职务',
		visible: false,
		condition: [],
		form: new WeaForm(),
		loading: true,
	};
	@observable jobGroup = {
		title: '新建职务类别',
		visible: false,
		condition: [],
		form: new WeaForm(),
		loading: true,
	}

	hide;
	setParentHideMthd = (func) => {
		this.hide = func;
	}

	@action
	getRight = () => {
		Api.getRight().then(rs => {
			const {
				api_status,
				hasRight = true
			} = rs;

			if (api_status) {
				this.post.authorized = hasRight;
				this.getPostForm();
			} else {
				message.error(rs.message);
			}
		}).catch(error => {
			message.error(error);
		});
	}

	@action
	getPostForm = () => {
		this.post.loading = true;
		let params = {
			jobtitleid: this.postId,
		}
		Api.getPostForm(params).then(rs => {
			const {
				api_status,
				condition,
			} = rs;

			if (api_status) {
				if (condition) {
					this.auth = true;
					if (!this.post.authorized) this.setFormReadOnly(condition);
					this.post.condition = condition;
					this.post.form.initFormFields(condition);
				} else {
					this.auth = false;
				}
			} else {
				message.error(rs.message);
			}
			this.post.loading = false;
		}).catch(error => {
			message.error(error);
		});
	}

	@action
	getJobForm = () => {
		this.resetJobForm();
		let params = {
			jobgroupid: this.post.form.getFormParams().jobactivityid,
		}
		Api.getJobForm(params).then(rs => {
			const {
				api_status,
				condition,
			} = rs;

			if (api_status) {
				if (condition) {
					this.job.condition = condition;
					this.job.form.initFormFields(condition);
				}
			} else {
				message.error(rs.message);
			}
			this.job.loading = false;
		}).catch(error => {
			message.error(error);
		});
	}

	@action
	getJobGroupForm = () => {
		this.resetJobGroupForm();
		Api.getJobGroupForm().then(rs => {
			const {
				api_status,
				condition,
			} = rs;

			if (api_status) {
				if (condition) {
					this.jobGroup.condition = condition;
					this.jobGroup.form.initFormFields(condition);
				}
			} else {
				message.error(rs.message);
			}
			this.jobGroup.loading = false;
		}).catch(error => {
			message.error(error);
		});
	}

	@action
	savePostForm = () => {
		this.post.form.validateForm().then(f => {
			if (f.isValid) {
				let params = {
					...this.post.form.getFormParams(),
					id: this.postId
				};
				Api.savePostForm(params).then(rs => {
					const {
						api_status,
						sign,
					} = rs;

					if (api_status) {
						sign == '1' ? message.success(rs.message) && this.hide() : message.warning(rs.message);
					} else {
						message.error(rs.message);
					}
				}).catch(error => {
					message.error(error);
				});
			} else {
				f.showErrors();
				this.setDate({
					date: new Date()
				});
			}
		});
	}

	@action
	saveJobGroupForm = () => {
		this.jobGroup.form.validateForm().then(f => {
			if (f.isValid) {
				let params = this.jobGroup.form.getFormParams();
				Api.saveJobGroupForm(params).then(rs => {
					const {
						api_status,
						sign,
					} = rs;

					if (api_status) {
						sign == '1' ? message.success(rs.message) && this.setJobGroupDialogVisible(false) : message.warning(rs.message);
					} else {
						message.error(rs.message);
					}
				}).catch(error => {
					message.error(error);
				});
			} else {
				f.showErrors();
				this.setDate({
					date: new Date()
				});
			}
		});
	}

	@action
	saveJobForm = () => {
		this.job.form.validateForm().then(f => {
			if (f.isValid) {
				let params = this.job.form.getFormParams();
				Api.saveJobForm(params).then(rs => {
					const {
						api_status,
						sign,
					} = rs;

					if (api_status) {
						sign == '1' ? message.success(rs.message) && this.setJobDialogVisible(false) : message.warning(rs.message);
					} else {
						message.error(rs.message);
					}
				}).catch(error => {
					message.error(error);
				});
			} else {
				f.showErrors();
				this.setDate({
					date: new Date()
				});
			}
		});
	}

	@action
	setFormReadOnly = (condition) => {
		condition.map(c => {
			c.items.forEach(item => {
				item.viewAttr = 1;
				if (item.browserConditionParam && item.browserConditionParam.hasAddBtn) {
					item.browserConditionParam.hasAddBtn = false;
				}
			});
		});
	}

	@action
	setJobDialogVisible = (bool) => {
		this.job.visible = bool;
		bool && this.getJobForm();
	}

	@action
	setJobGroupDialogVisible = (bool) => {
		this.jobGroup.visible = bool;
		bool && this.getJobGroupForm();
	}

	@action
	resetJobForm = () => {
		this.job.loading = true;
		this.job.form = new WeaForm();
	}

	@action
	resetJobGroupForm = () => {
		this.jobGroup.loading = true;
		this.jobGroup.form = new WeaForm();
	}

	@action
	setPostId = (id) => {
		this.postId = id;
	}

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

}