import axios from 'axios';

export default {
  namespaced: true,
  state: {
    registerIndividual: {
      name: '',
      surname: '',
      email: '',
      cellNumber: '',
      username: '',
      password: '',
    },
    registerAgency: {
      name: '',
      eaabeffcNumber: '',
      companyRegNumber: '',
      user: {
        name: '',
        surname: '',
        email: '',
        cellNumber: '',
        username: '',
        password: '',
      },
    },
  },
  mutations: {
    setIndividual(state, type) {
      state.registerIndividual = type;
    },
    setAgency(state, type) {
      state.registerAgency = type;
    },
    addIndividual(state, type) {
      state.registerIndividual = type;
    },
    addAgency(state, type) {
      state.registerAgency = type;
    },
    updateIndividual(state, type) {
      state.registerIndividual.find(item => item.id === type.id).username = type.username;
      state.registerIndividual.find(item => item.id === type.id).password = type.password;
    },
    updateAgency(state, type) {
      state.registerAgency.find(item => item.id === type.id).eaabeffcNumber = type
        .eaabeffcNumber;
      state.registerAgency.find(item => item.id === type.id).companyRegNumber = type
        .companyRegNumber;
    },
    clearIndividual(state) {
      state.registerIndividual = {
        name: '',
        surname: '',
        email: '',
        cellNumber: '',
        username: '',
        password: '',
      };
    },
    clearAgency(state) {
      state.registerAgency = {
        name: '',
        eaabeffcNumber: '',
        companyRegNumber: '',
        user: {
          name: '',
          surname: '',
          email: '',
          cellNumber: '',
          username: '',
          password: '',
        },
      };
    },
    removeIndividual(state, id) {
      state.registerIndividual.pop(state.registerIndividual.find(p => p.id === id));
    },
    removeAgency(state, id) {
      state.registerAgency.pop(state.registerAgency.find(a => a.id === id));
    },
  },
  getters: {},
  actions: {
    // getIndividual({
    //   commit,
    // }, id) {
    //   axios
    //     .get(`http://192.168.6.188:5000/api/user/${id}`)
    //     .then(result => commit('setIndividual', result.data))
    //     .catch(console.error);
    // },
    // getAgency({
    //   commit,
    // }, id) {
    //   axios
    //     .get(`http://192.168.6.188:5000/api/agency/${id}`)
    //     .then(result => commit('setAgency', result.data))
    //     .catch(console.error);
    // },

    saveIndividual({
      commit,
    }, item) {
      axios
        .post('http://192.168.6.188:5000/api/register/register', item)
        .then(result => commit('addIndividual', result.data))
        .catch(console.error);
    },
    saveAgency({
      commit,
    }, item) {
      axios
        .post('http://192.168.6.188:5000/api/register/registeragency', item)
        .then(result => commit('addAgency', result.data))
        .catch(console.error);
    },
    // updateIndividual({
    //   commit,
    // }, item) {
    //   axios
    //     .put('http://192.168.6.188:5000/api/individual', item)
    //     .then(result => commit('updateIndividual', item))
    //     .catch(console.error);
    // },
    // updateAgency({
    //   commit,
    // }, item) {
    //   axios
    //     .put('http://192.168.6.188:5000/api/agency', item)
    //     .then(result => commit('updateAgency', item))
    //     .catch(console.error);
    // },
    // clearIndividual({
    //   commit,
    // }) {
    //   commit('clearIndividual');
    // },
    // clearAgency({
    //   commit,
    // }) {
    //   commit('clearAgency');
    // },
    // deleteIndividual({
    //   commit,
    // }, id) {
    //   axios
    //     .delete(`http://192.168.6.188:5000/api/individual/${id}`)
    //     .then(result => commit('removeIndividual', id))
    //     .catch(console.error);
    // },
    // deleteAgency({
    //   commit,
    // }, id) {
    //   axios
    //     .delete(`http://192.168.6.188:5000/api/agency/${id}`)
    //     .then(result => commit('removeAgency', id))
    //     .catch(console.error);
    // },
  },
};
