import axios from "axios";

export default {
  namespaced: true,
  state: {
    userDefinedGroups: [],
    userDefinedGroup: {
      id: 0,
      description: "",
      usageType: 0,
      rank: 0,
      fields: []
    },
    userFields: [],
    userField: {
      id: 0,
      groupId: 0,
      fieldName: "",
      fieldType: "",
      rank: 0
    }
  },
  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;
    },
    addUserDefinedGroups(state, field) {
      state.userDefinedGroups.push(field);
    },
    updateUserDefinedGroups(state, field) {
      state.userDefinedGroups.find(item => item.id === field.id).description = field.description;
      state.userDefinedGroups.find(item => item.id === field.id).usageType = field.usageType;
      state.userDefinedGroups.find(item => item.id === field.id).rank = field.rank;
    },
    addUserField(state, field) {
      state.userFields.push(field);
    },
    updateUserFields(state, field) {
      state.userFields.find(item => item.id === field.id).fieldName = field.fieldName;
      state.userFields.find(item => item.id === field.id).fieldType = field.fieldType;
      state.userFields.find(item => item.id === field.id).rank = field.rank;
    },
    removeUserDefinedGroup(state, id) {
      state.userDefinedGroups.pop(state.userDefinedGroups.find(p => p.id === id));
    },
    removeUserField(state, id) {
      state.userFields.pop(state.userFields.find(p => p.id === id));
    },
    clearUserGroup(state) {
      state.userDefinedGroup = {
        id: 0,
        description: "",
        usageType: 0,
        rank: 0
      };
    },
    clearUserField(state) {
      state.userField = {
        id: 0,
        fieldName: "",
        fieldType: "",
        rank: 0
      };
    },
    clearUserFields(state) {
      state.userFields = [];
    }
  },
  getters: {},
  actions: {
    getUserDefinedGroups({ commit }) {
      axios
        .get("/api/UserDefinedGroup")
        .then(result => commit("setUserDefinedGroups", result.data))
        .catch(console.error);
    },
    getUserDefinedGroup({ commit }, id) {
      axios
        .get(`/api/UserDefinedGroup/${id}`)
        .then(result => commit("setUserDefinedGroup", result.data))
        .catch(console.error);
    },
    getUserFields({ commit }, groupId) {
      axios
        .get(`/api/UserDefinedField/group/${groupId}`)
        .then(result => commit("setUserFields", result.data))
        .catch(console.error);
    },
    getUserField({ commit }, id) {
      axios
        .get(`/api/UserDefinedField/${id}`)
        .then(result => commit("setUserField", result.data))
        .catch(console.error);
    },
    saveUserDefinedGroup({ commit }, item) {
      axios
        .post("/api/UserDefinedGroup", item)
        .then(response => commit("addUserDefinedGroups", response.data))
        .catch(console.error);
    },
    updateUserDefinedGroup({ commit }, item) {
      axios
        .put("/api/UserDefinedGroup", item)
        .then(response => commit("updateUserDefinedGroups", response.data))
        .catch(console.error);
    },
    saveUserField({ commit }, item) {
      axios
        .post("/api/UserDefinedField", item)
        .then(response => commit("addUserField", response.data))
        .catch(console.error);
    },
    updateUserField({ commit }, item) {
      axios
        .put("/api/UserDefinedField", item)
        .then(response => commit("updateUserFields", response.data))
        .catch(console.error);
    },
    deleteUserDefinedGroup({ commit }, id) {
      axios
        .delete(`/api/UserDefinedGroup/${id}`)
        .then(result => commit("removeUserDefinedGroup", id))
        .catch(console.error);
    },
    deleteUserField({ commit }, id) {
      axios
        .delete(`/api/UserDefinedField/${id}`)
        .then(result => commit("removeUserField", id))
        .catch(console.error);
    },
    clearUserGroup({ commit }) {
      commit("clearUserGroup");
    },
    clearUserField({ commit }) {
      commit("clearUserField");
    },
    clearUserFields({ commit }) {
      commit("clearUserFields");
    }
  }
};
