import Vue from 'vue'
import Vuex from 'vuex'
import * as actions from './actions'
import * as getters from './getters'
import {getServerList, getHostList} from "../api/message_bus"

Vue.use(Vuex)

// 应用初始状态
const state = {
    count: 10,
    serverList: [],
    virtualHostList: [],
}

// 定义所需的 mutations
const mutations = {
    StoreServerList(state) {
        getServerList().then((response) => {
            let res = response.data;
            if (res.code !== '200') {
                return;
            }
            // 获取服务器列表数据
            state.serverList = res.data;
        }).catch(error => {
            this.$message.error(error.toString());
        });
    },
    StoreHostList(state, serverId) {
        getHostList(serverId).then((response) => {
            let res = response.data;
            if (res.code !== '200') {
                return;
            }
            state.virtualHostList = res.data;
        }).catch(error => {
            this.$message.error(error.toString());
        });
    },


    INCREMENT(state) {
        state.count++
    },
    DECREMENT(state) {
        state.count--
    }
}

// 创建 store 实例
export default new Vuex.Store({
    actions,
    getters,
    state,
    mutations
})