//logs.js
let util = require('../../utils/util.js');
let wechat = require("../../utils/wechat");
let amap = require("../../utils/amap");
Page({
  data: {
    lonlat: "",
    city: "",
    longitude: null,
    latitude: null,
    tips: [],
    searchString: null
  },
  onLoad(e) {
    let {
      lonlat,
      city
    } = e;
    var longitude = lonlat.split(",")[0];
    var latitude = lonlat.split(",")[1];
    console.log({
      lonlat,
      longitude,
      latitude,
      city
    });
    this.setData({
      lonlat,
      longitude,
      latitude,
      city
    })
  },
  bindInput2(e) {

    let {
      value
    } = e.detail;
    let {
      lonlat,
      city
    } = this.data;
    amap.getInputtips(city, lonlat, value)
      .then(d => {
        //console.log(d);
        if (d && d.tips) {
          this.setData({
            tips: d.tips
          });
        }
      })
      .catch(e => {
        console.log(e);
      })
  },
  bindInput(e) {
    this.setData({
      searchString: e.detail.value
    });
  },
  bindSearch2(e) {
    let {
      keywords
    } = e.currentTarget.dataset;
    let pages = getCurrentPages();
    let prevPage = pages[0]; //首页
    //console.log(pages);
    //console.log(e);
    console.log(keywords);
    if (keywords) {
      prevPage.setData({
        keywords
      });
      console.log(keywords.name, keywords.location);
      //amap.getPoiAround(keywords.name, keywords.location)
      //.then(d => {
      //  console.log(d);
      //  let {
      //    markers
      //  } = d;
      //  markers.forEach((item, index) => {
      //    item.callout = {}; //marker绑定点击事件后，导致气泡一闪一闪，不想显示
      //    item.iconPath = "/images/marker.png";
      //    item.width = '88rpx';
      //    item.height = '112rpx';
      //    item.address = d.poisData[index].cityname + d.poisData[index].adname + d.poisData[index].address;
      //  })
      //  prevPage.setData({
      //    markers
      //  });
      //  prevPage.showMarkerInfo(markers[0]);
      //  prevPage.changeMarkerColor(0);
      //})
      //.catch(e => {
      //  console.log(e);
      //})
    }
    //let url = `/pages/index/index`;
    //wx.navigateBack({
    //  url
    // });
    let {
      city,
      longitude,
      latitude
    } = this.data;
    let url = `/pages/search_result/search_result?city=${city}&longitude=${longitude}&latitude=${latitude}&name=${keywords.name}&location=${keywords.location}`;
    wx.navigateTo({
      url
    });
  },
  bindSearch(e) {
    let {
      searchString: keywords
    } = this.data;
    let pages = getCurrentPages();
    let prevPage = pages[0]; //首页
    if (keywords) {
      prevPage.setData({
        keywords
      });
      let {
        city,
        longitude,
        latitude
      } = this.data;
      let url = `/pages/search_result/search_result?city=${city}&longitude=${longitude}&latitude=${latitude}&searchString=${keywords}`;
      wx.navigateTo({
        url
      });
    }
  },
  cancel(e) {
    console.log(e);
    wx.navigateBack();
  }
});