import http from '../../../api/fastposter'

const state = {
    items: [],
    activeId: 0
}
const getters = {
    isActived() {
        return state.activeId !== 0
    }
}

const actions = {
    getItems({commit}) {
        http.loadAsideItems(items => {
            commit('set_items', items)
        })
    },
    setActiveItem({commit}, playload) {
        commit('set_active_id', playload)
    },
    openPoster({commit}){
        // 海报的ID为5
        commit('set_active_id', {id: 5, activated: true})
    }
}
const mutations = {
    set_items(state, items) {
        state.items = items
        state.items.filter(a => a.activated).forEach(a => state.activeId = a.id)

    },
    set_active_id(state, {id, activated, extend}) {
        // if(!extend) return
        if (state.activeId === id && !activated) {
            state.activeId = 0
            return
        }
        state.activeId = id
        state.items.filter(a => a.id !== id).forEach(a => a.activated = false)
        state.items.filter(a => a.id === id).forEach(a => a.activated = true)
    }
}

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