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

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

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
  },
  selectType(e){
    this.data.list = [];
    this.setData({
      type: parseInt(e.currentTarget.dataset.id)
    });
    page = 1;
    flag = 0;
    this.showData();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  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();
  },
    handleAgree(e){
        var that = this;
        var type = Number(e.currentTarget.dataset.type);
        var id = Number(e.currentTarget.dataset.id);
        let msg = "";
        let s =  0;
        if(type == 1){
            msg = '确定要同意此条申请吗？';
            s = 1;
        }else if(type==2){
            msg = '确定要拒绝此条申请吗？';
            s = 4;
        }else if (type==3){
            msg = '确定取消此条预约吗？';
            s = 5;
        }

        wx.showModal({
            title: '提示',
            content: msg,
            success (res) {
                if (res.confirm) {
                    console.log('用户点击确定')

                    app.ajax2({
                        url: app.globalData.visitorServerUrl + 'visitorOrder/deal',
                        type: 'POST',
                        apiname: 'deal',
                        data: {
                            id: id,
                            s: s
                        }
                    });
                } else if (res.cancel) {
                    console.log('用户点击取消')
                }
            }
        })

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

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

  },
  goDetail(event){
    var id = event.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/visitor/detail/index?id=' + id
    })
  },
})
