//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: '',
    keywords: '',
    pagesize:30,
    pagenum:1,
    total:-1,
    loadTotal: 0,
    dataLoading: false,
    finishedLoadTap: 0,
    finishedLoadShowTimes: 0,
    chargList: [],
  },
  onLoad(e) {
    this.getNextPage()
  },

  bindScrollTolowerEvent: function(){
    this.getNextPage();
  },
  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;
    //console.log(pages);
    //console.log(e);
    // console.log(keywords);
    if (keywords) {
      //------------新增code 解决异步请求数据回显问题----------------------------
      let api = getApp().globalData.postHeadAgreement
      let path = 'restapi/wechat/getuserstation'
      let params = keywords.id
      console.log(params);
      //调用微信封装的Promise  携带的参数根据需求写为json{} 还是Long类型
      wechat.request(api,path,params)
      .then(response=>{
        let userInfo = wx.getStorageSync('userInfo');
        console.log("用户");
        console.log(userInfo);
        let pages = getCurrentPages();
        let prevPage = pages[pages.length - 2]; //上一个页面
        let worksheetinfo = prevPage.data.worksheetinfo;
        let brandName = keywords.brandName;
        let chargStationName = keywords.chargStationName
        worksheetinfo.chargStationId =  keywords.id;
        worksheetinfo.mainLocation =  keywords.address;
        worksheetinfo.accendant = '';

        //处理数据
        let {
           data: items
        } = response;
              items.forEach((item, index) => {
                if (item.userName.includes(userInfo.userName)){
                  worksheetinfo.accendant =userInfo.userName
                }
              });
              worksheetinfo.pams =  [];
              prevPage.setData({
                brandName,
                chargStationName,
                chargStationNameClearFlag:false,
                worksheetinfo,
                accendantids:[userInfo.userId],
              });
              console.log("异步慢");
              console.log(prevPage.data.worksheetinfo);
              wx.navigateBack({
                delta: 1
              });
      })
      .catch(error=>{
        console.log("请求失败");
        wx.navigateBack({//返回上一个页面
          delta: 1
        });
      });
    }
    //let url = `/pages/index/index`;
    //wx.navigateBack({
    //  url
    // });
  },
  bindSearch(e) {
    let {
      searchString: keywords
    } = this.data;
    console.info(keywords)
    this.setData({
      keywords,
      pagenum:1,
      chargList: []
    })

    console.log(this.data);
    console.log("7898");
    this.getNextPage();

  },
  cancel(e) {
    console.log(e);
    wx.navigateBack();
  },
  getNextPage(){
    let userInfo = wx.getStorageSync('userInfo');
    let isLogin = wx.getStorageSync('isLogin');
    //必须排除返回结果值为空的情况，负责一直搜索为空
    if(this.data.chargList.length == this.data.total && this.data.chargList.length != 0){
      this.setData({
        finishedLoadTap:this.data.finishedLoadTap+1
      })
      if(this.data.finishedLoadTap>0 && this.data.finishedLoadShowTimes==0){
        this.setData({
          finishedLoadShowTimes:this.data.finishedLoadShowTimes+1
        })
        wx.showToast({
          title: '全部加载完毕',
          icon: 'success',
          duration: 2000
        })
      }
      return
    }

    if(this.data.dataLoading){
      return
    }
    this.setData({
      dataLoading: true
    })
    wx.showLoading({
      title: '数据加载中....',
    })

  let that = this;
  if (userInfo && isLogin) {


    wx.request({
      url: getApp().globalData.postHeadAgreement + '/restapi/wechat/userChargStationsWeb',
      data: {
        userId: userInfo.userId,
        searchString:that.data.keywords,
        pagenum: this.data.pagenum++,
        pagesize: this.data.pagesize,
      },
      method: 'POST',
      success(res) {
        let chargList = res.data.rows;

        // console.log(chargList);
        // that.rechargeTimeOrder(chargemoneylogs);
        wx.hideLoading()
        that.setData({
          chargList: that.data.chargList.concat(chargList),
          total: res.data.total,
          dataLoading: false,
        });
      }
    });

  }


  },
});