import { createStore } from 'vuex'

export default createStore({
    state: {
        tagsList: [],
        collapse: true,
        //stationList
        stationList: [],
        stationListAll: [],
        //实时报警
        warningList: [],
        //是否存在实时报警轮询
        warnInterval: null,
        //是否启用报警弹窗
        enableWarn: false,
        //报警弹窗持续时间
        warnTime: 5
    },
    mutations: {
        delTagsItem(state, data) {
            state
                .tagsList
                .splice(data.index, 1);
        },
        setTagsItem(state, data) {
            state
                .tagsList
                .push(data)
        },
        clearTags(state) {
            state.tagsList = []
        },
        closeTagsOther(state, data) {
            state.tagsList = data;
        },
        closeCurrentTag(state, data) {
            for (let i = 0, len = state.tagsList.length; i < len; i++) {
                const item = state.tagsList[i];
                if (item.path === data.$route.fullPath) {
                    if (i < len - 1) {
                        data
                            .$router
                            .push(state.tagsList[i + 1].path);
                    } else if (i > 0) {
                        data
                            .$router
                            .push(state.tagsList[i - 1].path);
                    } else {
                        data
                            .$router
                            .push("/");
                    }
                    state
                        .tagsList
                        .splice(i, 1);
                    break;
                }
            }
        },
        // 侧边栏折叠
        hadndleCollapse(state, data) {
            state.collapse = data;
        },
        // stationList
        setStationList(state, data) {
            state.stationList = data;
        },
        setStationListAll(state, data) {
            state.stationListAll = data;
        },
        // --------------------------------------------------------------------
        //实时报警
        setWarning(state, data) {
            state.warningList.unshift(data);
        },
        //是否存在实时报警轮询
        setWarnInterval(state, data) {
            state.warnInterval = data;
        },
        //是否启用报警弹窗
        setEnableWarn(state, data) {
            state.enableWarn = data;
        },
        setWarnTime(state, data) {
            state.warnTime = data;
        }
    },
    actions: {
        actionsWarning(context, newData) {
            context.commit("setWarning", newData);
        },
        //stationList
        actionsStationList(context, newData) {
            context.commit("setStationList", newData);
        },
        actionsStationListAll(context, newData) {
            context.commit("setStationListAll", newData);
        },
        //是否存在实时报警轮询
        actionsWarnInterval(context, newData) {
            context.commit("setWarnInterval", newData);
        }
    },
    modules: {},
    getters: {
        isStation(state) {
            return state.stationList.length == 1
        }
    }
})