import * as Api from '../../apis/otherSetting';
import {
	toJS,
} from 'mobx';
import {
	observable,
	action,
	computed,
} from 'mobx';
import {
	WeaForm,
	WeaTableNew,
} from 'comsMobx';
import {
	Button,
	message,
} from 'antd';
import {
	i18n,
} from '../../hrm/i18n';

const {
	TableStore,
} = WeaTableNew;

export default class HrmOtherSetting {
	@observable main = {
		btnMenu: [],
		authorized: false,
		loading: true,
	}
	@observable formInfo = {
		conditions: [],
		form: new WeaForm(),
		loading: true,
		date: '',
		id: '',
	}

	@observable hasSynchroRight = false;

	@computed get menu() {
		let topMenu = this.main.btnMenu.filter(item => item.isTop == '1' || item.isBatch == '1');
		let rightMenu = this.main.btnMenu.filter(item => !item.isBatch);

		return {
			topMenu,
			rightMenu,
		};
	}

	@action
	getAuth = () => {
		Api.getAuth().then((cb) => {
			const {
				status,
				hasRight,
			} = cb;

			if (status == '1') {
				hasRight && this.grantAuth();
				this.main.loading = false;
			} else {
				message.error(cb.message);
			}
		}).catch((error) => {
			message.error(error);
		});
	}

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

			if (api_status) {
				this.main.btnMenu = btnMenu;
			} else {
				message.error(rs.message);
			}
		}).catch((error) => {
			message.error(error);
		});
	}

	@action
	getFormInfo = () => {
		Api.getFormInfo().then((rs) => {
			const {
				api_status,
				conditions,
				id,
				hasRight,
			} = rs;

			if (api_status) {
				this.formInfo.conditions = conditions;
				this.formInfo.form.initFormFields(conditions);
				this.formInfo.id = id;
				this.formInfo.loading = false;

				this.hasSynchroRight = hasRight;
			} else {
				message.error(rs.message);
			}
		}).catch((error) => {
			message.error(error);
		});
	}

	@action
	save = () => {
		const {
			form,
			id,
		} = this.formInfo;

		const params = {
			id,
			...form.getFormParams(),
		};

		Api.save(params).then((rs) => {
			const {
				api_status,
				sign,
			} = rs;

			if (params.defaultLinkMode !== undefined) {
				window.localStorage.setItem('ec_open_rewrite', params.defaultLinkMode);
			}

			if (params.dragColumns !== undefined) {
				window.localStorage.setItem('dragColumns_WeaTable', params.dragColumns);
			}

			if (params.isPageAutoWrap !== undefined) {
				window.localStorage.setItem('isPageAutoWrap', params.isPageAutoWrap);
			}

			if (api_status) {
				sign == '1' ? message.success(rs.message) : message.warning(rs.message);
			} else {
				message.error(rs.message);
			}
		}).catch((error) => {
			message.error(error);
		});
	}

	@action
	grantAuth = () => {
		this.main.authorized = true;
	}

	@action
	closeAuth = () => {
		this.main.authorized = false;
	}

	@action
	refreshFormInfo = () => {
		this.formInfo.form = new WeaForm();
		this.formInfo.conditions = [];
		this.formInfo.loading = true;
	}

	@action
	getButtons = () => {
		const btns = [(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@0xastc`} type="primary" onClick={() => this.save()}>{i18n.button.save()}</Button>)];

		return btns;
	}

	@action
	getRightMenu = () => {
		const menu = [{
			key: '0',
			icon: <i className="icon-coms-Preservation" />,
			content: i18n.button.save(),
		}];

		return menu;
	}

	@action
	onRightMenuClick = (key) => {
		key == '0' && this.save();
	}

	synchro = () => {
		const {
			form,
		} = this.formInfo,
{
			defaultLinkMode,
		} = form.getFormParams();

		Api.synchro({
			defaultLinkMode,
		}).then((rs) => {
			const {
				status,
			} = rs;
			status ? message.success('同步成功') : message.warning('同步失败');
		}).catch((error) => {
			message.error(error);
		});
	}
}
