const state = {
    lastFetch: null,
    pedido_id: null,
    nombre: null,
    productos: null,
    aprobado: null,
    total: null,
    total_transporte: null,
    cantidad_transporte: null,
    total_sin_devoluciones: null,
    devoluciones_total: null,
    devoluciones_notas: null,
};

const mutations = {
    setState(state, pedido) {
        state.lastFetch = new Date();
        state.pedido_id = pedido.id;
        state.nombre = pedido.nombre;
        state.productos = pedido.productos;
        state.aprobado = pedido.aprobado;
        state.total = pedido.total;
        state.total_transporte = pedido.total_transporte;
        state.cantidad_transporte = pedido.cantidad_transporte;
        state.total_sin_devoluciones = pedido.total_sin_devoluciones;
        state.devoluciones_total = pedido.devoluciones_total;
        state.devoluciones_notas = pedido.devoluciones_notas;
    },
};

const actions = {
    async crearPedido({ commit }, { nombre, grupo_de_compra_id }) {
        const response = await axios.post("/api/subpedidos", {
            nombre: nombre,
            grupo_de_compra_id: grupo_de_compra_id
        });
        const pedido = response.data;
        await axios.post("/subpedidos/sesion", { id: pedido.id, });
        commit('setState', pedido);
    },
    async getPedido({ commit }) {
        const sesion = await axios.get("/subpedidos/sesion");
        const response = await axios.get(`/api/subpedidos/${sesion.data}`);
        commit('setState', response.data.data);
    },
    async elegirPedido({ commit }, { pedido }) {
        await axios.post("/subpedidos/sesion", { id: pedido.id, });
        window.location.href = 'productos';
    },
    async modificarChismosa({ commit }, producto_id, cantidad, notas) {},
    async modificarDevoluciones({ commit }, monto, notas) {}
};

const getters = {
    pedidoDefinido() {
        return state.lastFetch !== null;
    },
}

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