import cart from '@/core/models/cart';

export default {
    namespaced: true,
    state: {
        count: 0,
        briefCart: {
            ids: [],
            list: {},
        },
    },
    getters: {},
    mutations: {
        count(state, data) {
            state.count = data;
        },
        briefCart(state, data) {
            state.briefCart = data;
        }
    },
    actions: {
        updateCount(context) {
            cart.count().then(count => {
                context.commit('count', count);
            });
        },
        briefCart(context) {
            cart.list().then((list) => {
                let data = {
                    ids: [],
                    list: {},
                };
                list.forEach((item) => {
                    item.good.forEach((k) => {
                        data.list[k.gid] = k.number;
                    });
                });
                data.ids = Object.keys(data.list);
                context.commit('briefCart', data);
            });
        },
        updateAll(context) {
            cart.list(true).then(() => {
                context.dispatch('briefCart');
                context.dispatch('updateCount');
            });
        }
    }
}
