// pages/ucenter/mychargestation/mychargestation.js
let Util = require('../../../utils/util');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    markers: []
  },
  goIndex(e) {
    //console.log(e);
    let markerId = e.currentTarget.id;
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 3]; //上一个页面
    this.data.markers.forEach((item, index) => {
      if (markerId && markerId == item.id) {
        let {
          latitude,
          longitude
        } = item;
        prevPage.setData({
          markerId,
          latitude,
          longitude,
          textData: item
        });

        wx.navigateBack({
          delta: 3
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let userInfo = wx.getStorageSync('userInfo');
    let isLogin = wx.getStorageSync('isLogin');
    let that = this;
    //console.log(12345);


    if (userInfo && isLogin) {
      wx.getLocation({
        success: function(res) {
          //console.log(res);
          let {
            latitude,
            longitude,
          } = res;
          wx.request({
            url: getApp().globalData.postHeadAgreement +'/restapi/wechat/userChargStations',
            data: {
              userId: userInfo.userId,
              lat: latitude,
              lon: longitude
            },
            method: 'POST',
            success(res) {
              console.log(res);
              let {
                data
              } = res;
              let markers = [];
              data.forEach((item, index) => {
                //item.callout = {
                // content: item.name, //文本	String	1.2.0
                //  display: 'BYCLICK', //'BYCLICK': 点击显示; 'ALWAYS': 常显	String	1.2.0
                // textAlign: 'center' //文本对齐方式。有效值: left, right, center	String	1.6.0
                // };
                //console.log(Util.distance(latitude, longitude, item.lat, item.lon));
                var marker = {
                  name: item.chargStationName,
                  address: item.address,
                  width: "46rpx",
                  height: "67rpx",
                  iconPath: "/images/marker.png",
                  id: item.id,
                  callout: {},
                  latitude: item.lat,
                  longitude: item.lon,
                  distance: Math.round(item.distance) / 1000,
                  //distance: Util.distance(latitude, longitude, item.lat, item.lon),
                  chargPileNum: item.fastCharg + item.slowCharg,
                  fastCharg: item.fastCharg,
                  slowCharg: item.slowCharg,
                  freenum: item.freenum,
                  fastfreenum: item.fastfreenum,
                  slowfreenum: item.slowfreenum,
                  breaknum: item.breaknum,
                  /** 电费 */
                  chargprice: item.chargprice,
                  /** 服务费 */
                  serviceprice: item.serviceprice,
                  /** 停车费 */
                  stopprice: item.stopprice,
                  operationState: item.operationState
                };
                markers[index] = marker;
              });
              that.setData({
                markers
              });
            }
          });
        }
      });
    }
  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})