import {
	observable,
	action,
} from 'mobx';
import {
	WeaLocaleProvider,
} from 'ecCom';
import {
	message,
} from 'antd';
import {
	AuthorityStore,
	TopStore,
	FormStore,
} from '../public/valhalla/stores/index.js';
import * as api from '../apis/applicationSettings.js';
const getLabel = WeaLocaleProvider.getLabel;

class ApplicationSettingsStore {
	@observable authorityStore = new AuthorityStore(api.getHasRight);
	@observable topStore = new TopStore(api.getRightMenu, this);
	@observable formStore = new FormStore(api.getSettingsForm, this);

	get topLoading() {
		return this.formStore.loading;
	}

	get rightMenuStates() {
		const {
			loading,
		} = this.formStore;

		return [loading];
	}

	get topConfig() {
		return {
			title: getLabel(507184, "应用设置")
		}
	}

	get formConfig() {
		return {
			isCustomRender: true,
		}
	}

	preMount = () => {
		this.topStore.setTopConfig(this.topConfig);
	}

	init = () => {
		this.authorityStore.fetchAuthority().then(() => {
			const {
				setFormConfig,
				fetchForm
			} = this.formStore;

			this.topStore.fetchRightMenu();

			setFormConfig(this.formConfig);
			fetchForm();
		})
	}

	save = () => {
		const params = this.formStore.form.getFormParams();

		api.saveSettings({datas:JSON.stringify(params) }).then(datas => {
			const {
				sign
			} = datas;

			if (sign === '1') {
				message.success(datas.message);
			} else {
				message.warning(datas.message);
			}
		})
	}


}

export const applicationSettingsStore = new ApplicationSettingsStore();