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

export class HrmLoginLog {
	
	@observable btnMenu = [];
	@observable authorized = false;
	@observable detachable = false;
	@observable loading = true;
	@observable isPanelShow = false;
	@observable tableStore = new TableStore();
	@observable key = '3';
	@observable conditionForm = new WeaForm();
	@observable searchPanel = {
		condition: [],
		form: new WeaForm(),
		value: '',
		loading: true,
		conditionCount: observable.ref(5),
		selectedValue: ['3', null, null]
	}

	@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 getLabel(516115, '人员登录日志');
	} 

	orgTreeProps = {
		needSearch: true,
		isLoadAllSub: false,
		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.getTableInfo();
	};

	@action
	getRight = async () => {
		this.hrmDeptId = null;
		this.hrmSubComId = null;
		this.loading = true;
		const data = await commonApi.getDetachableAll();
		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.getButtonsMenu();
				this.getSearchCondition().then(() => {
					this.getTableInfo();
				})
			}
			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
	getTableInfo = () => {
		this.tableStore = new TableStore();
		let params;
		if (this.conditionForm.isFormInit) {
			params = {
				name: this.searchPanel.value,
				...this.conditionForm.getFormParams(),
				...this.getParamsFromWeaSelect(),
			}
		} else {
			params = {
				selectDate: this.key,
				name: this.searchPanel.value
			}
		}
		this.hrmDeptId != null && Object.assign(params, {hrmDeptId: this.hrmDeptId});
		this.hrmSubComId != null && Object.assign(params, {hrmSubComId: this.hrmSubComId});
		Api.getTableInfo(params).then(rs => {
			const {
				api_status,
				sessionkey
			} = rs;

			if (api_status) {
				sessionkey && this.tableStore.getDatas(sessionkey, 1);
			} else {
				message.error(rs.message);
			}
		}).catch(error => {
			message.error(error);
		});
	}

	@action
	getSearchCondition = () => {
		this.searchPanel.loading = true;
		return new Promise( (resolve,reject) => {
			Api.getSearchCondition().then(rs => {
				const {
					api_status,
					conditions,
				} = rs;

				if (api_status) {
					if (conditions) {
						this.searchPanel.condition = conditions;
						this.conditionForm.initFormFields(conditions);
					}
					this.searchPanel.loading = false;

					resolve();
				} else {
					message.error(rs.message);

					reject();
				}
			}).catch(error => {
				message.error(error);
				reject();
			});
		})
	}

	@action
	getParamsFromWeaSelect = () => {
		const {
			selectedValue
		} = this.searchPanel;

		const obj = {};
		selectedValue.map((val, index) => {
			if (index == 0) {
				obj.selectDate = val
			} else if (index == 1 && val !== null) {
				obj.startDate = val
			} else if (index == 2 && val !== null) {
				obj.endDate = val
			}
		})
		return obj;
	}


	@action
	setPanelStatus = (bool) => {
		this.isPanelShow = bool;
	}

	@action
	updateFields = (val) => {
		this.conditionForm.updateFields({
			name: {
				value: val
			}
		});
	}

	@action
	setBaseValueOfSearch = (val) => {
		this.searchPanel.value = val;
	}

	isEmpty = (c) => {
		if (c.length == 0) return true;
		return false;
	}

	@action
	switchKey = (key) => {
		this.key = key;
		this.isPanelShow && this.setPanelStatus(false);
		this.setSelectedValue([key, null, null]);
		this.getTableInfo();
	}

	@action
	setSelectedValue = (arr) => {
		this.searchPanel.selectedValue = arr;
		if (parseInt(arr[0]) > 5) {
			this.key = '0';
		} else {
			this.key = arr[0];
		}
	}

	@action
	mobxDatasReset = () => {
		this.conditionForm = new WeaForm();
		this.searchPanel.value = '';
		this.searchPanel.condition = [];
		this.isPanelShow = false;
		this.authorized = false;
		this.key = '3';
		this.setSelectedValue(['3', null, null]);
	}

}