// pages/coupon/coupon.js
const util = require('../../utils/util.js');
const api = require('../../config/api.js');


Page({

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

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    getApp().checkNetStatu();
    this.setData({
      datas: [],
      enableCount: 0,
      disableCount: 0
    })
  },

  gotoInfo(ev) {
    let item = ev.target.dataset.item
    wx.navigateTo({
      url: '/pages/coupon/info/info?id=' + item.id,
    })
  },

  async getCouponList() {
    this.setData({loadding: true})
    let { data } = await util.request(api.CouponList + '?pageSize=100000&pageNum=1')
    let coupons = data.map(item => {
      return {
        ...item,
        used: true
        // used: !(item.couponStatus == 3 || item.useStatus == 3 || item.useStatus == 4)
      }
    })
    
    let { data : data1 } = await util.request(api.CouponNoUsedList + '?pageSize=100000&pageNum=1')
    coupons = coupons.concat(
      data1.list.map(item => {
        return {
          ...item,
          used: false
          // used: !(item.couponStatus == 3 || item.useStatus == 3 || item.useStatus == 4)
        }
      })
    )
    this.setData({
      datas: coupons,
      enableCount: coupons.filter(({used}) => used).length,
      disableCount: coupons.filter(({used}) => !used).length,
      loadding: false
    })
  },

  enableHandle() {
    this.setData({showUsedTab: true})
  },

  disableHandle() {
    this.setData({showUsedTab: false})
  },

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

  },

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

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

  },

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

  },

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

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

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

  }
})