import * as Api from '../apis/attendanceSetting';
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 HrmAttendanceSetting {
	@observable main = {
		authorized: false,
		title: i18n.module.attendanceSetting(),
		loading: true,
	}
	@observable mainDialog = {
		title: i18n.label.attendanceRulesSetting(),
		visible: false,
		contentType: 'new',
	}
	@observable pageFormInfo = {
		form: new WeaForm(),
		conditions: [],
		loading: true,
		prompts: [],
		id: '',
	}
	@observable subFormInfo = {
		form: new WeaForm(),
		conditions: [],
		loading: true,
		prompts: [],
		date: '',
	}
	@observable tableEdit = {
		tableStore: new TableStore(),
	}

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

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

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

	@action
	closeAuth = () => {
		this.main.authorized = false;
	}

	@action
	getContentInfo = () => {
		this.tableEdit.tableStore = new TableStore();
		Api.getContentInfo().then(cb => {
			const {
				status,
				sessionkey,
				items,
				id,
			} = cb;


			if (status == '1') {
				items[0].items[1].precision = 1;
				const cs = items,
					prompts = items[0].prompts;

				this.pageFormInfo.conditions = cs;
				this.pageFormInfo.form.initFormFields(cs);
				this.pageFormInfo.prompts = prompts;

				this.pageFormInfo.id = id;

				this.tableEdit.tableStore.getDatas(sessionkey, 1);

				this.stoploading('content');
			} else {
				message.error(cb.message);
			}
		}).catch(error => {
			message.error(error);
		});
	}

	@action
	getSubFormInfo = () => {
		Api.getSubFormInfo().then(cb => {
			const {
				title,
				defaultshow,
				items,
				prompts,
				status
			} = cb;

			items.splice(-1, 1); //脏数据

			if (status == '1') {
				const cs = [{
					title: title,
					defaultshow: defaultshow,
					items: items,
				}];

				this.addRules(cs);

				this.subFormInfo.conditions = cs;
				this.subFormInfo.form.initFormFields(cs);
				this.subFormInfo.prompts = prompts;

				this.stoploading('sub');
			} else {
				message.error(cb.message);
			}
		}).catch(error => {
			message.error(error);
		});
	}

	@action
	addRules = (cs) => {
		cs.map(c => {
			c.items.map(item => {
				const key = item.domkey[0];
				if (key == 'showseclevel' || key == 'reportname') {
					item.viewAttr = 3;
				}
				// if (key == 'showseclevel') item.rules = 'scopeRequired|scopeDirty';
				if (key == 'reportname') item.rules = 'required|string';

			});
		})
	}

	@action
	recoverloading = (type) => {
		if (type == 'content') this.pageFormInfo.loading = true;

		if (type == 'sub') this.subFormInfo.loading = true;
	}

	@action
	stoploading = (type) => {
		if (type == 'content') this.pageFormInfo.loading = false;

		if (type == 'sub') this.subFormInfo.loading = false;
	}

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

		if (type == 'main') {
			form = this.pageFormInfo.form;
			func = 'saveMainForm';
			params = {
				id: this.pageFormInfo.id,
				type: '0',
				...this.pageFormInfo.form.getFormParams()
			};
		} else {
			form = this.subFormInfo.form;
			func = 'saveSubForm';
			params = this.subFormInfo.form.getFormParams();

		}

		form.validateForm().then(f => {
			const {
				deepValidate
			} = this;

			if ((type == 'sub' && deepValidate(f)) || type == 'main') {
				Api[func](params).then(cb => {
					const {
						status,
						errorMsg,
					} = cb;

					if (status == '1') {
						message.success(i18n.message.saveSuccess());
						if (type == 'sub') {
							this.getContentInfo();
							this.closeDialog();
						}
					} else {
						message.warning(errorMsg);
					}
				}).catch(error => {
					message.error(error);
				})
			} else {
				// f.showErrors();
				this.setDate({
					date: new Date()
				});
			}
		})
	}

	@action
	deepValidate = (f) => {
		const params = this.subFormInfo.form.getFormParams();

		const {
			showseclevel,
			showseclevelto,
			reportname
		} = params;

		const keys = [...Object.keys(params)];
		for (let i = 0; i < keys.length; i++) {
			const k = keys[i];
			if (k === 'showseclevel' && (!showseclevel || !showseclevelto)) {
				f.showError('showseclevel__showseclevelto', `' ${'分钟'} '必填`);
				return false;
			} else if (k == 'reportname' && !reportname) {
				f.showError('reportname', `' ${'报表字段名称'} '必填`);
				return false
			}
		}

		return true;
	}

	@action
	deleteTableInfo = () => {
		const params = {
			ids: this.tableEdit.tableStore.selectedRowKeys
		}
		Api.deleteTableInfo(params).then(cb => {
			const {
				status
			} = cb;

			if (status == '1') {
				message.success(i18n.message.deleteSuccess());
				this.getContentInfo();
			} else {
				message.warning(i18n.message.actionError());
			}
		}).catch(error => {
			message.error(error);
		});
	}

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

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

	@action
	refreshPage = () => { //刷新页面
		this.recoverloading('content');
		this.getContentInfo();
	}

	@action
	refreshSubFormInfo = () => {
		this.subFormInfo.form = new WeaForm();
		this.subFormInfo.conditions = [];
		this.recoverloading('sub');
	}

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

}