// pages/s-task/s-task.js
var myRequest = require("../../utils/api.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    icons: [,
      'chevron-right',
    ],
    siteData: [],
    projectId: '',
    page: 1,
    size: 5,
    name: '',
    isFromSearch: false,
    searchLoading: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

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

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var projectId = wx.getStorageSync('projectId');
    this.setData({
      projectId: projectId,
      page: 1,
      siteData: [],
    })
    this.getSiteData()
  },
  getSiteData() {
    var that = this;
    console.log(123);
    console.log(that.data.name);
    myRequest.myRequest('device/list', {
      projectId: that.data.projectId,
      page: that.data.page,
      size: that.data.size,
      name: that.data.name,
    }, 'get').then(function(res){
      var siteList = res.data.data.items;
      // var searchLoading = that.data.searchLoading;
      if (siteList.length != 0 ){
        var list = [];
        //如果isFromSearch是true从data中取出数据，否则先从原来的数据继续添加 
        // that.data.isFromSearch ? list=siteList : list=that.data.siteData.concat(siteList) 
        list = that.data.siteData.concat(siteList)
        that.setData({
          siteData: list, //获取数据数组 
        });
      }
      that.setData({ 
        searchLoadingComplete: true, //把“没有数据”设为true，显示 
        searchLoading: false //把"上拉加载"的变量设为false，隐藏 
       });
    }).catch(function(res){
      console.log(res);
    })
  },
  getName(e) {
    var name = e.detail.value;
    this.setData({
      name: name,
      siteData: [],
      page: 1,
      isFromSearch: false,
      searchLoading: true
    })
    this.getSiteData();
  },
  details(e) {
    var id = e.currentTarget.dataset.id;
    var code = e.currentTarget.dataset.code;
    wx.navigateTo({
      url: '../p-task/p-task?deviceId=' + id + '&code=' + code,
    })
  },
  navigation(e) {
    let latitude = e.currentTarget.dataset.latitude;
    let longitude = e.currentTarget.dataset.longitude;
    let name = e.currentTarget.dataset.name;
    wx.openLocation({
      latitude: latitude,
      longitude: longitude,
      name: name,
      address: name,
      scale: 28
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.setData({
      siteData: [],
      page: 1,
    })
  },

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

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      siteData: [],
      page: 1,
      isFromSearch: false,
      searchLoading: true
    })
    this.getSiteData()
  },

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

    this.setData({
      page: Number(this.data.page)+1,
      isFromSearch: false
    });
    console.log(this.data.page)
    this.getSiteData()
  },

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