//index.js
let currentPage = 0 // 当前第几页,0代表第一页 
let pageSize = 6
var util = require('../../utils/util.js')
var app = getApp()
Page({
  data: {
    search:'',
    re:[],
    feed: [], 
    feed_length: 0,
    loadMore: false, //"上拉加载"的变量，默认false，隐藏  
    loadAll: false //“没有数据”的变量，默认false，隐藏 
    //imgList:[]
  },
  //事件处理函数
bindItemTap: function(event) {
    /*wx.navigateTo({
      url: '../answer/answer'
    })*/
    var aid=event.currentTarget.dataset.aid;
    console.log(aid)
    //console.log("1")
  wx.navigateTo({
    url: '../answer/answer?aid='+aid,//要跳转到的页面路径
})  

  },

onLoad: function () {
    console.log('onLoad')
    this.clearCache();
    let that = this
    //调用应用实例的方法获取全局数据
    this.getData();
    this.refresh()
  },
onShow: function (){
  console.log('onshow')
  this.upper()
},
 
  upper: function () {//下滑刷新
    wx.showNavigationBarLoading()
    this.refresh();
    console.log("upper");
    setTimeout(function(){wx.hideNavigationBarLoading();wx.stopPullDownRefresh();}, 1000);
  },
  lower: function (e) {//触底加载
    wx.showNavigationBarLoading();
    var that = this;
    setTimeout(function(){wx.hideNavigationBarLoading();that.nextLoad();}, 1000);
    console.log("lower")
  },
  scroll: function (e) {
    console.log("scroll")
  },

  //网络请求数据, 实现首页刷新
  refresh0: function(){
    this.getData;
    /*var index_api = '';
    util.getData(index_api)
        .then(function(data){
          //this.setData({
          //
          //});
          console.log(data);
        });*/
  },

  //使用本地 fake 数据实现刷新效果
  getData() {
    let that = this;
    //第一次加载数据
    if (currentPage == 1) {
      this.setData({
        loadMore: true, //把"上拉加载"的变量设为true，显示  
        loadAll: false //把“没有数据”设为false，隐藏  
      })
    }
    //云数据的请求
    wx.cloud.database().collection("tiezi")
    .orderBy('createTime', 'desc') //按发布视频排序
      .skip(currentPage * pageSize) //从第几个数据开始
      .limit(pageSize)
      .get({
        success(res) {
          if (res.data && res.data.length > 0) {
            console.log("请求成功", res.data)
            currentPage++
            //把新请求到的数据添加到dataList里  
            let list = that.data.feed.concat(res.data)
            that.setData({
              feed: list, //获取数据数组    
              loadMore: false //把"上拉加载"的变量设为false，显示  
            });
            if (res.data.length < pageSize) {
              that.setData({
                loadMore: false, //隐藏加载中。。
                loadAll: true //所有数据都加载完了
              });
            }
          } else {
            that.setData({
              loadAll: true, //把“没有数据”设为true，显示  
              loadMore: false //把"上拉加载"的变量设为false，隐藏  
            });
          }
        },
        fail(res) {
          console.log("请求失败", res)
          that.setData({
            loadAll: false,
            loadMore: false
          });
        }
      })
  },
  refresh: function(){
   
    this.clearCache();
    this.getData()
    let that = this
    if (!that.data.loadMore) {
      that.setData({
        loadMore: true, //加载中  
        loadAll: false //是否加载完所有数据
      });}

  },

  //使用本地 fake 数据实现继续加载效果
  nextLoad: function(){
    console.log("上拉触底事件")
    
    let that = this
    if (!that.data.loadMore) {
      that.setData({
        loadMore: true, //加载中  
        loadAll: false //是否加载完所有数据
      });

      //加载更多，这里做下延时加载
      setTimeout(function() {
        that.getData()
      }, 500)
    }
   
  },
  goto(){
    
    wx.navigateTo({
      url: '../submit/submit',//要跳转到的页面路径
})  

  },
// 清缓存
clearCache:function(){
  currentPage = 0;//分页标识归零
  this.setData({
     feed: [] //文章列表数组清空
  }); 
},
 



GetSearchInput: function(e) {
  this.setData({
      search: e.detail.value
  })

},
ToSearch: function(e) {
  //let search = e.detail.value;
  var that = this;
 
  if(this.data.search == '') {
      wx.showToast({
        title: '请输入',
        icon: 'none'
      })
      return
  }

  wx.showLoading({
    title: '搜索中',
  })
  const _ = wx.cloud.database().command
  wx.cloud.database().collection('tiezi').where(_.or([
  {
    content: wx.cloud.database().RegExp({
      regexp: this.data.search,
      options: 'i',
  }),
  },
  {
    title: wx.cloud.database().RegExp({
      regexp: this.data.search,
      options: 'i',
  }),
  }
]))

//   wx.cloud.database().collection('tiezi').where({
//     content: wx.cloud.database().RegExp({
//         regexp: this.data.search,
//         options: 'i',
//     }),
    
// })
.get()
.then(res => {
    if (res.data.length != 0) {
        this.setData({
            re: res.data,
        })
        wx.setStorageSync('re', res.data)
        let re= wx.getStorageSync('re')
        console.log(re)
        let that = this;
        wx.hideLoading({
          success: (res) => {
            that.setData({
              search: '',
            })
           }})
        wx.navigateTo({
          url: '../searchShow/searchShow?re='+JSON.stringify(re),//要跳转到的页面路径
          
    })  
    
    
    } else {
        wx.showToast({
            title: '未找到',
            icon: 'none'
        })
    }
    console.log(res.data)
    
    
})
.catch(res => {
  console.log("查询失败",res)
})
},


})

