import Ajax from '../../util/ajax'
import api from '../../util/api'
const http = new Object

// 请求数据
http.getNav = (data) => {
  return new Promise((resolve, reject) => {
    Ajax(api.index.location.url, {
      method: api.index.location.method,
      queryData: {
        issys: data.issys,
        orgid: data.orgid
      }
    }).then(res => {
      resolve(res)
    }).catch((err) => {
      reject(err)
    })
  })
}

http.getBanner = (data) => {
  // console.log(data)
  return new Promise((resolve, reject) => {
    Ajax(api.index.banner.url, {
      method: api.index.banner.method,
      queryData: {
        locationid: data.locationid,
        orgid: data.orgid
      }
    }).then(res => {
      resolve(res)
    }).catch((err) => {
      reject(err)
    })
  })
}

http.getList = (data) => {
  // console.log(data)
  return new Promise((resolve, reject) => {
    Ajax(api.index.list.url, {
      method: api.index.list.method
    }).then(res => {
      resolve(res)
    }).catch((err) => {
      reject(err)
    })
  })
}

http.getCms = (data) => {
  // console.log(data)
  return new Promise((resolve, reject) => {
    Ajax(api.index.cms.url, {
      method: api.index.cms.method,
      queryData: {
        locationid: data.locationid,
        orgid: data.orgid
      }
    }).then(res => {
      resolve(res)
    }).catch((err) => {
      reject(err)
    })
  })
}

http.getLocationDetails = (data) => {
  return new Promise((resolve, reject) => {
    Ajax(api.index.locationDetails.url, {
      method: api.index.locationDetails.method,
    }).then(res => {
      resolve(res)
    }).catch((err) => {
      reject(err)
    })
  })
}

export default {
  namespaced: true,
  state: {
    nav: [],
    banner: [],
    list: [],
    cms: [],
    fiveAList: [],
    locationDetails: [],
  },
  mutations: {
    setNav (state, data) {
      state.nav = data
    },
    setBanner (state, data) {
      state.banner = data
    },
    setList (state, data) {
      state.list = data
    },
    setCms (state, data) {
      state.cms = data
    },
    setFiveAList (state, data) {
      state.fiveAList = data
    },
    setLocationDetails (state, data) {
      state.locationDetails = data
    }
  },
  actions: {
    getFiveAList (context, { ...payload }) { // 获取5A列表
      return new Promise((resolve) => {
        Ajax(api.index.getFiveAList.url, {
          method: api.index.getFiveAList.method,
          queryData: {
            ...payload
          },
        }).then(res => {
          context.commit('setFiveAList', res)
          resolve(res)
        })
      })
    },
    getNav (context, data) {
      return new Promise((resolve) => {
        http.getNav(data).then((res) => {
          context.commit('setNav', res)
          resolve(res)
        })
      })
    },
    getFiveAInfo (context, data) {
      // console.log(data)
      http.getBanner(data).then((res) => {
        context.commit('setBanner', res)
      })
      // http.getList(data).then((res) => {
      //   context.commit('setList', res)
      // })
      http.getCms(data).then((res) => {
        context.commit('setCms', res)
      })
    },
    getLocationDetails (context, data) {
      return new Promise((resolve) => {
        http.getLocationDetails(data).then((res) => {
          context.commit('setLocationDetails', res)
          resolve(res)
        })
      })
    }
  }
}
