import { fetchList, createCommunity, updateCommunity, fetchProvinceList, fetchCityList, fetchDistrictList } from '@/api/community'

const community = {
  namespaced: true,
  state: {
    total: '',
    list: [],
    detail: {
      longitude: '',
      latitude: ''
    },
    provinceList: [],
    cityList: [],
    districtList: [],
    editCityList: [],
    editDistrictList: []
  },

  mutations: {
    SET_LIST: (state, list) => {
      state.list = list
    },
    Insert_LIST: (state, detail) => {
      state.list = [detail, ...state.list]
    },
    UpdateList: (state, detail) => {
      state.list = state.list.map(item => {
        if (item.id === detail.id) {
          return detail
        }
        return item
      })
    },
    SET_TOTAL: (state, total) => {
      state.total = total
    },
    setDetail: (state, item) => {
      state.detail = item
    },
    SET_PROVINCE_LIST: (state, provinceList) => {
      state.provinceList = provinceList
    },
    SET_CITY_LIST: (state, cityList) => {
      state.cityList = cityList
    },
    SET_DISTRICT_LIST: (state, districtList) => {
      state.districtList = districtList
    },
    SET_EDIT_CITY_LIST: (state, editCityList) => {
      state.editCityList = editCityList
    },
    SET_EDIT_DISTRICT_LIST: (state, editDistrictList) => {
      state.editDistrictList = editDistrictList
    },
    resetDetail: (state) => {
      state.detail = {
        ...{
          id: undefined,
          importance: 1,
          remark: '',
          timestamp: new Date(),
          title: '',
          status: 'published',
          type: '',
          longitude: '',
          latitude: ''
        }
      }
    }
  },

  actions: {
    // fetchList,获取社区列表
    FetchCommunityList({ commit }, listQuery) {
      return new Promise((resolve, reject) => {
        fetchList(listQuery).then(response => {
          const data = response.data
          commit('SET_LIST', data.items)
          commit('SET_TOTAL', data.total)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    CreateCommunity({ commit }, datail) {
      return new Promise((resolve, reject) => {
        createCommunity(datail).then(response => {
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },

    UpdateCommunity({ commit }, datail) {
      return new Promise((resolve, reject) => {
        updateCommunity(datail).then(response => {
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },

    FetchProvinceList({ commit }) {
      return new Promise((resolve, reject) => {
        fetchProvinceList().then(response => {
          const data = response.data
          commit('SET_PROVINCE_LIST', data)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    FetchCityList({ commit }, provinceId) {
      return new Promise((resolve, reject) => {
        fetchCityList(provinceId).then(response => {
          const data = response.data
          commit('SET_CITY_LIST', data)
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },

    FetchDistrictList({ commit }, cityId) {
      return new Promise((resolve, reject) => {
        fetchDistrictList(cityId).then(response => {
          const data = response.data
          commit('SET_DISTRICT_LIST', data)
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },

    FetchEditCityList({ commit }, provinceId) {
      return new Promise((resolve, reject) => {
        fetchCityList(provinceId).then(response => {
          const data = response.data
          commit('SET_EDIT_CITY_LIST', data)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    FetchEditDistrictList({ commit }, cityId) {
      return new Promise((resolve, reject) => {
        fetchDistrictList(cityId).then(response => {
          const data = response.data
          commit('SET_EDIT_DISTRICT_LIST', data)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    }

  }
}

export default community
