import axios from 'axios'

export default {
  namespaced: true,
  state: {
    restaurantUsers: [],
    restaurantUser: {}
  },
  mutations: {
    setUsers(state, users) {
      state.restaurantUsers = users;
    },
    setUser(state, user) {
      state.restaurantUser = user;
    },
    addUser(state, user) {
      state.restaurantUsers.push(user);
    },
    updateUser(state, user) {
      state.restaurantUser = user;
      //state.restaurantUsers.find(res => res.id === user.id).cellphone = user.cellphone;
      //TODO: update Restaurant Users list.
    },
    removeUser(state, user) {
      state.restaurantUser = {};
      state.restaurantUsers.pop(user);
    }
  },
  getters: {},
  actions: {
    getUsers({
      commit
    }, restaurantId) {
      axios
        .get(`/api/RestaurantUser/GetRestaurantUsers/${restaurantId}`)
        .then(result => commit("setUsers", result.data));
    },
    getUser({
      commit
    }, userId) {
      axios
        .get(`/api/RestaurantUser/${userId}`)
        .then(result => commit("setUser", result.data));
    },
    insertUser({
      commit
    }, user) {
      axios
        .post('/api/RestaurantUser', user)
        .then(result => commit("addUser", result.data));
    },
    updateUser({
      commit
    }, user) {
      axios
        .put('/api/RestaurantUser', user)
        .then(result => commit("updateUser", user));
    },
    deleteUser({
      commit
    }, user) {
      axios
        .delete(`/api/RestaurantUser/${user.id}`)
        .then(result => commit("removeUser", result.data));
    }
  }
}
