import Ajax from '../../util/ajax'
import api from '../../util/api'

const http = new Object

// 请求数据
http.getIndexLocation = (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) => {
  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.getMessage = (data) => {
  return new Promise((resolve, reject) => {
    Ajax(api.index.message.url, {
      method: api.index.message.method,
      queryData: {
        locationid: data.locationid,
        orgid: data.orgid
      }
    }).then(res => {
      resolve(res)
    }).catch((err) => {
      reject(err)
    })
  })
}

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

http.getCms = (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)
    })
  })
}

export default {
  namespaced: true,
  state: {
    banner: [],
    fiveA: [],
    message: [],
    project: [],
    cms: [],
    courseList: [],
    longitude1: 0,
    latitude1: 0
  },
  mutations: {
    setBanner (state, data) { // 获取首页轮播图
      data = data || []
      state.banner = data
    },
    setFiveA (state, data) { // 获取5A
      data = data || []
      state.fiveA = data
    },
    setMessage (state, data) { // 获取首页消息
      data = data || []
      if (data.length > 3) {
        state.message = data.slice(0, 3)
      } else {
        state.message = data
      }
    },
    setProject (state, data) { // 获取项目专题
      state.project = data
      // if (state.latitude1 === 0 && state.longitude1 === 0) {
      //   data.forEach((item) => {
      //     item.jl = '未获取到当前定位'
      //   })
      //   state.project = data
      // } else {
      //   data = data || []
      //   let arr = []
      //   let dataNew = []
      //   data.forEach((item) => {
      //     if (item.Coordinate) {
      //       item.jl = parseInt(toolClass.getGreatCircleDistance(state.latitude1, state.longitude1, item.Coordinate.split(',')[0], item.Coordinate.split(',')[1]))
      //       arr.push({ jl: item.jl, CmsCaseId: item.CmsCaseId })
      //     } else {
      //       item.jl = 400000000
      //       arr.push({ jl: item.jl, CmsCaseId: item.CmsCaseId })
      //     }
      //   })
      //   arr.sort((a, b) => { return a.jl - b.jl })
      //   arr.forEach((curr) => {
      //     data.forEach((item) => {
      //       if (curr.CmsCaseId === item.CmsCaseId && item.jl === curr.jl) {
      //         item.jl = `距离${item.jl}米`
      //         dataNew.push(item)
      //       }
      //     })
      //   })
      //   state.project = dataNew
      // }
    },
    setCms (state, data) { // 获取cms
      data = data || []
      state.cms = data
    },
    updateCourseList (state, data) { // 更新附近课程
      state.courseList = data.list || []
    },
    upDateLocation (state, data) {
      state.longitude1 = data.longitude1
      state.latitude1 = data.latitude1
    }
  },
  actions: {
    setLocation (context, data) {
      context.commit('upDateLocation', data)
    },
    getCourseList (context, payload) { // 获取附近课程
      return new Promise((resolve, reject) => {
        Ajax(api.index.getCourseList.url, {
          method: api.index.getCourseList.method,
          queryData: { ...payload }
        }).then(res => {
          context.commit('updateCourseList', res)
          resolve(res)
        }).catch((err) => {
          reject(err)
        })
      })
    },
    getIndexLocation (context, data) {
      return new Promise((resolve) => {
        http.getIndexLocation(data).then((res) => {
          context.commit('setFiveA', res)
          resolve(res)
        })
      })
    },
    getProjectList (context, data) {
      return new Promise((resolve, reject) => {
        Ajax(api.index.project.url, {
          method: api.index.project.method,
          queryData: {
            locationid: data.locationid,
            orgid: data.orgid
          }
        }).then(res => {
          context.commit('setProject', res)
          resolve(res)
        }).catch((err) => {
          reject(err)
        })
      })
    },
    async getIndexInfo (context, param) { // 首页全部请求
      try {
        let banner = await http.getBanner(param)
        context.commit('setBanner', banner)
        let message = await http.getMessage(param)
        context.commit('setMessage', message)
        let project = await http.getProject(param)
        context.commit('setProject', project)
        let cms = await http.getCms(param)
        context.commit('setCms', cms)
      } catch (err) {
        console.log(err)
      }
    }
  }
}
