import Vue from 'vue'
import Vuex from 'vuex'
import Store from 'store'
import lib from 'lib'
import api from '../depend/API/API.js'
import $ from 'jquery'

const reasonMap = [
    "广告贴",
    "不要灌纯水哦~*^_^*多多和会员交流",
    "内容违反版规喽",
    "帮你移动到正确版面，注意归类哦^_^",
    "你的贴子很不错。推荐一下！ O(∩_∩)O",
    "活动奖励发放 O(∩_∩)O",
    "欢迎来到多玩， *^_^*希望你在这玩得愉快~",
    "你的提问已回复，你也要多多帮助别人哦*^_^*",      
    "好东西！再接再厉哦！",
    "感谢你帮助玩友O(∩_∩)O",
    "感谢您配合我们的工作。O(∩_∩)O",

]

const topLevelList = [
    "无",
    "本版置顶",
    "大版置顶",
    "分类置顶",
    "全局置顶"
]



const Api = new api();
const protocol = window.location.protocol + '//'
let searchHistory = Store.get("searchHistory") || [];

Vue.use(Vuex);

export default new Vuex.Store({
    state: {
        reasonMap,
        primaryData : {},   //游戏页面首屏数据
        newGameData:[],     //游戏页面最近新测游戏数据
        kaData:[],          //游戏页面最新礼包数据
        syData:[],          //游戏页面手游数据
        searchHistory: searchHistory,
        topLevelList
    },
    mutations: {
        setPrimaryData (state, data) {
            state.primaryData = data;
        },
        setNewGameData (state, data) {
            state.newGameData = data
        },
        setKaData (state, data) {
            state.kaData = data
        },
        setSyData (state, data) {
            state.syData = data
        },
        setSearchHistory (state, data) {
            let pn = data.pn
            let flag = state.searchHistory.find( n => n.pn == pn )
            if(!flag) {
                state.searchHistory.push(data)
                Store.set("searchHistory", state.searchHistory);
            }
        },
        delSearchHistory (state, data) {
            state.searchHistory = []
            Store.remove('searchHistory')
        }

    },
    actions: {
        getPrimaryData (context) {
            Api.getPrimaryData().done(ret => {
                let list = ret.data
                let data = []
                list.forEach((item,index) => {
                    let obj = {}
                    obj.title = item.title
                    obj.link = item.link
                    obj.img = item.img.replace(/http:\/\//,protocol);
                    data.push(obj)
                })
                context.commit('setPrimaryData',data);
            })
        },
        getNewGameData (context) {
            Api.getNewGameData().done(ret => {
                let list = ret.data
                let data = []
                list.forEach((item,index) => {
                    let obj = {}
                    let $dom = $(item.dom)
                    obj.title = $dom.find('h3 a').text()
                    obj.link = $dom.find('h3 a').attr('href').replace(/com/,'cn')
                    obj.img = item.img.replace(/http:\/\//,protocol);
                    data.push(obj)
                })
                context.commit('setNewGameData',data)
            })
        },
        getKaData (context) {
            Api.getKaData().done(ret => {
                
                context.commit('setKaData',ret.data);
            })
        },

        getSyData (context) {
            Api.getSyData().done(ret => {
                let list = ret.data
                let data = []
                list.forEach((item,index) => {
                    let obj = {}
                    obj.game_id = item.game_id
                    obj.game_name = item.game_name
                    obj.game_logo_img = item.game_logo_img.replace(/http:\/\//,protocol);
                    obj.game_type_id = item.game_type_id
                    obj.h5_url = item.h5_url
                    obj.type_name = obj.type_name
                    data.push(obj)
                })
                context.commit('setSyData',data);
            })
        },


        
    },
    getters: {}
})
