// pages/myCoupon/myCoupon.js
var http = require("../../utils/http.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: 1,
    couponList: [],
    unUseCount: 0,
    useCount: 0,
    expiredCount: 0,
    pages: 0,
    current: 1,
    size: 10
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.couponCount()
    this.loadMyCouponData(1)
  },

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

  },

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

  },

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

  },

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

  },

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

    if (this.data.current < this.data.pages) {
      this.loadOrderData(this.data.sts, this.data.current + 1);
    }

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    if (this.data.current < this.data.pages) {
      this.setData({
        current: this.data.current + 1
      })
      this.loadMyCouponData(this.data.type)
    }

  },

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

  },

  /**
   * 获取我的优惠券列表
   */
  loadMyCouponData(type) {
    // 我的优惠券状态(0:过期 1:可用 2：已用)
    let status;
    if (type == 1) {
      status = 2
    } else if (type == 0) {
      status = 1
    } else if (type == 2) {
      status = 0
    }
    var params = {
      url: "/p/myCoupon/getCouponList",
      method: "GET",
      data: {
        status: status,
        current: this.data.current,
        size: this.data.size
      },
      callBack: (res) => {
        let list = []
        if (res.current == 1) {
          list = res.records
        } else {
          list = this.data.couponList
          list = list.concat(res.records)
        }
        this.setData({
          couponList: list,
          pages: res.pages
        })

        if (status == 0) {
          // 已过期数量
          this.setData({
            expiredCount: res.total
          });
        } else if (status == 1) {
          // 可用数量
          this.setData({
            unUseCount: res.total
          });

        } else if (status == 2) {
          // 使用过总数量
          this.setData({
            useCount: res.total
          });
        }
      }
    };
    http.request(params);
  },

  /**
   *  标签切换事件
   */
  changeTab(e) {
    this.setData({
      current: 1,
      type: e.currentTarget.dataset.type,
      couponList: []
    });
    this.loadMyCouponData(this.data.type)
  },

  /**
   *  获取各个状态下优惠券数量
   */
  couponCount() {
    var ths = this
    var params = {
      url: "/p/myCoupon/getMyCouponsStatusCount",
      method: "GET",
      data: {},
      callBack: function(res) {
        ths.setData({
          unUseCount: res.unUseCount,
          expiredCount: res.expiredCount,
          useCount: res.useCount,
        })
      }
    }
    http.request(params);
  },

  /**
   * 删除优惠券
   */
  delCouponHandle(e) {
    let couponUserId = e.currentTarget.dataset.couponuserid
    let ths = this
    wx.showModal({
      title: '',
      content: '确定要删除此优惠券吗？',
      confirmColor: "#eb2444",
      success(res) {
        if (res.confirm) {
          wx.showLoading();
          let params = {
            url: "/p/myCoupon/delCoupon/" + couponUserId,
            method: "DELETE",
            data: {},
            callBack: function(res) {
              wx.hideLoading();
              ths.setData({
                current: 1,
                pages: 0
              })
              wx.showToast({
                title: res,
                duration: 1000
              })
              setTimeout(() => {
                ths.loadMyCouponData(ths.data.type)
              },1000)
            }
          }
          http.request(params);
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  }
})