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

export class AdminAreaDisplaySettingStore {
    get browserConf(){
        return [{ name: getLabel(522412, "省份浏览框"), type: 2222 }, { name: getLabel(522413, "城市浏览框"), type: 58 }, { name: getLabel(522414, "区县浏览框"), type: 263 }]
    } 
    @observable authorityStore = new AuthorityStore(api.getRegionSettingHasRight);
    @observable topStore = new TopStore(this);
    @observable formStore = new FormStore(this);

    @computed get topLoading() {
        return !this.formStore.form.isFormInit;
    }

    init = () => {
        this.authorityStore.fetchAuthority().then(() => {
            this.initTop();
            this.initForm();
        })
    }

    initTop = () => {
        const shareObj = { menuFun: "saveRegionDisplaySetting", menuName: getLabel(30986, '保存') };

        const config = {
            title: getLabel(522415, "行政区域显示设置"),
            tConf: [shareObj],
            rConf: [{ ...shareObj, menuIcon: "icon-coms-Preservation" }, { menuFun: "doLog", menuName: getLabel(83, '日志'), menuIcon: "icon-coms-Print-log" }],
        }

        this.topStore.init(config);
    }

    initForm = () => {
        const config = {
            api: api.getRegionDisplaySetting,
            isCustomRender: true
        }

        const fs = this.formStore;
        fs.init(config);
        fs.fetchForm();
    }

    doLog = () => {
        window.setLogViewProps({
            logSmallType: 20079
        })
    }

    saveRegionDisplaySetting = () => {
        const { form, validateForm } = this.formStore;

        validateForm(form).then(() => {
            if (!this.checkPathDelimiter()) {
                message.warning(i18n.label['530834']());
                return;
            }
            api.saveRegionDisplaySetting(form.getFormParams()).then(res => {
                if (res.status == "1") {
                    message.success(res.message || getLabel(83551, '保存成功！'));
                    this.resetBrowser();
                } else {
                    message.error(res.message);
                }
            })
        })
    }

    checkPathDelimiter = () => {
        const {form} = this.formStore;
        const {pathDelimiter} = form.getFormParams();
        return pathDelimiter.length <= 10;
    }

    setRef = (ref, type) => {
        this[`ref${type}`] = ref;
    }

    resetBrowser = () => {
        [2222, 58, 263].forEach(type => {
            this[`ref${type}`].set([]);
        })
    }

}