// packageC/hotel/hotelCoupon/hotelCoupon.js
var app = getApp();
Page({

  /**
	 * 页面的初始数据
	 */
  data: {
    language: '',
    loading: false,
    allLoaded: false,
    goload: true,
    isLoadMore: true,
    page: 1,
    total_page: 0,
    cupconList: [],
    COUPON_URL: "plugin.hotel.frontend.hotel.couponList.index",
    GET_COUPIN: "plugin.hotel.frontend.hotel.couponGet.index",
    id: ''
  },

  /**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function(options) {
    if (options.id) {
      this.setData({
        id: options.id
      });
    }
    this.initData();
    this.getData();
  },

  /**
	 * 生命周期函数--监听页面初次渲染完成
	 */
  onReady: function() {
    let language = wx.getStorageSync('langIndex');
    this.setData({ 'language': language.en});
  },

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

  },

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

  },

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

  },

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

  },

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

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

  },
  initData() {
    this.setData({
      loading: false,
      allLoaded: false,
      goload: true,
      isLoadMore: true,
      page: 1,
      total_page: 0,
      cupconList: []
    });
  },
  getData() {
    let that = this;
    let urlStr = app.getNetAddresss(that.data.COUPON_URL);
    app._getNetWork({
      url: urlStr,
      data: {
        hotel_id: this.data.id,
        page: this.data.page
      },
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            cupconList: res.data.data,
            total_page: res.data.last_page
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  getMoreData() {
    let that = this;
    let urlStr = app.getNetAddresss(that.data.COUPON_URL);
    if (this.data.page == this.data.total_page) {
      return;
    }
    if (this.data.page >= this.data.total_page) {
      this.setData({
        loading: true,
        allLoaded: true
      });
      return;
    } else {
      this.setData({
        page: this.data.page + 1
      });
      app._getNetWork({
        url: urlStr,
        data: {
          hotel_id: this.data.id,
          page: this.data.page
        },
        success: function(resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            that.setData({
              cupconList: that.data.cupconList.concat(res.data.data),
              loading: false,
              allLoaded: false
            });
          } else {
            that.setData({
              page: that.data.page - 1,
              loading: true,
              allLoaded: true,
              isLoadMore: false
            });
          }
        },
        fail: function(res) {
          console.log(res);
        }
      });
    }
  },
  //领取优惠券
  getCoupon(e) {
    let id = e.currentTarget.dataset.id;
    let index = e.currentTarget.dataset.index;
    let that = this;
    let urlStr = app.getNetAddresss(that.data.GET_COUPIN);
    app._getNetWork({
      url: urlStr,
      data: {
        coupon_id: id
      },
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            ['cupconList[' + index + ']']: res.data
          });
          wx.showToast({
            title: '领取成功',
            icon: 'none',
            duration: 1500
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 1500
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  }
});
