var app = getApp();
const util = require('../../utils/utils.js');
Page({
  data: {
    windowHeight:'',
    height:'',
    order:'',
    date:[],
    markers:[],
    polyline:[],
    latitude:'',
    longitude:'',
    url:'',
    index:'',
    img:app.globalData.imgUrl
  },
  onLoad: function (options) {
    var that = this;
    console.log(options)
    wx.getSystemInfo({
      success: function (res) {
        console.log(res.windowHeight);
        that.setData({ windowHeight: res.windowHeight})
      }
    })
    this.setData({order:options.order})
    if(options.index){
      if (options.index == 0) {
        that.setData({
          url : '/order?order_no='
        })
      }
      if (options.index == 1) {
        that.setData({
          url : '/rent/order?no='
        })
      }
      this.setData({index:options.index})
    }
    if(options.index){
      if (options.index == 'true') {
        that.setData({
          url : '/order?order_no='
        })
        this.setData({index:0})
      }
      if (options.index == 'false') {
        that.setData({
          url : '/rent/order?no='
        })
        this.setData({index:1})
      }
    }
    var query = wx.createSelectorQuery();
    query.select('.box').boundingClientRect();
    query.exec(function (res) {
      console.log(res)
      console.log(res[0].height)
      that.setData({ height: res[0].height });
    })
    wx.getLocation({
      type:'gcj02',
      success: function(res) {
        var latitude = res.latitude
        var longitude = res.longitude
        that.setData({ longitude: longitude, latitude: latitude })
      },
    })
    // this.load(); 
    this.getTrack();  //获取行车轨迹及订单信息
  },
  // 获取行车轨迹及订单信息
  getTrack(){
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })
    console.log(that.data.url)
    app.request(that.data.url + that.data.order+'&include=locations','','GET').then(res=>{
      console.log(res)
      if(res.statusCode == 200){
        wx.hideLoading()
        that.setData({ date: res.data });
        let points = res.data.locations.data;
        var center = parseInt(points.length/2)
        var obj = {};
        obj.points = points;
        obj.width = 4;
        obj.color = '#18D5B9';
        obj.borderWidth = 3;
        obj.borderColor = '#18D5B9';
        that.setData({ polyline: that.data.polyline.concat(obj) })
        if (points.length>0){
          var maker = [];
          maker = maker.concat(points[0]);
          maker = maker.concat(points[points.length - 1])
          for (var i = 0; i < maker.length; i++) {
            maker[i].width = 32;
            maker[i].height = 32;
            maker[i].zIndex = 1111;
          }
          //在地图上显示两个坐标
          maker[0].iconPath = 'http://resource.bike.hanyiyun.com/weapp/start1.png'
          maker[1].iconPath = 'http://resource.bike.hanyiyun.com/weapp/end1.png'
          that.setData({ markers: maker, longitude: points[center].longitude, latitude: points[center].latitude })
        } else {
          wx.getLocation({
            type: 'gcj02',
            success: function (res) {
              var latitude = res.latitude
              var longitude = res.longitude
              that.setData({ longitude: longitude, latitude: latitude })
            },
          })
        }
      }
    })
  },
  onReady: function () {

  },
  payment: util.throttle(function(){
    //点击去支付
    var url = ''
    if(this.data.index==1){
      url = '&rent=rent'
    }
    wx.navigateTo({
      url: '../payment/payment?order='+this.data.order+url,
    })
  },1000),
  onShow: function () {

  },
  onHide: function () {

  },
  onUnload: function () {

  },
  onPullDownRefresh: function () {

  },
  onReachBottom: function () {

  },
  onShareAppMessage: function () {
    
  }
})