import axios from 'axios';

export default {
  namespaced: true,
  state: {
    provinces: [],
    cities: [],
    suburbs: [],
  },
  mutations: {
    setProvince(state, provinces) {
      state.provinces = provinces;
    },
    setCities(state, cities) {
      state.cities = cities;
    },
    setSuburbs(state, suburbs) {
      state.suburbs = suburbs;
    },
  },
  getters: {},
  actions: {
    getProvince({
      commit,
    }) {
      axios.get('http://localhost:57260/region/province')
        .then(result => commit('setProvince', result.data))
        .catch(console.error);
    },
    getCities({
      commit,
    }, province) {
      axios
        .get(`http://localhost:57260/region/city/getby/${province}`)
        .then(result => commit('setCities', result.data));
    },
    getSuburbs({
      commit,
    }, item) {
      axios
        .get(
          `http://localhost:57260/region/Suburb/${item.province}/${item.city}`,
        )
        .then(result => commit('setSuburbs', result.data));
    },
  },
};
