// pages/coupons.js
const app = getApp()
const wxService = require('../../utils/wxService')
const utils = require('../../utils/util')
const envInfo = require('../../config/index').envInfo

wxService.page({
  /**
   * 页面的初始数据
   */
  data: {
    nav:[
      {
        type:'1',
        name:'可使用'
      },{
        type:'2',
        name:'已失效'
      }
    ],
    pageNo: 1,
    pageSize: 20,
    invalidPageNo: 1,
    totalPages: 0,
    totalElements: 0,
    invalidTotalElements: 0,
    couponList: [],
    invalidCouponList: [],
    invalidTotalPages: 0,
    invalidTotalElements: 0,
    currentTab: 0,
    noMoreCoupon: false,
    noMoreCouponInvalid: false
  },
    //滑动切换
    swiperTab: function (e) {
      const {current} =  e.detail
      if (this.data.currentTab == current) {
        return false;
      } else {
        this.setData({
          currentTab: current
        })
      }
       // 可使用
      if( current == 0 ){
        this.getCouponList(1,20,1)
      }else if( current == 1){
        // 失效
        this.getCouponList(1,20,0)
      }
    },
    // 点击切换
    clickTab: function (e) {
      const {current} = e.target.dataset
      if (this.data.currentTab == current) {
        return false;
      } else {
        this.setData({
          currentTab: current,
          // couponList: [],  // 清空数据
          // invalidCouponList: [],
        })
      }
       // 可使用
      if( current == 0 ){
        this.getCouponList(1,20,1)
      }else if( current == 1){
        // 失效
        this.getCouponList(1,20,0)
      }
    },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad () {
    wx.hideShareMenu();
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow () {
    // 可使用 已失效
    this.getCouponList(1,20,1)
    this.getCouponList(1,20,0)
  },
  handelPresentCouponDetail(e) {
    let {cardno} = e.currentTarget.dataset
    wxService.router(`/pages/couponDetail/couponDetail`).search({cardno})
  },
  handelPresentCouponCode(e) {
    let {cardno} = e.currentTarget.dataset
    wxService.router(`/pages/couponCodeDetail/couponCodeDetail`).search({cardno})
  },

  //获取优惠券列表
  getCouponList(pageNo,pageSize,statusNum){
    wx.showLoading({
      title: '加载中'
    })
    let params;
    if(statusNum == 1){
      params = {
        status: 1
      }
      if(pageNo == 1){
        this.setData({
          pageNo: 1,
          couponList: [],  // 清空数据
        })
      }
    } else {
      params = {
        statusNot: 1
      }
      if(pageNo == 1){
        this.setData({
          invalidCouponList: [],
          invalidPageNo: 1
        })
      }
    }

    wxService.post(`/coupon/coupon/listWithMember4Mina?number=${pageNo}&size=${pageSize}`, params).then(res => {
      if(!res) return false
      const {result,data} = res.data
      if(result == 0){
         wx.hideLoading()
         // 处理时间显示
         if(!data.content) return false
          data.content.forEach(item=> {
           item.startTime = item.startTime.substring(0,10)
           item.endTime = item.endTime.substring(0,10)
          })
          // 可使用 or 已失效
         if(statusNum == 1) {
           // couponList
          this.setData({
            couponList: this.data.pageNo == 1? [...data.content]: [...this.data.couponList,...data.content],
            totalPages: data.totalPages,
            totalElements: data.totalElements
           })
         } else {
           // invalidCouponList
          this.setData({
            invalidCouponList: this.data.invalidPageNo == 1? [...data.content]: [...this.data.invalidCouponList,...data.content],
            invalidTotalPages: data.totalPages,
            // invalidTotalElements: data.totalElements > 20 ? 20 : data.totalElements,
            invalidTotalElements: data.totalElements,
           })
         }
      }

    }).finally(() => {
      wx.hideLoading();
    })
  },
  /**
 * 页面相关事件处理函数--监听用户下拉动作
 */
  //scoll view 触到底部
  onBottom() {
    const { currentTab, pageNo, pageSize, totalPages, invalidPageNo, invalidTotalPages } = this.data
    if (currentTab == 0) {
      if (pageNo < totalPages) {
        this.setData({
          pageNo: pageNo + 1,
        })
        // status 1
        this.getCouponList(this.data.pageNo, pageSize, 1)
      } else {
        this.setData({
          noMoreCoupon: true
        })
      }
    } else {
      this.setData({
        noMoreCouponInvalid: true
      })
    }
  },

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