// pages/user/localtion/index.js

const {
  globalData: {
    util: {
      regeneratorRuntime,
      api: {
        allDept: url
      },
      request: {
        get
      },
      navigateBack,
      navigate_auth_native,
      telephone_native

    }
  }

} = getApp();
Page({

 

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad() {
    this.p = get(url)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    wx.getLocation({
      type: 'wgs84',
      success: async({
        latitude,
        longitude
      }) => {

        let items= await this.p //去分页后 去除花括号

        items.forEach(item => {
          let [x, y] = item.position.split(',')
          item.distance = this.getDistance(latitude, longitude, y, x) / 1000
          item.longlat = [x, y]
          if(item.deptName.indexOf("李楼")!=-1){
            item.img =  '/images/address/lilouzheng.jpg'
          } else if (item.deptName.indexOf("白马寺") != -1) {
            item.img = '/images/address/baimasi.jpg'
          } else if (item.deptName.indexOf("关林") != -1) {
            item.img = '/images/address/guanlin.jpg'
          } else if (item.deptName.indexOf("古城") != -1) {
            item.img = '/images/address/gucheng.jpg'
          } else if (item.deptName.indexOf("科技园") != -1) {
            item.img = '/images/address/kejiyuan.jpg'
          } else if (item.deptName.indexOf("泉舜") != -1) {
            item.img = '/images/address/quanshun.jpg'
          } else if (item.deptName.indexOf("龙门街道") != -1) {
            item.img = '/images/address/longmenjiedao.jpg'
          } else if (item.deptName.indexOf("太康") != -1) {
            item.img = '/images/address/taikangdonglu.jpg'
          } else if (item.deptName.indexOf("学府") != -1) {
            item.img = '/images/address/xuefujie.jpg'
          } else if (item.deptName.indexOf("安乐") != -1) {
            item.img = '/images/address/anlezhenzhengfu.jpg'
          } else if (item.deptName.indexOf("开元") != -1) {
            item.img = '/images/address/kaiyuanlu.jpg'
          }else{
            item.img = '/images/address/luoyang.jpg'
          }
        })
        this.setData({
          items: items.sort((a, b) => a.distance - b.distance).map(({ deptName: name, img,deptLeader: tel, distance,longlat})=>({
            name, tel, distance, img,longlat
          })),

        })
      },
      fail(e) {
        wx.getSetting({
          success({
            authSetting
          }) {
            if (!authSetting['scope.userLocation']) {
              wx.showModal({
                title: '是否授权当前位置',
                content: '需要获取您的地理位置，请确认授权，否则地图功能将无法使用',
                success: function(tip) {
                  if (tip.confirm) {
                    wx.openSetting({
                      success: function(data) {
                        if (data.authSetting["scope.userLocation"] === true) {
                          wx.showToast({
                            title: '授权成功',
                            icon: 'success',
                            duration: 1000
                          })
                        }
                      }
                    })

                  } else {
                    // wx.showToast({
                    //   title: '授权失败',
                    //   icon: 'none',
                    //   duration: 1000
                    // })
                    navigateBack();
                  }
                }
              })
            }
          }
        })
      }
    })
  },

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

  },

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

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },

  getDistance: function(lat1, lng1, lat2, lng2) {
    lat1 = lat1 || 0;
    lng1 = lng1 || 0;
    lat2 = lat2 || 0;
    lng2 = lng2 || 0;
    var rad1 = lat1 * Math.PI / 180.0;
    var rad2 = lat2 * Math.PI / 180.0;
    var a = rad1 - rad2;
    var b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
    var r = 6378137;
    return (r * 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(rad1) * Math.cos(rad2) * Math.pow(Math.sin(b / 2), 2)))).toFixed(0)
  },
  map:function(e){
    wx.openLocation({
      latitude: parseFloat(e.currentTarget.dataset.w),
      longitude: parseFloat(e.currentTarget.dataset.j),
      name: e.currentTarget.dataset.name ? e.currentTarget.dataset.name:'',
      scale: 18
    })
  },
  navigate_auth_native,
  telephone_native

})