// packageD/couponcode/couponcode.js
var app = getApp();
Page({

  /**
	 * 页面的初始数据
	 */
  data: {
    id: '',
    status: 0,
    getNum: 0,
    waitNum: 0,
    getListData_1: [],
    getListData_2: [],
    getListData_3: [],
    codeimg: '',
    //more
    isLoadMore: true,
    page: 1,
    total_page: 0,
  },

  /**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function(options) {
    if (options.id) {
      this.setData({
        id: options.id
      });
    }
    this.getDetail();
  },
  getDetail() {
    let that = this;
    let urlStr = app.getNetAddresss("plugin.coupon-qr.api.index.getDetail");
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: {
        status: this.data.status,
        qr_id: this.data.id
      },
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            getNum: res.data.getNum,
            waitNum: res.data.waitNum,
            total_page: res.data.last_page
          });
          if (that.data.status == 1) {
            that.setData({
              ['getListData_2[0]']: res.data.data
            });
          } else if (that.data.status == 2) {
            that.setData({
              ['getListData_3[0]']: res.data.data
            });
          } else {
            that.setData({
              ['getListData_1[0]']: res.data.data
            });
          }
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }

      },
      fail: function(res) {}
    });
  },
  _getMoreData() {
    var that = this;
    let urlStr = app.getNetAddresss("plugin.coupon-qr.api.index.getDetail");
    if (this.data.page >= this.data.total_page) {
      that.setData({
        isLoadMore: false
      });
      return;
    } else {
      that.setData({
        page: that.data.page + 1
      });
      app._postNetWork({
        url: urlStr,
        showToastIn: false,
        data: {
          page: this.data.page,
          status: this.data.status,
          qr_id: this.data.id
        },
        success: function(resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            if (that.data.status == 1) {
              that.setData({
                ['getListData_2[' + that.data.getListData_2.length + ']']: res.data.data
              });
            } else if (that.data.status == 2) {
              that.setData({
                ['getListData_3[' + that.data.getListData_3.length + ']']: res.data.data
              });
            } else {
              that.setData({
                ['getListData_1[' + that.data.getListData_1.length + ']']: res.data.data
              });
            }
          } else {
            that.setData({
              page: that.data.page - 1,
              isLoadMore: false
            });
            return;
          }

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

  },

  /**
	 * 生命周期函数--监听页面显示
	 */
  onShow: function() {

  },

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

  },

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

  },

  /**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
  onPullDownRefresh: function() {

  },

  /**
	 * 页面上拉触底事件的处理函数
	 */
  onReachBottom: function() {
    if (this.data.isLoadMore) {
      this._getMoreData();
    } else {
      console.log('没有更多数据');
    }
  },

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

  },
  chooseTabchange(e) {
    if (e.detail.index == 1) {
      this.setData({
        status: 1
      });
    } else if (e.detail.index == 2) {
      this.setData({
        status: 2
      });
    } else {
      this.setData({
        status: 0
      });
    }
    this.setData({
      isLoadMore: true,
      page: 1,
      total_page: 0,
      getListData_1: [],
      getListData_2: [],
      getListData_3: []
    });
    this.getDetail();
  },
  gosmall_qr(e) {
    let smallqr = e.currentTarget.dataset.smallqr;
    this.setData({
      codeimg: smallqr,
      showcode: true
    });
  },
  showcodecloseBtn() {
    this.setData({
      showcode: false
    });
  }
});
