import axios from 'axios';

export default {
  namespaced: true,
  state: {
    property: null,
    propertyImages: [],
    propertyTypes: [],
    propertyTypesRes: [],
    propertyTypesCom: [],
    propertyOverviewFields: [],
    propertyFields: [],
    properties: [],
  },
  mutations: {
    setProperty(state, property) {
      state.property = property;
    },
    setPropertyImages(state, images) {
      state.propertyImages = images;
    },
    setPropertyTypes(state, types) {
      state.propertyTypes = types;
    },
    setPropertyTypesRes(state, types) {
      state.propertyTypesRes = types;
    },
    setPropertyTypesCom(state, types) {
      state.propertyTypesCom = types;
    },
    setPropertyOverviewFields(state, fields) {
      state.propertyOverviewFields = fields;
    },
    setPropertyFields(state, fields) {
      state.propertyFields = fields;
    },
    updateCurrentProperty(state, property) {
      state.property = property;
    },
    updateSearch(state, propertySearch) {
      state.properties = propertySearch;
    },
  },
  getters: {},
  actions: {
    getProperty({ commit }, item) {
      axios
        .get(`http://localhost:57260/Property/Property/${item.id}`)
        .then(result => commit('setProperty', result.data))
        .catch(console.error);
    },
    getPropertyImages({ commit }, item) {
      axios
        .get(`http://localhost:57260/property/PropertyImage/getpropertyimages/${item.id}`)
        .then(result => commit('setPropertyImages', result.data))
        .catch(console.error);
    },
    getPropertyTypes({ commit }, item) {
      axios
        .get(`http://localhost:57260/Property/PropertyType/type/${item.propertyType}`)
        .then(result => commit('setPropertyTypes', result.data))
        .catch(console.error);
    },
    getPropertyTypesRes({ commit }) {
      axios
        .get('http://localhost:57260/Property/PropertyType/type/Residential')
        .then(result => commit('setPropertyTypesRes', result.data))
        .catch(console.error);
    },
    getPropertyTypesCom({ commit }) {
      axios
        .get('http://localhost:57260/Property/PropertyType/type/Commercial')
        .then(result => commit('setPropertyTypesCom', result.data))
        .catch(console.error);
    },
    getPropertyOverviewFields({ commit }) {
      axios
        .get('http://localhost:57260/Property/PropertyFields/Property Overview')
        .then(response => commit('setPropertyOverviewFields', response.data));
    },
    getPropertyFields({ commit }, item) {
      axios
        .get(`http://localhost:57260/property/propertyfields/Propertytype/${item.propertyType}`)
        .then(response => commit('setPropertyFields', response.data));
    },
    saveProperty({ commit }, item) {
      axios
        .post('http://localhost:57260/Property/Property', item.newProperty)
        .then(result => commit('updateCurrentProperty', result.data))
        .catch(console.error);
    },
    searchPropertiesParams({ commit }, item) {
      axios
        .get(
          `http://localhost:57260/Property/Property/Search/${item.type}/${item.propertyType}/${item.province}/${item.city}/${item.suburb}/${item.propType}`,
        )
        .then(response => commit('updateSearch', response.data))
        .catch(console.error);
    },
    searchPropertiesKeyword({ commit }, item) {
      axios
        .get(`http://localhost:57260/Property/Property/Search/Keyword/${item.keyword}`)
        .then(response => commit('updateSearch', response.data))
        .catch(console.error);
    },
  },
};
