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

import {
	WeaForm
} from 'comsMobx';

import {
	message
} from 'antd'

import {
	i18n
} from '../public/i18n';

import * as mobx from 'mobx';
import * as Api from '../apis/entryMaintenance';
const toJS = mobx.toJS;

export class HrmEntryMaintenanceSet {
	@observable weaTopTitle = i18n.module.entryMaintenanceSettings();
	@observable btnMenu = [];
	@observable authorized = false;
	@observable loading = true;
	@observable date = '';
	@observable content = {
		cLoading: true,
		form: new WeaForm(),
	}

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

	@action
	getRight = () => {
		this.loading = true;
		Api.getRight().then(res => {
			let {
				api_status,
				btnMenu = [],
				hasRight
			} = res;

			if (api_status) {
				if (hasRight) {
					this.authorized = true;
					this.btnMenu = btnMenu;
					this.getForm();
				}
				this.loading = false;
			} else {
				message.error(res.message);
			}
		}).catch(error => {
			message.error(error);
		});
	}

	getForm = () => {
		this.content.cLoading = true;
		Api.getForm().then(res => {
			let {
				api_status,
				condition,
				explain
			} = res;

			if (api_status) {
				if (condition) {
					condition[0].items.forEach(item => {			
						if (item) {
						if (item.browserConditionParam) {
							$.ajax({
								type: "GET",  
								async: false,
								url: "/hrPerson/hrmTb.jsp",
								dataType: "json",
								success: function(data){
									if(data==1){
										item.browserConditionParam.maxLength = 100
									}else{
										item.browserConditionParam.maxLength = 100000
									}
								}
							  })						}
						if (item.domkey[0] == 'changereason') {
							item.otherParams = {
								maxRows: 4
							}
						}
					}});
					this.content.form.initFormFields(condition);
					extendObservable(this.content, {
						condition: condition,
						explanation: explain
					})
				}
				this.content.cLoading = false;
			} else {
				message.error(rs.message);
			}
		}).catch(error => {
			message.error(error);
		});
	}

	save = () => {
		this.content.form.validateForm().then(f => {
			if (f.isValid) {
				let params = {
					...this.content.form.getFormParams()
				};

				Api.save(params).then((res) => {
					let {
						api_status,
						sign
					} = res;

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

	setState = (date) => {
		this.date = date;
	}

	mobxDataReset = () => {
		this.content.form = new WeaForm();
	}
}