import axios from 'axios';

export default {
  namespaced: true,
  state: {
    property: {
      id: 0,
      propertyTypeId: 0,
      propertyName: '',
      unit: '',
      addressLine1: '',
      addressLine2: '',
      addressLine3: '',
      suburbId: 0,
      cityId: 0,
      provinceId: 0,
      price: '',
      per: '',
      description: '',
      isSale: false,
      propertyUserFields: [],
      propertyImages: [],
    },
    propertyImages: [],
    propertyTypes: [],
    propertyTypesRes: [],
    propertyTypesCom: [],
    propertyOverviewFields: [],
    propertyFields: [],
    properties: [],
    latestProperties: [],
  },
  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;
    },
    setLatestProperties(state, properties) {
      state.latestProperties = properties;
    },
  },
  getters: {},
  actions: {
    getProperty({ commit }, id) {
      console.log(id);
      axios
        .get(`http://localhost:57260/Property/Property/${id}`)
        .then(result => commit('setProperty', result.data))
        .catch(console.error);
    },
    getPropertyImages({ commit }, id) {
      axios
        .get(`http://localhost:57260/property/PropertyImage/getpropertyimages/${id}`)
        .then(result => commit('setPropertyImages', result.data))
        .catch(console.error);
    },
    getPropertyTypes({ commit }, propertyType) {
      axios
        .get(`http://localhost:57260/Property/PropertyType/type/${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 }, propertyType) {
      axios
        .get(`http://localhost:57260/property/propertyfields/Propertytype/${propertyType}`)
        .then(response => commit('setPropertyFields', response.data));
    },
    saveProperty({ commit }, item) {
      axios
        .post('http://localhost:57260/Property/Property', item)
        .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/${item.keyword}`)
        .then(response => commit('updateSearch', response.data))
        .catch(console.error);
    },
    searchLatestProperties({ commit }) {
      axios
        .get('http://localhost:57260/property/property/latestProperties')
        .then(response => commit('setLatestProperties', response.data))
        .catch(console.error);
    },
  },
};
