const wx2my = require('../../wx2my');
const Behavior = require('../../Behavior');
// 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: 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:cardNo
    });
  },

  handelPresentCouponCode(e) {
    let {
      cardNo
    } = e.currentTarget.dataset;
    wxService.router(`/pages/couponCodeDetail/couponCodeDetail`).search({
      cardno:cardNo
    });
  },

  //获取优惠券列表
  getCouponList(pageNo, pageSize, statusNum) {
    wx2my.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) {
        wx2my.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(() => {
      wx2my.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
        });
      }
    }
  }
});