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

const state = {
    items: [],
}
const getters = {}

const actions = {
    getItems({commit}) {
        return new Promise((resolve) => {
            http.loadBatchs(r => {
                let batchs = r.data.items
                commit('setItems', batchs)
                resolve(batchs)
            })
        });
    },
    loadBatch({commit}, playload) {
        return new Promise((resolve) => {
            console.info('loadBatch', playload)
            http.loadBatch(playload, r => {
                resolve(r.data.batch)
            })
        });
    },

    saveOrUpdate({commit}, playload) {
        return new Promise((resolve) => {
            if (!playload.id) {
                http.saveBatch(playload, r => {
                    resolve(r.data.batch)
                })
            } else {
                http.updatBatch(playload, r => {
                    resolve(r.data.batch)
                })
            }
        });
    },

    remove({commit}, id) {
        return new Promise((resolve) => {
            http.removeBatch(id, r => {
                resolve(r.data.batch)
            })
        });
    }
}
const mutations = {
    setItems(state, items) {
        state.items = items;
    }
}

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