import { observable, action, computed } from 'mobx';
import {AuthorityStore,TopStore,FormStore} from '../../public/valhalla/stores/index';
import { Modal,message } from 'antd';
import { i18n } from '../../public/i18n';
import * as api from '../../apis/sensitiveWord.js';
const confirm = Modal.confirm;

export default class SettingStore {
	searchGroupProps = {col:1}
	get comProps(){
		return {
			onChange:this.handleFormChange
		}
	}
    
    @observable authorityStore = new AuthorityStore(api.hasRight);
    @observable topStore = new TopStore(this);
    @observable formStore = new FormStore(this);
	
    init = () => {
        this.authorityStore.fetchAuthority({type:'sensitiveSetting'}).then(()=>{
        	this.initTop();
        	this.initForm();
        })
    }

    initTop = () => {
    	const conf = {
    		btnMenu :[{
                "isTop": "1",
                "menuFun": "save",
                "menuIcon": "icon-coms-Preservation",
                "menuName": i18n.button.save(),
                "type": "BTN_Save"
            },{
            "isTop": "0",
            "menuFun": "log",
            "menuIcon": "icon-coms-Print-log",
            "menuName": i18n.button.log(),
            "type": "BTN_log"
        }],
            title:i18n.label['531342'](),
            additionalTopProps:{
                icon: <i className='icon-coms-currency'/>
            } 
    	},ts = this.topStore;
    	ts.init(conf)
    	ts.fetchRightMenu()
    }

    initForm = () => {
    	const conf = {
    		api:api.getSettingInfo,
    		isCustomRender:false
    	},fs = this.formStore;

    	fs.init(conf).fetchForm();
    }

    save = () => {
    	const {validateForm,form} = this.formStore;
    	validateForm().then(() => {
    		const params=form.getFormParams();
    		api.saveSettingInfo(params).then(res => {
				const {status} = res;
                message[(status == '1') ? 'success' : 'error'](res.message)
    		})
    	})
    }

    handleFormChange = (data)=>{
		if (data.isencrypt) {
			const {value} = data.isencrypt;
			if (value == '1') {
				confirm({
					title:i18n.confirm.defaultTitle(),
					content:i18n.label['531396'](),
					onOk:this.disableIsencrypt,
					onCancel:() => {
						const {form} = this.formStore;
						form.updateFields({
							isencrypt:'0'
						})
					}
				})
			}
		}
    }

    disableIsencrypt = () => {
		const {form} = this.formStore;
		form.setEmpty = () =>{};
		form.setField('isencrypt',{
			disabled:true
		})

    }

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