import Vue from 'vue'
import 'es6-promise'
import Vuex from 'vuex'
import hb from '@/store/modules/hb'
import api from "@/api/index.js"

Vue.use(Vuex)
export default new Vuex.Store({
    state: {
        CONFIG: {}, // 配置
        userInfo: {}, // 用户信息
        finance: 0, // 鸿币余额
        query: {}, // 路由携带参数 pay: game 游戏充值  hb 鸿币充值
        payArg: {}, // 支付传递的参数
        /**
         * gamepay页面
         * 显示哪个菜单内容
         * 游戏内充值: "" 鸿币充值优惠明细: hbDetailGamePay
         * 鸿币充值: hbGamePay  鸿币支付确认: hbConfirm
         * 
         */
        showGameInner: "",
        /**
         * android平台
         * 忘川伏魔录: wcfml
         */
        platform: "",
        showServiceInline: false, // 是否显示联系在线客服
        couponList: [], // 用户 优惠券列表
        gameRoleInfo: null, // 游戏内角色信息 
        // 所有角色列表
        // gameRoleList: [],
        gameRoleList: [{
            server_id: 880416,
            server_name: "惊鸿专服",
            role_id: 416,
            role_name: "麻辣火锅生煎包",
            name: '惊鸿专服 麻辣火锅生煎包'
        }, {
            server_id: 880416,
            server_name: "惊鸿专服",
            role_id: 4160,
            role_name: "小树小树向前冲",
            name: '惊鸿专服 小树小树向前冲'
        }, {
            server_id: 880416,
            server_name: "惊鸿专服",
            role_id: 4161,
            role_name: "小树最棒啦",
            name: '惊鸿专服 小树最棒啦'
        }],
    },
    modules: {
        hb
    },
    getters: {
        // 获取当前支付平台的中文名
        getPayPlatform: function(state) {
            const { query } = state
            let pay_platform;
            switch (query.pay_platform) {
                case "HB":
                    pay_platform = "鸿币"
                    break;
                case "WECHATPAY":
                    pay_platform = "微信"
                    break;
                case "ALIPAY":
                    pay_platform = "支付宝"
                    break;
            }
            return pay_platform;
        }
    },
    // 异步操作
    actions: {
        // 页面配置
        setConfigAction(context, payload) {
            context.commit('setConfigMutation', payload)
        },
        // 用户信息
        userInfoAction(context, payload) {
            context.commit("userInfoMutation", payload)
        },
        // 鸿币余额
        financeAction(context, payload) {
            context.commit("financeMutation", payload)
        },
        // 路由携带参数
        queryAction(context, payload) {
            context.commit("queryMutation", payload)
        },
        // 支付传递过来的参数
        payArgAction(context, payload) {
            context.commit("payArgMutation", payload)
        },
        // appid
        appidAction(context, payload) {
            context.commit("appidMutation", payload)
        },
        // 显示的菜单内容
        showGameInnerAction(context, payload) {
            context.commit("showGameInnerMutation", payload)
        },
        // 当前android平台
        platformAction(context, payload) {
            context.commit("platformMutation", payload)
        },
        // 是否显示在线客服
        showServiceInlineAction(context, payload) {
            context.commit("showServiceInlineMutation", payload)
        },
        // 用户红包列表
        couponListAction(context, payload) {
            const { server_id, role_id, appid } = payload;
            api.hbCouponList({
                    app_id: appid,
                    server_id,
                    role_id,
                })
                .then((res) => {
                    payload.checkCode(res);
                    const { code, data, msg } = res.data;
                    // 错误
                    if (code) {
                        return;
                    }
                    context.commit("couponListMutation", data)
                });
        },
        // 游戏内角色信息
        gameRoleInfoAction(context, payload) {
            context.commit("gameRoleInfoMutation", payload)
        },
        // 游戏内所有角色列表
        gameRoleListAction(context, payload) {
            context.commit("gameRoleListMutation", payload)
        },
    },
    mutations: {
        // 页面配置
        setConfigMutation(state, payload) {
            state.CONFIG = payload
        },
        // 用户信息
        userInfoMutation(state, payload) {
            state.userInfo = payload
        },
        // 鸿币余额
        financeMutation(state, payload) {
            state.finance = payload
        },
        // 路由携带参数
        queryMutation(state, payload) {
            state.query = payload
        },
        // 支付传递过来的参数
        payArgMutation(state, payload) {
            state.payArg = payload
        },
        // appid
        appidMutation(state, payload) {
            state.appid = payload
        },
        // 当前android平台
        platformMutation(state, payload) {
            state.platform = payload
        },
        // 是否显示在线客服
        showServiceInlineMutation(state, payload) {
            state.showServiceInline = payload
        },
        // 用户折扣列表
        couponListMutation(state, payload) {
            state.couponList = payload
        },
        // 游戏内角色信息
        gameRoleInfoMutation(state, payload) {
            state.gameRoleInfo = payload
        },
        // 游戏内角色所有角色列表
        gameRoleListMutation(state, payload) {
            state.gameRoleList = payload
        },
    }
})