import * as Api from '../apis/limitedAnalysis';
import * as commonApi from '../apis/common';
import {
	toJS
} from 'mobx'
import {
	observable,
	action,
	computed,
} from 'mobx';
import {
	message,
} from 'antd';
import {
	i18n
} from '../public/i18n';

export class HrmLimitedAnalysis {
	@observable btnMenu = [];
	@observable authorized = false;
	@observable detachable = false;
	@observable loading = true;
	@observable key = '0';
	@observable radioGroup = {
		maxConfig: [],
		timeScopeConfig: [],
		timeScopeParams: {},
	}
	@observable echart = {
		lineOption: {},
		barOption: {},
		windowWidth: '',
	}
	@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
		}
	}

	get title() {
	    return i18n.module.limitedLoginAnalysis();
	}

	orgTreeProps = {
		needSearch: true,
		isLoadAllSub: true,
		topPrefix: "hrmSearch",
		inputLeftDom: `<b>${i18n.label.organization()}</b>`,
		// needDropMenu: true,
		treeNodeClick: this.treeNodeClick,
		params: {
			rightStr: "LogView:View"
		},
		isLoadSubDepartment: true,
	};

	@action treeNodeClick = (event, ids, nodeids, nodes) => {
		this.hrmDeptId = null;
		this.hrmSubComId = null;
		const { data } = event.node.props;
		const {type, id} = data;
		if(type == 1 || type == 0)
			this.hrmSubComId = id;
		else if(type == 2)
			this.hrmDeptId = id;
		this.getMaxOnlineNumber();
	};

	@action
	getRight = async () => {
		this.hrmDeptId = null;
		this.hrmSubComId = null;
		this.loading = true;
		const data = await commonApi.detachable();
		const {status: s, detachable} = data;
		this.detachable = s == '1' && detachable == '1';

		const rs = await Api.getRight();
		const {status, hasRight, message} = rs;
		if(status == '1'){
			if(hasRight){
				this.authorized = true;
				this.getMaxOnlineNumber();
			}
			this.loading = false;
		}else{
			message.error(message);
		}
	}

	@action
	getButtonsMenu = () => {
		Api.getButtonsMenu().then(rs => {
			const {
				btnMenu,
				status = '1',
			} = rs;

			if (status == '1') {
				this.btnMenu = btnMenu;
			} else {
				message.error(rs.message);
			}
		}).catch(error => {
			message.error(error);
		});
	}

	@action
	getMaxOnlineNumber = () => {
		const {
			timeScopeParams
		} = this.radioGroup;

		let params = {
			dateTab: parseInt(this.key) + 1,
		};

		if (!this.isEmptyObject(toJS(timeScopeParams))) {
			params = {
				...params,
				...toJS(timeScopeParams)
			}
		}
		this.hrmDeptId != null && Object.assign(params, {hrmDeptId: this.hrmDeptId});
		this.hrmSubComId != null && Object.assign(params, {hrmSubComId: this.hrmSubComId});
		Api.getMaxOnlineNumber(params).then(rs => {
			const {
				api_status,
				config = []
			} = rs;

			if (api_status) {
				this.radioGroup.maxConfig = config;
				this.getEchartsDatas();
			} else {
				message.error(rs.message);
			}
		}).catch(error => {
			message.error(error);
		});
	}

	@action
	getScope = () => {
		const {
			timeScopeParams
		} = this.radioGroup;

		let params = {
			dateTab: parseInt(this.key) + 1,
		};
		this.hrmDeptId != null && Object.assign(params, {hrmDeptId: this.hrmDeptId});
		this.hrmSubComId != null && Object.assign(params, {hrmSubComId: this.hrmSubComId});
		Api.getScope(params).then(rs => {
			const {
				api_status,
				config = []
			} = rs;

			if (api_status) {
				this.radioGroup.timeScopeConfig = config;
			} else {
				message.error(rs.message);
			}
		}).catch(error => {
			message.error(error);
		});
	}

	@action
	getEchartsDatas = () => {
		const {
			timeScopeParams
		} = this.radioGroup;

		let params = {
			dateTab: parseInt(this.key) + 1,
		};
		this.hrmDeptId != null && Object.assign(params, {hrmDeptId: this.hrmDeptId});
		this.hrmSubComId != null && Object.assign(params, {hrmSubComId: this.hrmSubComId});
		if (!this.isEmptyObject(toJS(timeScopeParams))) {
			params = {
				...params,
				...toJS(timeScopeParams)
			}
		}

		Api.getEchartsDatas(params).then(rs => {
			const {
				api_status,
				option1 = {},
				option2 = {}
			} = rs;

			if (api_status) {
				!this.isEmptyObject(option1) && this.operateOption(option1);
				!this.isEmptyObject(option2) && this.operateOption(option2);
				this.echart.lineOption = option1;
				this.echart.barOption = option2;
				this.getButtonsMenu();
				this.loading = false;
			} else {
				message.error(rs.message);
			}
		}).catch(error => {
			message.error(error);
		});
	}

	@action
	switchKey = (key) => {
		this.key = key;

		if (key == '5') {
			this.getScope();
		} else {
			this.radioGroup.timeScopeParams = {};
			this.radioGroup.timeScopeConfig = [];
			this.getMaxOnlineNumber();
		}

	}

	@action
	operateOption = (option) => {
		if (option.yAxis.min) {
			option.yAxis.min = function(value) {
				return value.min - 1;
			}
		}
		const grid = {
			grid: {
				left: '3%',
				right: '3%',
				containLabel: 'true',
			},
		}
		Object.assign(option, grid);
	}

	@action
	setTimeScopeParams = (params) => {
		this.radioGroup.timeScopeParams = params;
		this.getMaxOnlineNumber();
	}


	isEmptyObject = (obj) => {
		if (Object.keys(obj).length == 0) {
			return true;
		} else {
			return false;
		}
	}

	@action
	mobxDatasReset = () => {
		this.key = '0';
		this.radioGroup.timeScopeParams = {};
		this.radioGroup.timeScopeConfig = [];
	}
}