import request from '../../utils/request'
import apis from '../../config/api'

export default {
  namespaced: true,
  state: {
    apartmentts: [],
    detail: {}
  },
  mutations: {
    updateList (state, payload) {
      state.apartments = payload
    },
    updateDetail (state, payload) {
      state.detail = payload
    }
  },
  actions: {
    setDetailNull ({ commit }) {
      commit('updateDetail', {})
    },
    getApartments ({ commit }, payload) {
      return new Promise((resolve, reject) => {
        request({
          ...apis.apartment.list,
          urlData: payload,
        }).then((data) => {
          commit('updateList', data)
          resolve(data)
        }).catch(({ message }) => {
          if (typeof message === 'string') {
            reject(message)
          }
        })
      })
    },
    getApartmentDetail ({ commit }, payload) {
      return new Promise((resolve, reject) => {
        request({
          ...apis.apartment.detail,
          urlData:payload
        }).then((data) => {
          commit('updateDetail', data)
          resolve(data)
        }).catch(({ message }) => {
          if (typeof message === 'string') {
            reject(message)
          }
        })
      })
    },
    addApartment (_, payload) {
      return new Promise((resolve, reject) => {
        request({
          ...apis.apartment.add,
          data:payload
        }).then((data) => {
          
          resolve(data)
        }).catch(() => {
          reject()
        })
      })
    },
    editApartment (_, payload) {
      return new Promise((resolve, reject) => {
        request({
          ...apis.apartment.edit,
          urlData:{id:payload.apartmentId},
          data:payload
        }).then((data) => {
          resolve(data)
        }).catch(() => {
          reject()
        })
      })
    },
    deleteApartment (_, payload) {
      return new Promise((resolve, reject) => {
        // const api = replaceApiParams(lodash.get(apis, 'apartment.'), payload)
        request({
          ...apis.apartment.delete,
          urlData:payload
        }).then(() => {
          resolve()
        }).catch(() => {
          reject()
        })
      })
    },
  }
}
