import axios from 'axios';

export default {
  namespaced: true,
  state: {
    carouselList: [],
    carousel: {
      id: 0,
      propertyID: 0,
      timeshareID: 0,
      header: '',
      image: '',
    },
  },
  mutations: {
    setCarouselItem(state, item) {
      state.carousel = item;
    },
    setCarouselList(state, items) {
      state.carouselList = items;
    },
    addToCarouselList(state, item) {
      state.carouselList.push(item);
    },
    removeCarousel(state, id) {
      state.carouselList.pop(state.carouselList.find(p => p.id === id));
    },
  },
  getters: {},
  actions: {
    getCarouselItem({ commit }, id) {
      axios
        .get(`/api/Carousel/${id}`)
        .then(result => commit('setCarouselItem', result.data))
        .catch(console.error);
    },
    getCarouselList({ commit }) {
      axios
        .get('/api/Carousel')
        .then(result => commit('setCarouselList', result.data))
        .catch(console.error);
    },
    saveCarouselItem({ commit }, item) {
      axios
        .post('/api/Carousel', item)
        .then(result => commit('addToCarouselList', result.data))
        .catch(console.error);
    },
    deleteCarousel({ commit }, id) {
      axios
        .delete(`/api/Carousel/${id}`)
        .then(commit('removeCarousel', id))
        .catch(console.error);
    },
  },
};
