// pages/store_coupon/store_coupon.js
var app = getApp();
const COUPON_URL = "plugin.store-cashier.frontend.store.couponList.index";
const GET_COUPIN = "plugin.store-cashier.frontend.store.couponGet.index";
Page({

  /**
	 * 页面的初始数据
	 */
  data: {
    language: '',
    loading: false,
    allLoaded: false,
    goload: true,
    isLoadMore: true,
    page: 1,
    total_page: 0,
    cupconList: []
  },

  /**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function(options) {
    if (options.id) {
      this.setData({
        store_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();
    } 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(COUPON_URL);
    urlStr += '&store_id=' + this.data.store_id;
    urlStr += '&page=' + this.data.page;
    app._getNetWork({
      url: urlStr,
      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;
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      that.setData({
        page: this.data.page + 1
      });
      let urlStr = app.getNetAddresss(COUPON_URL);
      urlStr += '&store_id=' + this.data.store_id;
      urlStr += '&page=' + this.data.page;
      app._getNetWork({
        url: urlStr,
        success: function(resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            let myData = res.data.data;
            if (myData.length > 0) {
              let cupconList = that.data.cupconList;
              that.setData({
                cupconList:	cupconList.concat(myData)
              });
            } else {
              that.setData({
                isLoadMore: false
              });
            }
          } else {
            that.setData({
              page: that.data.page - 1,
              isLoadMore: false
            });
          }
        },
        fail: function(res) {
          console.log(res);
        }
      });
    }
  },
  //领取优惠券
  getCoupon(e) {
    let that = this;
    let id = e.currentTarget.dataset.id;
    let index = e.currentTarget.dataset.index;
    let urlStr = app.getNetAddresss(GET_COUPIN);
    urlStr += '&coupon_id=' + id;
    app._getNetWork({
      url: urlStr,
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            ['cupconList[' + index + ']']: res.data
          });
          wx.showToast({
            icon: 'none',
            title: '领取成功',
            duration: 1500
          });
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  }
});
