/* eslint-disable no-restricted-syntax */
/* eslint-disable guard-for-in */
import axios from "axios";

export default {
  namespaced: true,
  state: {
    items: [],
    weekCount: {}
  },
  mutations: {
    setItems(state, list) {
      state.items = list;
    },
    setItem(state, item) {
      state.week = item;
    },
    setWeekCount(state, item){
      state.weekCount = item
    },
    updateItem(state, item) {
      var index;
      var count = 0;
      state.items.forEach(el => {
        if (el.id === item.id) {
          index = count;
        }
        count++;
      });

      Object.assign(state.items[index], item);
    },
    updatePublished(state, item) {
      state.items.find(week => week.id === item.id).publish = true;
    },
    updateUnpublished(state, item) {
      state.items.find(week => week.id === item.id).publish = false;
    },
    removeListing(state, id) {
      var index = state.items.findIndex((item) => item.id == id)
      state.items.splice(index, 1)
    },
  },
  getters: {
    getNeedsVerify(state) {
      return state.items;
    }
    
  },
  actions: {
    async getItems({ commit }, id) {
      await axios
        .get(`/api/timeshareweek/getMyWeek/${id}`)
        .then(r => commit("setItems", r.data))
        .catch(console.error);
    },
    async getAllItems({ commit }) {
      const response = await axios.get("api/timeshareweek/");
      if (response.status === 200) {
        commit("setItems");
        return Promise.resolve();
      } else {
        return Promise.reject();
      }
    },
    async getWeekCount({commit}, id){
      await axios.get("api/timeshareweek/indivWeekCount/"+ id)
      .then((res) => {
        commit("setWeekCount", res.data)
        return Promise.resolve()
      })
      .catch((ex) => {
        return Promise.reject(ex.response)
      })
    },
    verifyWeek({ commit }, id) {
      try {
        axios.post(`/api/timeshareweek/verifyweek/${id}`).catch(console.error);
      } catch (err) {
        console.log(err);
      }
    },
    publishWeek({ commit }, item) {
      axios
        .post(`/api/timeshareweek/publishweek/${item.id}`)
        .then(result => commit("updatePublished", item))
        .catch(console.error);
    },
    unpublishWeek({ commit }, item) {
      axios
        .post(`/api/timeshareweek/UnpublishWeek/${item.id}`)
        .then(result => commit("updateUnpublished", item))
        .catch(console.error);
    },
    deleteListing({ commit }, id) {
      axios
      .delete(`/api/timeshareWeek/${id}`)
      .then(result => commit('removeListing', id))
      .catch(console.error);
    },
    async getWeek({ commit }, id) {
      await axios
        .get(`/api/timeshareweek/${id}`)
        .then(r => {
          //console.log(JSON.stringify(r));
          commit("setItem", r.data);
        })
        .catch(console.error);
    },
    async publishOnly({ commit }, week) {
      await axios
        .put(`/api/timeshareweek/publishOnly`, week)
        .then(r => {
          commit("setItem", week);
        })
        .catch(console.error);
    },
    async editSave({ commit }, week) {
      await axios
        .put(`/api/timeshareweek`, week)
        .then(r => {
          week.owner = week.owner.name;
          if(week.agent !== undefined){
            week.agent = week.agent.name;
          }          
          commit("setItem", week);
          return Promise.resolve()
        })
        .catch((ex) => {
          return Promise.reject(ex.response)
        });
    }
  }
};
