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

export class HrmLoginFailLog {
	@observable title = getLabel(516116,"人员登录失败日志");
	@observable btnMenu = [];
	@observable authorized = false;
	@observable loading = true;
	@observable isPanelShow = false;
	@observable tableStore = new TableStore();
	@observable key = '3';
	@observable searchPanel = {
		condition: [],
		form: new WeaForm(),
		value: '',
		loading: true,
		conditionCount: observable.ref(4),
		selectedValue: ['1', 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
		}
	}

	@action
	getRight = () => {
		this.loading = true;
		Api.getRight().then(rs => {
			const {
				status,
				hasRight
			} = rs;

			if (status == '1') {
				if (hasRight) {
					this.authorized = true;
					this.getButtonsMenu();
					this.getTableInfo();
				}
				this.loading = false;
			} else {
				message.error(rs.message);
			}
		}).catch(error => {
			message.error(error);
		});
	}

	@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.searchPanel.form.isFormInit) {
			params = {
				...this.searchPanel.form.getFormParams(),
				...this.getParamsFromWeaSelect(),
			}
		} else {
			params = {
				selectDate: this.key,
				loginId: this.searchPanel.value
			}
		}
		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;
		Api.getSearchCondition().then(rs => {
			const {
				api_status,
				conditions,
			} = rs;

			if (api_status) {
				if (conditions) {
					this.searchPanel.condition = conditions;
					this.searchPanel.form.initFormFields(conditions);
				}
				this.searchPanel.loading = false;
			} else {
				message.error(rs.message);
			}
		}).catch(error => {
			message.error(error);
		});
	}

	@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;
		bool && this.isEmpty(this.searchPanel.condition) && this.getSearchCondition();
	}

	@action
	updateFields = (val) => {
		this.searchPanel.form.updateFields({
			loginId: {
				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.searchPanel.form = new WeaForm();
		this.searchPanel.value = '';
		this.searchPanel.condition = [];
		this.isPanelShow = false;
		this.authorized = false;
		this.key = '3';
		this.setSelectedValue(['3', null, null]);
	}

}