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

class SalaryApplySet {
	@observable authorityStore = new AuthorityStore(api);
	@observable topStore = new TopStore(api, this);
	@observable formStore = new FormStore(this);

	get topTitle() {
		return getLabel(508046, '工资应用设置');
	}

	get searchGroupProps(){
		return {
			center:true,
			col:1,
		}
	}

	@computed get pageloading() {
		return this.formStore.loading;
	}

	getRightMenuState = () => {
		return [this.formStore.loading];
	}

	get formConfig() {
		return {
			api: api.fetchForm,
			isCustomRender: true,
			linkageConfigs: [{
				passiveKey: "specialauth",
				activeKey: "isenable",
				showValue: "1"
			}, {
				passiveKey: "doubleauth",
				activeKey: "isenable",
				showValue: "1"
			}, {
				passiveKey: "authtype",
				activeKey: "isenable",
				showValue: "1"
			}, {
				passiveKey: "verifier",
				activeKey: ["doubleauth", "isenable"],
				showValue: "1"
			}]
		}
	}

	// handleFormChange = data => {
	// 	if(has(data, 'isenable') && data.isenable.value == '0'){
	// 		this.formStore.form.updateFields({
	// 			doubleauth: {
	// 				value: '0'
	// 			}
	// 		})
	// 	}
	// }

	init = () => {
		this.authorityStore.fetchAuthority().then(hasRight => {
			if (hasRight) {
				this.topStore.fetchRightMenu();
				this.formStore.init(this.formConfig);
				this.formStore.fetchForm();
			}
		})
	}

	showLog = () => {
		window.setLogViewProps({
			logType: '38',
			logSmallType: '17'
		});
	}

	save = async () => {
		const params = this.formStore.form.getFormParams();
		
		window.e9HideFormFieldKeys = [];
        if(params.isenable != '1'){
            window.e9HideFormFieldKeys.push(...['specialauth', 'verifier', 'authtype']); 
        }else if(params.isenable == '1' && params.specialauth != '1'){
            window.e9HideFormFieldKeys.push(...['verifier']);
		}

		const f = await this.formStore.form.validateForm();
        if(f.isValid){
            const data = await api.save(params);
            if(data.status == '1'){
                message.success(getLabel(83551, '保存成功！'));
            }else{
                message.error(data.message);
            }
        }else{
            f.showErrors();
            const fields = this.formStore.form.fieldMap;
            [...Object.keys(fields)].map(k => {
                const e = this.formStore.form.getError(fields[k]);
                if (e != null && e != "") {
                    // console && console.debug(e);
                }
            });
        }
	}

}
export const salaryApplySet = new SalaryApplySet();