import axios from 'axios';

export default {
  namespaced: true,
  state: {
    menuItem: {},
    menuItems: []
  },
  mutations: {
    setMenuItems(state, items) {
      state.menuItems = items;
    },
    setMenuItem(state, item) {
      state.menuItem = item;
    },
    addMenuItem(state, item) {
      state.menuItem = item;
      state.menuItems.push(item);
    },
    updateMenuItem(state, item) {
      state.menuItem = item;
      //state.menuItems.find(m => m.id === item.id) = item;
    },
    removeMenuItem(state, item) {
      state.menuItems.pop(item);
    }
  },
  getters: {},
  actions: {
    getMenuItem({
      commit
    }, id) {
      axios
        .get(`/api/MenuItem/GetMenuItem/${id}`)
        .then(result => commit("setMenuItem", result.data));
    },
    getMenuItems({
      commit
    }, id) {
      axios
        .get(`/api/MenuItem/${id}`)
        .then(result => commit("setMenuItems", result.data));
    },
    insertMenuItem({
      commit
    }, menuItem) {
      axios
        .post('/api/MenuItem', menuItem)
        .then(result => commit("addMenuItem", result.data));
    },
    updateMenuItem({
      commit
    }, menuItem) {
      axios
        .put('/api/MenuItem', menuItem)
        .then(result => commit("updateMenuItem", menuItem));
    },
    deleteMenuItem({
      commit
    }, menuItem) {
      axios
        .delete(`/api/MenuItem/${menuItem.id}`)
        .then(result => commit("removeMenuItem", menuItem));
    }
  }
}
