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

export default {
  namespaced: true,
  state: {
    buildings: {},
    detail: {},
    cityNameList:[],
  },
  mutations: {
    updateList (state, payload) {
      state.buildings = payload
    },
    updateDetail (state, payload) {
      state.detail = payload
    },
    updateCityNameList (state, payload) {
      state.cityNameList = payload
    },

  },
  actions: {
    setDetailNull ({ commit }) {
      commit('updateDetail', {})
    },
    getBuildings ({ commit }, payload) {
      return new Promise((resolve, reject) => {
        request({
          ...apis.building.list,
          params: payload,
        }).then((data) => {
          window.console.log(data)
          commit('updateList', { ...data, list: data.records})
          resolve(data)
        }).catch(({ message }) => {
          if (typeof message === 'string') {
            reject(message)
          }
        })
      })
    },
    getCityName ({ commit }, payload) {
      return new Promise((resolve, reject) => {
        request({
          ...apis.building.getCityName,
          params: payload,
        }).then((data) => {
          window.console.log(data,"data")
          commit('updateCityNameList', data)
          resolve(data)
        }).catch(({ message }) => {
          if (typeof message === 'string') {
            reject(message)
          }
        })
      })
    },
    getDetail ({ commit }, payload) {
      return new Promise((resolve, reject) => {
        request({
          ...apis.building.detail,
          urlData: payload,
        }).then((data) => {
          commit('updateDetail', data)
          resolve(data)
        }).catch(({ message }) => {
          if (typeof message === 'string') {
            reject(message)
          }
        })
      })
    },
    addBuilding (_, payload) {
      const { onSuccess } = payload
      request({
        ...apis.building.add,
        data: payload.detail,
      }).then((data) => {
        onSuccess(data)
      })
    },
    editBuilding (_, payload) {
      const { onSuccess } = payload
      request({
        ...apis.building.edit,
        urlData: { id: payload.detail.buildingId },
        data: payload.detail
      }).then((data) => {
        onSuccess(data)
      })
    },
    delBuilding (_, payload) {
      return new Promise((resolve, reject) => {
        request({
          ...apis.building.delete,
          urlData: payload,
        }).then((data) => {
          resolve(data)
        }).catch(({ message }) => {
          if (typeof message === 'string') {
            reject(message)
          }
        })
      })
    },
    updateBuildingStatus (_, payload) {
      return new Promise((resolve, reject) => {
        request({
          ...apis.building.updateStatus,
          data: payload,
        }).then((data) => {
          resolve(data)
        }).catch(({ message }) => {
          if (typeof message === 'string') {
            reject(message)
          }
        })
      })
    },
    getBuildingIntention(_, payload) { // 获取意向值列表和意向值
      return new Promise((resolve, reject) => {
        request({
          ...apis.buildingIntention.list,
          params: payload
        }).then((data) => {
          resolve(data)
        }).catch((err) => {
          reject(err)
        })
      })
    },
    saveOrUpdateBatchBuildingIntention(_, payload) { // 批量更新意向值和列表
      return new Promise((resolve, reject) => {
        request({
          ...apis.buildingIntention.saveOrUpdateBatch,
          urlData: { id: payload.buildingId },
          data: payload.list
        }).then((data) => {
          resolve(data)
        }).catch((err) => {
          reject(err)
        })
      })
    }
  }
}
