import * as Api from '../apis/onlineAnalysis';
import * as commonApi from '../apis/common';
import {
	toJS
} from 'mobx'
import {
	observable,
	action,
	computed,
} from 'mobx';
import {
	message,
} from 'antd';
import {
	WeaSelect,
	WeaLocaleProvider,
} from 'ecCom';
import {
	i18n
} from '../public/i18n';
const getLabel = WeaLocaleProvider.getLabel;

export class HrmOnlineAnalysis {
	title = i18n.module.onlineAnalysis;
	@observable btnMenu = [];
	@observable authorized = false;
	@observable detachable = false;
	@observable loading = true;
	@observable key = '0';
	@observable radioGroup = {
		maxConfig: [],
		timeScopeConfig: [],
		timeScopeParams: {},
		hasSelect: false,
		options: [],
		serverIpIndex: 'ALL',
		serverIp: getLabel('332', "全部"),
	}
	@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
		}
	}

	@observable loading1 = false;

	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();
		this.getScope();
	};

	@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.getScope();
			}
			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,
			serverIpIndex,
			serverIp
		} = this.radioGroup;

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

		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});
		this.loading1 = true;
		Api.getMaxOnlineNumber(params).then(rs => {
			const {
				api_status,
				config = []
			} = rs;

			if (api_status) {
				this.radioGroup.maxConfig = config;
				this.getEchartsDatas();
			} else {
				message.error(rs.message);
			}
			this.loading1 = false;
		}).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;
				['hasSelect', 'options'].map(v => {
					this.radioGroup[v] = rs[v];
				});
			} else {
				message.error(rs.message);
			}
		}).catch(error => {
			message.error(error);
		});
	}

	@computed get clusterConfig() {
		const {
			options
		} = this.radioGroup;

		const config = [{
			label: getLabel('15038', "服务器"),
			com: <WeaSelect ecId={`${this && this.props && this.props.ecId || ''}_WeaSelect@y2lm6v`} options={toJS(options)} value={this.radioGroup.serverIpIndex} style={{width:100}} onChange ={this.handleSelectChange} />,
			labelcol: 6,
			fieldcol: 18,
		}];

		return config
	}

	handleSelectChange = (key, name) => {
		['serverIpIndex', 'serverIp'].map((v, i) => {
			this.radioGroup[v] = (i === 0) ? key : name
		});

		['getMaxOnlineNumber'].map(f => {
			this[f]();
		});

	}

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

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

		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.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;
		const {selectDate,startDate,endDate} = params;
		if ( selectDate == "6" && (!startDate || !endDate) ) {
			return;
		}
		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 = [];
		//重置服务器参数
		['serverIpIndex', 'serverIp'].map((v, i) => {
			this.radioGroup[v] = (i === 0) ? 'ALL' : getLabel('332', "全部");
		});
		this.radioGroup.options = [];
		this.radioGroup.hasSelect = false;
	}
}