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/transfer';
const toJS = mobx.toJS;

export class HrmTransferSet {
	@observable weaTopTitle = i18n.module.personTransfer();
	@observable btnMenu = [];
	@observable authorized = false;
	@observable loading = true;
	@observable saveLoading = false;
	@observable date = '';
	@observable selectedKey = '0';
	@observable tabOne = {
		loading: 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(rs => {
			const {
				api_status,
				btnMenu = [],
				hasRight
			} = rs;

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

	getForm = () => {
		this.tabOne.loading = true;
		this.tabOne.form = new WeaForm();
		Api.getForm().then(rs => {
			let {
				api_status,
				condition,
			} = rs;

			if (api_status) {
				condition[0].items.forEach(item => {
					if (item) {
						let key = item.domkey[0];
						if (item.browserConditionParam) {
							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
									}
								}
							}
						}
						if (key == 'changereason') {
							item.otherParams = {
								maxRows: 4
							}
						}
						if (key == 'newjoblevel') {
							item.conditionType = 'INPUTNUMBER';
						}
					}
				});
				this.tabOne.form.initFormFields(condition);
				extendObservable(this.tabOne, {
					condition: condition,
				});
				this.tabOne.loading = false;
			} else {
				message.error(rs.message);
			}
		}).catch(error => {
			message.error(error);
		});
	}


	save = () => {
		this.tabOne.form.validateForm().then(f => {
			if (f.isValid) {
				this.saveLoading = true;
				let params = this.tabOne.form.getFormParams()
				Api.save(params).then(rs => {
					const {
						api_status,
						sign
					} = rs;

					this.saveLoading = false;

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

	setSelectedKey = (key) => {
		this.selectedKey = key;
		key == '0' && this.getForm();
	}

	mobxDataReset = () => {
		this.selectedKey = '0';
	}
}