import axios from 'axios';
import {
  result
} from 'lodash';

export default {
  namespaced: true,
  state: {
    restaurantsCategories: [],
    restaurantCategory: {}
  },
  mutations: {
    setRestaurantsCategories(state, categories) {
      state.restaurantsCategories = categories;
    },
    addCategory(state, category) {
      state.restaurantCategory = category;
      state.restaurantsCategories.push(category);
    },
    updateCategories(state, category) {
      state.restaurantsCategories.find(item => item.id === category.id).description = category
        .description;
      state.restaurantsCategories.find(item => item.id === category.id).image = category
        .image;
    },
    removeCategories(state, id) {
      state.restaurantsCategories.pop(state.restaurantsCategories.find(item => item.id === id));
    }
  },
  getters: {},
  actions: {
    getCategories({
      commit
    }) {
      axios
        .get('/api/RestaurantCategory')
        .then(result => commit('setRestaurantsCategories', result.data));
    },
    addCategory({
      commit
    }, category) {
      axios
        .post('/api/RestaurantCategory', category)
        .then(result => commit('addCategory', result.data));
    },
    updateCategory({
      commit
    }, category) {
      axios
        .put('/api/RestaurantCategory', category)
        .then(result => commit('updateCategories', result.data));
    },
    deleteCategory({
      commit
    }, id) {
      axios
        .delete(`/api/RestaurantCategory/${id}`)
        .then(result => commit('removeCategories', id));
    },
    getSelectCategories({
      commit
    }) {
      axios
        .get('/api/RestaurantCategory/GetSelectCategories')
        .then(result => commit("setRestaurantsCategories", result.data));
    }
  }
}
