import axios from 'axios';

export default {
  namespaced: true,
  state: {
    bidItem: {},
    bidItems: [],
  },
  mutations: {
    setBid(state, bid) {
      state.bidItem = bid;
    },
    setBids(state, bid) {
      state.bidItems = bid;
    },
    addToBids(state, bid) {
      state.bidItems.push(bid);
    },
    updateBidList(state, bid) {
      state.bidItems.find(item => item.id === bid.id).status = bid.status;
      console.log(JSON.stringify(bid));
    },
  },
  getters: {},
  actions: {
    getBid({ commit }, id) {
      axios
        .get(`/api/bid/${id}`)
        .then(result => commit('addToBids', result.data))
        .catch(console.error);
    },
    getBids({ commit }) {
      axios
        .get('/api/bid/GetBids/hjsdhj')
        .then(result => commit('setBids', result.data))
        .catch(console.error);
    },
    saveBid({ commit }, item) {
      axios
        .post('/api/bid', item)
        .then(result => commit('setBid', result.data))
        .catch(console.error);
    },
    updateBid({ commit }, item) {
      axios
        .put('/api/bid', item)
        .then(result => commit('setBid', item))
        .catch(console.error);
    },
    acceptBid({ commit }, id) {
      axios
        .put(`/api/bid/AcceptBid/${id}`)
        .then(result => commit('updateBidList', result.data))
        .catch(console.error);
    },
    declineBid({ commit }, item) {
      axios
        .put('/api/bid/DeclineBid', item)
        .then(result => commit('updateBidList', result.data))
        .catch(console.error);
    },
  },
};
