
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,
            onerrorAvatar: `this.onerror=null,this.src='${altAvatar}'`,
            pengumuman: [],
            sidebar_menu: [],
            jadwal_pelajaran: {},
            person: {},
            appname: '',
            logo: '',
            is_https: location.protocol === 'https:',
            favicon: '',
            timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
        }
    },
    actions: {
        loadPerson({ commit }, params = {}) {
            axios({
                method: 'GET',
                url: store.state.config.base_url + '/profile/me',
            }).then((response) => {
                if (typeof response.data != 'object') return;
                commit('csrf', response.data.csrf);
                delete response.data.csrf;
                commit('person', response.data)
                if (typeof params.success != 'undefined') params.success(response);
            }, (error) => {
                if (typeof params.error != 'undefined') params.error(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);
            });
        },
        loadJadwalPelajaran({ commit }, params = {}) {
            params.day = (params.day || mm().day());
            let m = getMomentDay(params.day);
            axios({
                method: 'GET',
                url: store.state.config.base_url + "/kelas/jadwal/all?&startTime=" + m.hours(0).minutes(0).seconds(0).unix() + "&endTime=" + m.hours(23).minutes(59).seconds(59).unix(),
            }).then((response) => {
                if (typeof response.data != 'object') return;
                response.data = response.data.map(v => {
                    v.sdtm = mm({ unix: v.startDateTime });
                    v.edtm = mm({ unix: v.endDateTime });
                    return v;
                })
                commit('jadwal_pelajaran', { day: params.day, val: response.data });
                if (typeof params.success != 'undefined') params.success(response);
            }, (error) => {
                if (typeof params.error != 'undefined') params.error(error);
            });
        },
        loadPengumuman({ commit }, params = {}) {
            axios({
                method: 'GET',
                url: store.state.config.base_url + '/pengumuman/all',
            }).then((response) => {
                if (typeof response.data != 'object') return;
                let p = response.data.map(v => {
                    v.time = `${(new Date(v.created_at)).getDate()} ${bulanIndo((new Date(v.created_at)).getMonth())} ${(new Date(v.created_at)).getFullYear()}`;
                    return v;
                });
                commit('pengumuman', p)
                if (typeof params.success != 'undefined') params.success(response);
            }, (error) => {
                if (typeof params.error != 'undefined') params.error(error);
            });
        },
    },
    mutations: {
        timezone(state, n) {
            state.timezone = n;
        },
        base_url(state, n) {
            state.base_url = n;
        },
        sidebar_menu(state, n) {
            state.sidebar_menu = n;
        },
        pengumuman(state, n) {
            state.pengumuman = n;
        },
        jadwal_pelajaran(state, n) {
            state.jadwal_pelajaran[n.day] = n.val;
        },
        person(state, n) {
            state.person = n;
        },
        appname(state, n) {
            state.appname = n;
        },
        logo(state, n) {
            state.logo = n;
        },
        favicon(state, n) {
            state.favicon = n;
        },
        csrf(state, n) {
            if (typeof n == 'string') {
                state.csrf.value = n;
            } else {
                state.csrf = n;
            }
        },
    }
});
const VAPP = Vue.createApp({
    mixins: myVueMixin,
    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();
        // setInterval(() => this.$store.dispatch('config/loadVDTG'), 1000 * 60 * 3);
        axios({
            method: 'get',
            url: this.$store.state.config.base_url + '/panel/utils/header',
        }).then((response) => {
            if (!isObject(response.data)) return;
            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);
        });
    }
});
