import axios from 'axios';

export default {
  namespaced: true,
  state: {
    userDefinedGroups: [],
    userDefinedGroup: null,
    userFields: [],
    userField: null,
  },
  mutations: {
    setUserDefinedGroups(state, groups) {
      state.userDefinedGroups = groups;
    },
    setUserDefinedGroup(state, group) {
      state.userDefinedGroup = group;
    },
    setUserFields(state, fields) {
      state.userFields = fields;
    },
    setUserField(state, field) {
      state.userField = field;
    },
    updateUserDefinedGroups(state, field) {
      state.userDefinedGroups.push(field);
    },
  },
  getters: {},
  actions: {
    getUserDefinedGroups({ commit }) {
      axios
        .get('http://localhost:57260/Property/UserDefinedGroup')
        .then(result => commit('setUserDefinedGroups', result.data))
        .catch(console.error);
    },
    getUserDefinedGroup({ commit }, item) {
      axios
        .get(`http://localhost:57260/Property/UserDefinedGroup/${item.id}`)
        .then(result => commit('setUserDefinedGroup', result.data))
        .catch(console.error);
    },
    getUserFields({ commit }, item) {
      axios
        .get(`http://localhost:57260/Property/UserDefinedField/group/${item.groupId}`)
        .then(result => commit('setUserFields', result.data))
        .catch(console.error);
    },
    getUserField({ commit }, item) {
      axios
        .get(`http://localhost:57260/Property/UserDefinedField/${item.id}`)
        .then(result => commit('setUserField', result.data))
        .catch(console.error);
    },
    saveUserDefinedGroup({ commit }, item) {
      axios
        .post('http://localhost:57260/Property/UserDefinedGroup', item.group)
        .then(response => commit('updateUserDefinedGroups', response.data))
        .catch(console.error);
    },
    updateUserDefinedGroup({ commit }, item) {
      axios
        .put('http://localhost:57260/Property/UserDefinedGroup', item.group)
        .then(response => commit('updateUserDefinedGroups', response.data))
        .catch(console.error);
    },
    // deleteUserDefinedGroup(item) {
    //   axios
    //     .delete(`http://localhost:57260/Property/UserDefinedField/${item.id}`)
    //     .then((response) => {})
    //     .catch(console.error);
    // },
  },
};
