var app = getApp();
var page = 1;
var flag = 0;
var size = 10;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: []
  },

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

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    app.ajaxReadyCallback = res => {
      console.log(res);
      var oldlist = that.data.list;
      var list = res.data.data;
      if (page == 1) {
        oldlist = [];
      }
      var newlist = oldlist.concat(list);
      console.log(newlist);
      that.setData({
        list: newlist
      });

      if (list.length < size) {
        flag = 1;
      } else {
        page++;
      }
    }
    page = 1;
    flag = 0;
    this.showData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

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

  },

  onPullDownRefresh() { //下拉刷新
    page = 1;
    flag = 0;
    this.showData();
  },

  onReachBottom() { //上拉加载
    this.showData();
  },

  showData:function(){
    if(flag == 1){
      return false;
    }
    app.ajax({
      url: app.globalData.serverUrl + 'visitorOrder/apply',
      type: 'POST',
      data: {
        page: page,
        size: size
      }
    });
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var userId = app.globalData.userinfo.userId;
    var orgId = wx.getStorageSync("orgId")?wx.getStorageSync("orgId"):0;
    let r = "/pages/splash/splash?type=visitorAdd&userId="+userId+"&orgId="+orgId;
    return {
      title:'访客预约单',
      imageUrl: '/images/zi.png',
      path: r
    }
  },
  goDetail(event){
    var id = event.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/visitor/apply/detail/index?id=' + id
    })
  },
})