import axios from 'axios';

export default {
  namespaced: true,
  state: {
    property: {
      createdBy: '',
      propertyTypeId: 0,
      propertyName: '',
      unit: '',
      operationalCosts: 0.0,
      price: 0,
      pricePer: '',
      isSale: false,
      description: '',
      shortDescription: '',
      addressLine1: '',
      addressLine2: '',
      addressLine3: '',
      suburbId: 0,
      cityId: 0,
      provinceId: 0,
      published: false,
      propertyUserFields: [],
      propertyImages: [],
      newImages: [],
      id: 0,
      virtualTour: '',
      video: '',
      userId: 0,
    },
    propertyImages: [],
    propertyTypes: [],
    propertyTypesRes: [],
    propertyTypesCom: [],
    propertyOverviewFields: [],
    propertyFields: [],
    mayEdit: false,
    newPropertyImages: {
      propertyId: 0,
      images: [],
    },
  },
  mutations: {
    setProperty(state, property) {
      state.property = property;
    },
    setPropertyImages(state, images) {
      state.propertyImages = images;
    },
    setPropertyOverviewFields(state, fields) {
      state.propertyOverviewFields = fields;
    },
    setPropertyFields(state, fields) {
      state.propertyFields = fields;
    },
    setMayEdit(state, data) {
      state.mayEdit = data;
    },
    clearProperty(state) {
      state.property = {
        createdBy: '',
        propertyTypeId: 0,
        propertyName: '',
        unit: '',
        operationalCosts: 0.0,
        price: 0,
        pricePer: '',
        isSale: false,
        description: '',
        shortDescription: '',
        addressLine1: '',
        addressLine2: '',
        addressLine3: '',
        suburbId: 0,
        cityId: 0,
        provinceId: 0,
        published: false,
        propertyOverviewFields: [],
        propertyFields: [],
        propertyImages: [],
        newImages: [],
        id: 0,
        virtualTour: '',
        video: '',
        userId: 0,
      };
    },
    clearPropertyImages(state) {
      state.propertyImages = [];
    },
    ClearNewImages(state) {
      state.newPropertyImages = {
        propertyId: 0,
        images: [],
      };
    },
  },
  getters: {},
  actions: {
    getSavedPropertyData({ commit }, item) {
      commit('clearProperty');
      commit('clearPropertyImages');

      axios
        .get(`/api/Property/getProperty/${item.id}`)
        .then(result => commit('setProperty', result.data))
        .catch(console.error);

      axios
        .get(`/api/PropertyImage/GetProperySavedImages/${item.id}`)
        .then(result => commit('setPropertyImages', result.data))
        .catch(console.error);

      axios
        .get(`/api/PropertyFields/GetSavedValues/Residential/Property Overview/${item.id}`)
        .then(response => commit('setPropertyOverviewFields', response.data))
        .catch(console.error);

      axios
        .get(`/api/PropertyFields/GetSavedValues/${item.type}/All/${item.id}`)
        .then(response => commit('setPropertyFields', response.data))
        .catch(console.error);
    },
    updateProperty({ commit }, item) {
      return new Promise((resolve) => {
        axios
          .post('/api/PropertyImage', item.images)
          .then(commit('ClearNewImages'))
          .catch(console.error);

        axios
          .put('/api/Property', item.property)
          .then(commit('setProperty', item.property))
          .catch(console.error);
        resolve(true);
      });
    },
    mayEditProperty({ commit }, id) {
      axios
        .get(`/api/property/MayEditProperty/${id}`)
        .then(response => commit('setMayEdit', response.data))
        .catch(console.error);
    },
  },
};
