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

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

import {
	WeaForm,
} from 'comsMobx';

import {
	message
} from 'antd';

export class HrmSingleJobFormStore {
	@observable jobId = '';
	@observable date = '';
	@observable job = {
		condition: [],
		form: new WeaForm(),
		loading: true,
		authorized: false,
	};
	@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.job.authorized = hasRight;
				this.getJobForm();
			} else {
				message.error(rs.message);
			}
		}).catch(error => {
			message.error(error);
		});
	}

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

			if (api_status) {
				if (condition) {
					!this.job.authorized && this.setFormReadOnly(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
	saveJobForm = () => {
		this.job.form.validateForm().then(f => {
			if (f.isValid) {
				let params = {
					...this.job.form.getFormParams(),
					id: this.jobId
				};
				Api.saveJobForm(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
	setFormReadOnly = (condition) => {
		condition.map(c => {
			c.items.forEach(item => {
				item.viewAttr = 1;
			});
		});
	}

	@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
	setJobId = (id) => {
		this.jobId = id;
	}

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

}