var QQMapWX = require('../../utils/qqmap-wx-jssdk.min.js');
var app = getApp();
var qqmapsdk = new QQMapWX({
  key: app.globalData.mapKey // 必填
});
Page({
  data: {
    longitude: '',
    latitude: '',
    suggestion: [],
    value: '',
    value1: '',
    height: 490,
    touchS: [0],
    touchE: [0],
    animatheightadd: null,
    mapHeight: 245,
    iconHeight:58,
    animatMap: null,
    windowHeight: null,
    page: 1,
    animaticon:null,
    posiHeight:'',
    img:app.globalData.imgUrl
  },
  tolower: function () {
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })
    var page = that.data.page + 1
    qqmapsdk.reverseGeocoder({
      location: {
        latitude: that.data.latitude,
        longitude: that.data.longitude
      }, //坐标
      get_poi: 1, //是否获取坐标对应附近列表
      poi_options: 'policy=2;radius=3000;page_size=10;page_index=' + page, //poi 参数
      success: function (res) {
        console.log(res)
        var poiList = res.result.pois;
        if (poiList.length == 0) {
          wx.showToast({
            title: '没有更多了~',
            icon:'none'
          })
        } else {
          that.setData({
            suggestion: that.data.suggestion.concat(poiList),
            page
          })
          wx.hideLoading({
            complete: (res) => {},
          })
        }

      }
    })
  },
  getList: function (lat, lng) {
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })
    qqmapsdk.reverseGeocoder({
      location: {
        latitude: lat,
        longitude: lng
      }, //坐标
      get_poi: 1, //是否获取坐标对应附近列表
      poi_options: 'policy=2;radius=3000;page_size=10;page_index=1', //poi 参数
      success: function (res) {
        // console.log(res)
        var poiList = res.result.pois;
        for(var i=0;i<poiList.length;i++){
          poiList[i].select = false;
          poiList[0].select = true;
        }
        wx.hideLoading({
          complete: (res) => {},
        })
        that.setData({
          suggestion: poiList,
          value1:poiList[0].title
        })
      }
    })
  },
  onLoad: function (options) {
    var that = this;
    wx.getLocation({
      type: 'gcj02',
      success: (res) => {
        console.log('经纬度为：+++++++++++++++++++++' + res.latitude + '++++++++' + res.longitude)
        var latitude = res.latitude
        var longitude = res.longitude
        that.setData({
          longitude: longitude,
          latitude: latitude
        })
        that.getList(latitude, longitude)
      },
      fail: function (err) {
        console.log(err)
      }
    })
    wx.getSystemInfo({
      success: (res) => {
        this.setData({
          mapHeight: res.windowHeight - 245,
          windowHeight: res.windowHeight
        })
      }
    })
  },
  touchStart: function (e) {
    // console.log(e.touches[0].pageX)
    // console.log(e)
    let sy = e.touches[0].pageY;
    this.data.touchS = [sy]
  },
  move(e) {
    var that = this;
    let sy = e.touches[0].pageY;
    this.data.touchE = [sy];
    this.animation()
  },
  scroll: function (e) {
    // console.log(e)
    var that = this;
    let sy = e.detail.deltaY;
    this.data.touchE = [sy];
    this.animation()
  },
  animation: function () {
    var that = this;
    var animation = wx.createAnimation({
      duration: 500,
      timingFunction: "ease",
      delay: 0,
      transformOrigin: "50% 50%",
    })
    var animatheightadd = wx.createAnimation({
      duration: 500,
      timingFunction: 'ease-in',
    })
    var animaticon = wx.createAnimation({
      duration: 500,
      timingFunction: 'ease-in',
    })
    var animatMap = wx.createAnimation({
      duration: 500,
      timingFunction: 'ease-in',
    })
    if (this.data.touchS[0] < this.data.touchE[0]) {
      animation.rotate(0).step();
      animatheightadd.height(245).step()
      animaticon.height(that.data.posiHeight).step()
      animatMap.height(that.data.windowHeight - 245).step()
      that.setData({
        animatheightadd: animatheightadd.export(),
        animatMap: animatMap.export(),
        animaticon: animaticon.export()
      })
    } else {
      //上拉
      animation.rotate(90).step();
      animatheightadd.height(450).step()
      animaticon.height(that.data.posiHeight-200).step()
      animatMap.height(that.data.windowHeight - 450).step()
      that.setData({
        animatheightadd: animatheightadd.export(),
        animatMap: animatMap.export(),
        animaticon: animaticon.export()
      })
    }
  },
  selected(e) {
    var that = this;
    var city = that.data.suggestion;
    // console.log(city)
    var index = e.currentTarget.dataset.index;
    var lat = '';
    var lng = '';
    var arrress = '';
    for(var i=0;i<city.length;i++){
      city[i].select = false;
      city[index].select = true;
    }
    if (city[index].location != undefined) {
      lat = city[index].location.lat;
      lng = city[index].location.lng;
    } else {
      lat = city[index].latitude;
      lng = city[index].longitude;
      arrress = city[index].addr;
    }
    that.setData({
      value1: city[index].title,
      latitude: lat,
      longitude: lng,
      suggestion:city
    })
  },
  bindregionchange: function (e) {
    //实现大头针移动选点
    var that = this
    if (e.type == "end" && e.causedBy == 'drag') {
      //停止
      var mapCtx = wx.createMapContext("map")
      mapCtx.getCenterLocation({
        success: function (res) {
          // console.log(res)
          var latitude = res.latitude
          var longitude = res.longitude
          that.setData({
            latitude,
            longitude
          })
          that.getList(latitude, longitude)
        }
      })
    } else {
      wx.hideLoading({
        complete: (res) => {},
      })
    }
  },
  input(e) {
    var _this = this;
    //调用关键词提示接口
    if (e.detail.value == '') {
      _this.setData({ //设置suggestion属性，将关键词搜索结果以列表形式展示
        suggestion: [],
      });
      return;
    }
    _this.setData({
      value: e.detail.value
    })
    var location = _this.data.latitude + ',' + _this.data.longitude
    console.log(location)
    qqmapsdk.getSuggestion({
      //获取输入框值并设置keyword参数
      keyword: e.detail.value, //用户输入的关键词，可设置固定值,如keyword:'KFC'
      page_size: 20,
      location: location,
      region_fix: 1,
      success: function (res) { //搜索成功后的回调
        console.log(res);
        _this.setData({ //设置suggestion属性，将关键词搜索结果以列表形式展示
          suggestion: res.data,
        });
      }
    });
  },
  submit:function(){
    var that = this;
    var value = '';
    if(that.data.value==''){
      value = that.data.value1
    }else{
      value = that.data.value
    }
    var data = {
      area_id:wx.getStorageSync('home').id,
      location_name:value,
      longitude:this.data.suggestion[0].location.lng,
      latitude:this.data.suggestion[0].location.lat
    }
    app.request('/pages/applyAddParking', data, 'POST', app.globalData.req).then(res => {
      console.log(res)
      if(res.statusCode==200){
        wx.showModal({
          title: '提交成功',
          content: '感谢您提交的 p 点 （还车点） 的定点还车区域，我们会尽快评估，若您的建议被采纳，将会在地图上显示新的还车点。',
          showCancel: false,//是否显示取消按钮
          confirmText:"我知道了",//默认是“确定”
          confirmColor: '#18D5B9',//确定文字的颜色
          success:function(){
            wx.navigateBack()
          }
        })
      }
    })
  },
  posi: function () {
    //点击定位  
    //这是一段注释
    var that = this;
    wx.getLocation({
      type: 'gcj02',
      success: (res) => {
        // console.log('经纬度为：+++++++++++++++++++++' + res.latitude + '++++++++' + res.longitude)
        var latitude = res.latitude
        var longitude = res.longitude
        that.setData({
          longitude: longitude,
          latitude: latitude
        })
        that.getList(latitude, longitude)
      }
    })
  },
  onReady: function () {
    var that = this;
    var query = wx.createSelectorQuery();
    query.select('.circle').boundingClientRect();
    query.exec(function (res) {
      console.log(res)
      that.setData({
        posiHeight: res[0].height
      })
    })
  },
  onShow: function () {

  },
  onHide: function () {

  },
  onUnload: function () {

  },
  onPullDownRefresh: function () {

  },
  onReachBottom: function () {

  },
  onShareAppMessage: function () {

  }
})