import * as mobx from 'mobx'

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

import {
	WeaForm
} from 'comsMobx';

import {
	message
} from 'antd'
const toJS = mobx.toJS;

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

import * as Api from '../apis/adminDecentralization';

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

	@computed get formKeys() {
		const switchKeysExceptSanyuan = [],
			browserKeysExceptDftsubcom = [],
			couple = {};

		const condition = this.content.condition;

		condition && condition.forEach( (c,outIndex) => {
			c.items.map( (item,innerIndex) => {
				const key = item.domkey[0];
				if (item.conditionType === "SWITCH") {
					(key!="sanyuanable") && switchKeysExceptSanyuan.push(key);
				} else {
					(key!="dftsubcomid") && browserKeysExceptDftsubcom.push(key);
				}
			})
		});

		const allBrowserKeys = ["dftsubcomid", ...browserKeysExceptDftsubcom];
		allBrowserKeys.forEach((key,index) => {
			couple[key] = switchKeysExceptSanyuan[index]
		})


		return {
			switchKeysExceptSanyuan,
			browserKeysExceptDftsubcom,
			couple
		};
	}

	@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(rs.message);
			}
		}).catch(error => {
			message.error(error);
		});
	}

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

			if (api_status) {
				if (condition) {
					this.content.form.initFormFields(condition);
					extendObservable(this.content, {
						condition: condition,
						data: condition[0].items[1].browserConditionParam.replaceDatas,
					})
					this.btnShow = condition[0].items[0].value == '0' ? false : true;
				}
				this.content.cLoading = false;
			} else {
				message.error('Form interface exception !');
			}
		}).catch(error => {
			message.error('Form interface exception !');
		});
	}

	save = () => {
		const {detachable,sanyuanable} =this.content.form.getFormParams();
		if (detachable == "1") {
			this.addRules("detachable");
			this.content.form.updateFields({ sanyuanable: { value: sanyuanable } })
		}
		this.content.form.validateForm().then(f => {
			if (f.isValid) {
				const {detachable} = this.content.form.getFormParams();
				let params = {
					...this.content.form.getFormParams()
				};
				if (detachable === "0") {
					Object.assign(params,{
						sanyuanable:"0"
					});
				}

				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('The operation of saving has failed !');
					}
				}).catch(error => {
					message.error('The operation of saving has failed !');
				});
			} else {
				f.showErrors();
				this.setDate({
					date: new Date()
				});
			}
		});
	}

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

	setBrowserData = (obj) => {
		extendObservable(this.content, {
			data: obj
		});
		this.content.condition[0].items[1].browserConditionParam.replaceDatas = obj;
	}

	changeBrowserReplaceDatas = (obj) => {
		let c = this.content.condition;
		c.map(cd => {
			cd.items.map(item => {
				if (item.browserConditionParam) {
					item.browserConditionParam.replaceDatas = [obj];
				}
			});
		});
	}

	handleClickOfCheckbox = (key, val) => {
		let f = this.content.form;
		if (key == 'detachable') {
			this.btnShow = val == '0' ? false : true;
			const keys = this.formKeys.switchKeysExceptSanyuan;
			// const keys = ['detachable', 'hrmdetachable', 'wfdetachable', 'docdetachable','odocdetachable',
			// 	'portaldetachable', 'cptdetachable', 'mtidetachable', 'wcdetachable', 'fmdetachable', 'mmdetachable', 'carsdetachable', 'votingdetachable', 'blogdetachable', 'executiondetachable'
			// ];

			keys.map(key => this.changeCheckboxValue(key, val));

			val == '0' && keys.map(key => this.deleteRules(key));
			val == '1' && keys.map(key => this.addRules(key));
		} else {
			this.clickCheckBoxOfSecondGroup(key, val);
		}

	}

	clickCheckBoxOfSecondGroup = (key, val) => {
		this.changeCheckboxValue(key, val);
		if (this.isSecondGroupCheckboxAllClosed()) {
			this.btnShow = false;
			this.changeCheckboxValue('detachable', '0');
		} else {
			this.btnShow = true;
			this.changeCheckboxValue('detachable', '1');
		}
	}

	isSecondGroupCheckboxAllClosed = () => {
		let c = toJS(this.content.condition);
		let b = true;

		c[1].items.map((item, index) => {
			if (item.value == '1') {
				b = false;
			}
		});

		return b;
	}

	changeCheckboxValue = (key, val) => {
		let c = this.content.condition;

		c.map(cd => {
			cd.items.map(item => {
				if (item.domkey[0] == key) {
					item.value = val;
				}
			});
		})
	}

	changeReplaceDatas = (key, obj) => {
		let c = this.content.condition;
		this.content.form = new WeaForm();
		c.map(cd => {
			cd.items.map(item => {
				if (item.domkey[0] == key) {
					item.browserConditionParam.replaceDatas = obj;
				}
			});
		})
		this.content.form.initFormFields(c);
	}

	deleteRules = (key) => {
		let c = this.content.condition;
		this.content.form = new WeaForm();
		c.map(cd => {
			cd.items.map((item, index) => {
				if (item.domkey[0] == key) {
					item.value = '0';
					delete cd.items[index + 1].rules;
				}
			});
		})
		this.content.form.initFormFields(c);
	}

	addRules = (key) => {
		let c = this.content.condition;
		this.content.form = new WeaForm();
		c.map(cd => {
			cd.items.map((item, index) => {
				if (item.domkey[0] == key) {
					item.value = '1';
					cd.items[index + 1].rules = 'required|string';
				}
			});
		})
		this.content.form.initFormFields(c);
	}

	setPageStatus = (bool) => {
		this.loading = bool;
	}

	setBtnStatus = (bool) => {
		this.btnShow = bool;
	}

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

}