import defaultSettings from "@/settings";

const {
    sideTheme,
    showSettings,
    topNav,
    tagsView,
    fixedHeader,
    sidebarLogo,
    dynamicTitle,
} = defaultSettings;
import {
    getCollectionRoutesList,
    addCollectionRoute,
    deleteCollectionRoute,
} from "@/api/system/collection";

const storageSetting = JSON.parse(localStorage.getItem("layout-setting")) || "";
const state = {
    title: "",
    theme: storageSetting.theme || "#409EFF",
    sideTheme: storageSetting.sideTheme || sideTheme,
    showSettings: showSettings,
    topNav: storageSetting.topNav === undefined ? topNav : storageSetting.topNav,
    tagsView: storageSetting.tagsView === undefined ? tagsView : storageSetting.tagsView,
    fixedHeader: storageSetting.fixedHeader === undefined ?
        fixedHeader :
        storageSetting.fixedHeader,
    sidebarLogo: storageSetting.sidebarLogo === undefined ?
        sidebarLogo :
        storageSetting.sidebarLogo,
    dynamicTitle: storageSetting.dynamicTitle === undefined ?
        dynamicTitle :
        storageSetting.dynamicTitle,
    zone: 2, // 1:mes 2:wms
    collectRoutes: [],
};
const mutations = {
    CHANGE_SETTING: (state, { key, value }) => {
        if (state.hasOwnProperty(key)) {
            state[key] = value;
        }
    },

    SET_COLLECT_ROUTES: (state, routes) => {
        state.collectRoutes = routes || [];
    },
};

const actions = {
    // 修改布局设置
    changeSetting({ commit }, data) {
        commit("CHANGE_SETTING", data);
    },
    // 设置网页标题
    setTitle({ commit }, title) {
        state.title = title;
    },
    // 收藏菜单
    addCollectRoute({ dispatch, state }, data) {
        return new Promise((resolve, reject) => {
            data.zone = state.zone;
            addCollectionRoute(data)
                .then((res) => {
                    dispatch("getCollectroutes");
                    resolve(res.data);
                })
                .catch((err) => {
                    reject(err);
                });
        });
    },
    // 取消收藏
    deleteCollectRoute({ dispatch }, id) {
        return new Promise((resolve, reject) => {
            deleteCollectionRoute(id)
                .then((res) => {
                    dispatch("getCollectroutes");
                    resolve(res.data);
                })
                .catch((err) => {
                    reject(err);
                });
        });
    },
    // 获取收藏列表
    getCollectroutes({ commit, state }) {
        return;
        const params = { zone: state.zone };
        return new Promise((resolve, reject) => {
            getCollectionRoutesList(params)
                .then((res) => {
                    commit("SET_COLLECT_ROUTES", res.data || []);
                    resolve(res.data);
                })
                .catch((err) => {
                    reject(err);
                });
        });
    },
};

export default {
    namespaced: true,
    state,
    mutations,
    actions,
};