// pages/server_point/server_point.js
const {
  api,
  rapi,
  formatTime,
  imgName,
  getUserInfo,
  wxLogin,
  alertTip,
  authAgainByUser,
  userLocation
} = require('../../lib/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    first: true,
    imgLoc: imgName('pointloc.png'),
    imgNav: imgName('navigate.png'),
    imgPhone: imgName('security_settings_telephone_active.png'),
    imgLogo: imgName('logo.png'),
    longitude: 120.126593,
    latitude: 30.342009,
    markers: [],
    page: 1, //当前页面
    hasMorePage: false, //更多页面
    pointLists: [], //站点列表
    showList: [], //我的服务列表
    serverPage: 1, //当前页面
    hasaAuthorization: true,
    serverHasMorePage: false, //更多页面
    tips:''
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },
  //站点
  toPoint(e) {
    let that = this
    let {
      point
    } = e.currentTarget.dataset
    wx.openLocation({
      latitude: Number(point.latitude),
      longitude: Number(point.longitude),
      scale: 28
    })
  },
  onShow() {
    this.getPointLists(1)
  },
  //获取站点列表
  getPointLists(page) {
    let curPage = page || this.data.page
    let that = this
    userLocation()
      .then(res => {
        this.setData({
          hasaAuthorization: true
        })
        let {
          longitude,
          latitude
        } = res
        api.get('site/getNearSiteList', {
            longitude,
            latitude,
            curPage
          })
          .then(res => {
            if(res.items.length==0){
              if (curPage===1){
                this.setData({
                  tips:'未开通相应的服务网点'
                })
              }else{
                this.setData({
                  tips: '无更多数据'
                })
              }
            }
            let markers = []
            let listArrays = res.items.map(item => {
              item.distance = parseFloat(item.distance).toFixed(1);
              let oMarker = {
                iconPath: '/static/image/pointloc.png',
                width: 25,
                height: 25
              }
              if (item.latitude && item.longitude) {
                oMarker.latitude = item.latitude
                oMarker.longitude = item.longitude
                markers.push(oMarker)
              }
              return item
            })
            wx.createMapContext('myMap').includePoints({
              padding: [10],
              points: markers
            })
            //首页)
            if (curPage == 1) {
              this.setData({
                pointLists: listArrays,
                page: curPage + 1,
                hasMorePage: res.page < res.totalPage
              })
            } else {
              let newPoints = that.data.pointLists.concat(listArrays)
              this.setData({
                pointLists: newPoints,
                page: curPage + 1,
                hasMorePage: res.page < res.totalPage
              })
            }
          })
          .catch(err => {
            alertTip(err)
          })
      })
      .catch(err => {
        this.setData({
          hasaAuthorization: false
        })
        alertTip('你拒绝了地理定位，请打开后重试')
      })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },
  //拨打电话
  callPointPhone(e) {
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.id
    })
  },
  //显示站点详细信息
  toPointDetail(e) {
    let pointId = e.currentTarget.id
    let index = e.currentTarget.dataset.index
    wx.setStorageSync('point', this.data.pointLists[index])
    //站点详情&look=false
    wx.navigateTo({
      url: `/pages/point_detail/point_detail`,
    })
  },
  //获取服务工单
  getMyOrderList(page) {
    // let curPage = page || this.data.serverPage
    // let that = this
    // let userId = wx.getStorageSync('user').userMapp.id
    // rapi.get('userOrder/myOrderList', {
    //   userId,
    //   curPage,
    // }).then(res => {
    //   let listArrays = res.items.map(item => {
    //     item.pics = item.pics.split(',')[0]
    //     let stateArray = ['已下单', '已接单', '待评价', '已评价', '已取消']
    //     item.state = stateArray[item.state]
    //     item.orderTime = formatTime(new Date(item.orderTime))
    //     return item
    //   })
    //   //首页
    //   if (curPage == 1) {
    //     this.setData({
    //       showList: listArrays,
    //       serverPage: res.model.haveMore ? (curPage + 1) : 1,
    //       serverHasMorePage: res.model.haveMore
    //     })
    //   } else {
    //     let newArrays = that.data.showList.concat(listArrays)
    //     this.setData({
    //       showList: newArrays,
    //       serverPage: res.model.haveMore ? (curPage + 1) : page,
    //       serverHasMorePage: res.model.haveMore
    //     })
    //   }
    // }).catch(err => {
    //   console.log(err)
    // })
  },
  //获取服务详情
  toServerDetail(e) {
    let serverId = e.currentTarget.id
    wx.navigateTo({
      url: `/pages/server_detail/server_detail?id=${serverId}`,
    })
  }
})