// 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: {
    imageData: [
      {
        name: '大脸猫爱吃鱼大脸猫爱吃鱼大脸猫爱吃鱼大脸猫爱吃鱼大脸猫爱吃鱼',
        heart_num: '1',
        title: '你所不知道的红酒知识你所不知道的红酒知识你所不知道的红酒知识你所不知道的红酒知识你所不知道的红酒知识',
        url: 'http://f10.baidu.com/it/u=121654667,1482133440&fm=72',
        avatar: 'http://img4.imgtn.bdimg.com/it/u=349345436,3394162868&fm=26&gp=0.jpg'
      },
      {
        name: '大脸猫爱吃鱼',
        heart_num: '2',
        title: '你所不知道的红酒知识你所不知道的红酒知识你所不知道的红酒知识你所不知道的红酒知识你所不知道的红酒知识',
        url: 'http://img3.imgtn.bdimg.com/it/u=1417732605,3777474040&fm=26&gp=0.jpg',
        avatar: 'http://img4.imgtn.bdimg.com/it/u=349345436,3394162868&fm=26&gp=0.jpg'
      },
      {
        name: '大脸猫爱吃鱼',
        heart_num: '3',
        title: '你所不知道的红酒知识你所不知道的红酒知识你所不知道的红酒知识你所不知道的红酒知识你所不知道的红酒知识',
        url: 'http://img3.imgtn.bdimg.com/it/u=1417732605,3777474040&fm=26&gp=0.jpg',
        avatar: 'http://img4.imgtn.bdimg.com/it/u=349345436,3394162868&fm=26&gp=0.jpg'
      }, {
        name: '大脸猫爱吃鱼',
        heart_num: '4',
        title: '你所不知道的红酒知识你所不知道的红酒知识你所不知道的红酒知识你所不知道的红酒知识你所不知道的红酒知识',
        url: 'http://f10.baidu.com/it/u=121654667,1482133440&fm=72',
        avatar: 'http://img4.imgtn.bdimg.com/it/u=349345436,3394162868&fm=26&gp=0.jpg'
      },
      {
        name: '大脸猫爱吃鱼',
        heart_num: '5',
        title: '你所不知道的红酒知识你所不知道的红酒知识你所不知道的红酒知识你所不知道的红酒知识你所不知道的红酒知识',
        url: 'http://f10.baidu.com/it/u=121654667,1482133440&fm=72',
        avatar: 'http://img4.imgtn.bdimg.com/it/u=349345436,3394162868&fm=26&gp=0.jpg'
      },
      {
        name: '大脸猫爱吃鱼',
        heart_num: '6',
        title: '你所不知道的红酒知识你所不知道的红酒知识你所不知道的红酒知识你所不知道的红酒知识你所不知道的红酒知识',
        url: 'http://img3.imgtn.bdimg.com/it/u=1417732605,3777474040&fm=26&gp=0.jpg',
        avatar: 'http://img4.imgtn.bdimg.com/it/u=349345436,3394162868&fm=26&gp=0.jpg'
      },
      {
        name: '大脸猫爱吃鱼',
        heart_num: '7',
        title: '你所不知道的红酒知识你所不知道的红酒知识你所不知道的红酒知识你所不知道的红酒知识你所不知道的红酒知识',
        url: 'http://img4.imgtn.bdimg.com/it/u=2748975304,2710656664&fm=26&gp=0.jpg',
        avatar: 'http://img4.imgtn.bdimg.com/it/u=349345436,3394162868&fm=26&gp=0.jpg'
      }, {
        name: '大脸猫爱吃鱼',
        heart_num: '8',
        title: '你所不知道的红酒知识你所不知道的红酒知识你所不知道的红酒知识你所不知道的红酒知识你所不知道的红酒知识',
        url: 'http://img2.imgtn.bdimg.com/it/u=1561660534,130168102&fm=26&gp=0.jpg',
        avatar: 'http://img4.imgtn.bdimg.com/it/u=349345436,3394162868&fm=26&gp=0.jpg'
      }
    ],
  
    baseImgUrl: app.globalData.imageUrl,
    listData: [{
      desc: "string",
      imageUrl: "/group3/M00/00/0B/Cv6CBF0RvcuAXoDUAA5M5vbNswU382.png",
      link: { linkId: "10", brandId: "2711", name: "会员服务", url: "5", type: 2, status: 0 },
      number: 1,
      redirectId: "10",
      type: 0
    }, {
      desc: "string",
        imageUrl: "/group3/M00/00/08/Cv6CBF0BqvyADM4iAABJQ-gxAy4311.png",
      link: { linkId: "10", brandId: "2711", name: "会员服务", url: "5", type: 2, status: 0 },
      number: 1,
      redirectId: "10",
      type: 0
    },
      {
        desc: "string",
        imageUrl: "/group3/M00/00/08/Cv6CBF0BqvyADM4iAABJQ-gxAy4311.png",
        link: { linkId: "10", brandId: "2711", name: "会员服务", url: "5", type: 2, status: 0 },
        number: 1,
        redirectId: "10",
        type: 0
      },
      {
        desc: "string",
        imageUrl: "/group3/M00/00/0B/Cv6CBF0RvcuAXoDUAA5M5vbNswU382.png",
        link: { linkId: "10", brandId: "2711", name: "会员服务", url: "5", type: 2, status: 0 },
        number: 1,
        redirectId: "10",
        type: 0
      }],
    pageNo: 1,
    pageSize: 10,
    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, 10, 1)
    } else if (current == 1) {
      // 失效
      this.getCouponList(1, 10, 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, 10, 1)
    } else if (current == 1) {
      // 失效
      this.getCouponList(1, 10, 0)
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() { },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // 可使用 已失效
    this.getCouponList(1, 10, 1)
    this.getCouponList(1, 10, 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
          })
        }
      }

    }).finally(() => {
      wx.hideLoading();
    })
  },
  /**
 * 页面相关事件处理函数--监听用户下拉动作
 */
  onPullDownRefresh: function () { },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    // 可使用 0  or 已失效 1
    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 {
      if (invalidPageNo < invalidTotalPages) {
        this.setData({
          invalidPageNo: invalidPageNo + 1,
        })
        // status 0
        this.getCouponList(this.data.invalidPageNo, pageSize, 0)
      } else {
        this.setData({
          noMoreCouponInvalid: true
        })
      }
    }
  },
})