
var timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
const store = Vuex.createStore()

store.registerModule('config', {
    namespaced: true,
    state() {
        return {
            base_url,
            altAvatar,
            appname: '',
            logo: '',
            favicon: '',
            rtime: '',
            onerrorAvatar: `this.onerror=null,this.src='${altAvatar}'`,
            redis: {
                state: 0,
                description: 'redis off',
            },
            is_https: location.protocol === 'https:',
            person: {},
            rdate: '',
            timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
        }
    },
    actions: {
        loadPerson({ commit }, params = {}) {
            axios({
                method: 'GET',
                url: store.state.config.base_url + '/panel/profile/me',
            }).then((response) => {
                if (typeof response.data != 'object') return;
                commit('person', response.data.data)
                commit('csrf', response.data.csrf);
                if (typeof params.success != 'undefined') params.success(response);
            }, (error) => {
                if (typeof params.error != 'undefined') params.error(error);
            });
        },
        loadRedisState({ commit }, params = {}) {
            axios({
                method: 'GET',
                url: store.state.config.base_url + '/panel/app/settings/redis/state',
            }).then((response) => {
                if (typeof response.data != 'object') return;
                commit('redis', { state: response.data.state ? 1 : 0, description: gaem({ data: response.data }) })
                if (typeof params.success != 'undefined') params.success(response);
            }, (error) => {
                if (typeof params.error != 'undefined') params.error(error);
                commit('redis', { state: 2, description: gaem(error) })
            });
        },
        loadVDTG({ commit }, params = {}) {
            axios({
                method: 'GET',
                url: store.state.config.base_url + '/vdtg',
            }).then((response) => {
                if (typeof response.data != 'object') return;
                commit('csrf', response.data);
                if (typeof params.success != 'undefined') params.success(response);
            }, (error) => {
                if (typeof params.error != 'undefined') params.error(error);
            });
        },
    },
    mutations: {
        timezone(state, n) {
            state.timezone = n;
        },
        person(state, n) {
            state.person = n;
        },
        redis(state, n) {
            state.redis = n;
        },
        appname(state, n) {
            state.appname = n;
        },
        logo(state, n) {
            state.logo = n;
        },
        favicon(state, n) {
            state.favicon = n;
        },
        rtime(state, n) {
            state.rtime = n;
        },
        rdate(state, n) {
            state.rdate = n;
        },
        csrf(state, n) {
            if (typeof n == 'string') {
                state.csrf.value = n;
            } else {
                state.csrf = n;
            }
        },
    }
});
const VAPP = Vue.createApp({
    mixins: myVueMixin,
    data() {
        return {
            user: {
                name: '',
            },
            sidebar: {
                hide: false,
                overlay: false,
            }
        }
    },
    mounted() {
        if (mm().format('YYYY') < 2022 && window.location.href.indexOf("clock-error") == -1) return window.location.href = this.$store.state.config.base_url + '/clock-error';
        else if (mm().format('YYYY') >= 2022 && window.location.href.indexOf("clock-error") != -1) return window.location.href = this.$store.state.config.base_url;
        this.$store.dispatch('config/loadPerson');
        this.$store.commit('config/csrf', xaedm);
        document.getElementById('cxaedm').remove();
        setTimeout(() => {
            this.$store.dispatch('config/loadRedisState');
            // setInterval(() => this.$store.dispatch('config/loadVDTG'), 1000 * 60 * 3);
        }, 1000 * 10);
        setInterval(() => this.renderTime(), 100);
        // setInterval(() => this.$store.dispatch('config/loadRedisState'), 1000 * 60 * 3);
        // if (window.location.href.indexOf('panel/auth/login') == -1) setInterval(() => this.$store.dispatch('config/loadPerson'), 1000 * 10);
        axios({
            method: 'get',
            url: this.$store.state.config.base_url + '/panel/utils/header',
        }).then((response) => {
            timezone = response.data.timezone;
            this.$store.commit('config/timezone', response.data.timezone);
            this.$store.commit('config/appname', response.data.appname);
            this.$store.commit('config/logo', response.data.logo);
            localStorage.setItem("candy-timezone", response.data.timezone);
        }, (error) => {
            let tz = localStorage.getItem("candy-timezone");
            if (!tz) return;
            timezone = tz;
            this.$store.commit('config/timezone', tz);
        });
    },
    methods: {
        renderTime() {
            this.$store.commit('config/rtime', mm().format('HH:mm:ss'));
            this.$store.commit('config/rdate', mm().format('dddd, LL'));
        },
        sidebarDOM(state) {
            const sidebarNavWrapper = document.querySelector(".sidebar-nav-wrapper");
            const mainWrapper = document.querySelector(".main-wrapper");
            const menuToggleButton = document.querySelector("#menu-toggle");
            const menuToggleButtonIcon = document.querySelector("#menu-toggle i");
            const overlay = document.querySelector(".overlay");

            if (document.body.clientWidth > 1200) {
                if (menuToggleButtonIcon.classList.contains("lni-chevron-left")) {
                    menuToggleButtonIcon.classList.remove("lni-chevron-left");
                    menuToggleButtonIcon.classList.add("lni-menu");
                } else {
                    menuToggleButtonIcon.classList.remove("lni-menu");
                    menuToggleButtonIcon.classList.add("lni-chevron-left");
                }
            } else {
                this.sidebar.overlay = state;
                if (menuToggleButtonIcon.classList.contains("lni-chevron-left")) {
                    menuToggleButtonIcon.classList.remove("lni-chevron-left");
                    menuToggleButtonIcon.classList.add("lni-menu");
                }
            }
            overlay.addEventListener("click", () => {
                this.sidebar.hide = false;
                this.sidebar.overlay = false;
            });
        }
    },
    computed: {
        sidebar_hide: {
            get() {
                return this.sidebar.hide;
            },
            set(v) {
                this.sidebar.hide = v;
            }
        },
    },
    watch: {
        // overlay
        sidebar_hide(e) {
            this.sidebarDOM(e);
        }
    },
});