const navigationBarHeight = (getApp().statusBarHeight + 44) + 'px'
const util = require("../../../utils/util.js");
const config = require("../../../config/config.js");
const Http = require("../../../utils/HttpBasics");
const imgurl = require("../../../utils/imgurl");
let app = getApp();
Page({
  data: {
    navigationBarHeight,
    couponUrl: imgurl.noCoupon.url,
    linessUrl: imgurl.liness.url,
    loadingUrl: imgurl.loading.url,
    wmhome: imgurl.wmhome.url,
    wmgive: imgurl.wmgive.url,
    tabs: [{
      key: 0,
      name: "未使用"
    },
    {
      key: 1,
      name: "已使用"
    },
    {
      key: 2,
      name: "已过期"
    },
    {
      key: 3,
      name: "已退款"
    }
    ],
    list: [],
    current: "0",
    current_scroll: "0",
    page: 1,
    allow_load: true,
    loading: false, //"上拉加载"的变量，默认false，隐藏
    content: "",
    mystatus: '',
    showPage: false,
    goHomeUrl: "",
  },
  onLoad() {
    this.setData({
      goHomeUrl: app.globalData.goHomeUrl
    })
    this.getList(0, 1);

  },
  onShow: function () {
    let that = this;
    wx.setStorage({
      key: 'couponNum',
      data: "couponNum1",
    })
    // wx.hideTabBarRedDot({
    //   index:2
    // })
  },
  goback: function () {
    let this_ = this
    wx.switchTab({
      url: this_.data.goHomeUrl,
    })
  },
  //点击跳转到券详情页面
  gotouse: function (e) {
    if (this.data.mystatus == '' || this.data.mystatus == 'undefined') {
      var mystatus = e.currentTarget.dataset.couponorderstatus;
    } else {
      var mystatus = this.data.mystatus;
    }
    wx.navigateTo({
      url: `/pages/couponorder/detail/index?quancode=${e.currentTarget.dataset.quancode}&couponorderstatus=${mystatus}`
    });
  },
  getList(key, pageNum) {
    var that = this;
    if (that.data.allow_load) {
      that.setData({
        loading: true,
        content: "小主,我在玩命加载中...",
      });
      Http.get({
        url: config.api.couponOrderList,
        data: {
          pageNum: pageNum,
          pageSize: 10,
          couponOrderStatus: key
        }
      })
        .then(res => {
          if (res.code == 200) {
            that.setData({
              showPage: true
            })
            setTimeout(() => {
              that.setData({
                loading: false
              });
            }, 1400);
          }
          res.data.list.map(file => {
            file.expiredTimeStr = util.fmtDate(Number(file.expiredTime));
            console.log(file.expiredTime, file.expiredTimeStr)
          });

          if (pageNum >= res.data.pages) {
            that.setData({
              allow_load: false
            });
          }
          if (pageNum == 1) {
            that.setData({
              list: []
            })
          }
          var tmpArr = [];
          tmpArr = that.data.list
          // Object.assign(tmpArr, res.data.list);
          if (pageNum == 1) {
            console.log(res.data.list)
            that.setData({
              list: res.data.list
            })
          } else {
            tmpArr = [...tmpArr, ...res.data.list]
            that.setData({
              list: tmpArr
            })
          }

        })
        .catch(err => {
          wx.showModal({
            title: '提示',
            content: err.errMsg,
            showCancel: false
          })
        })
    } else {
      that.setData({
        loading: true,
        content: "——— 再拉裤子就掉了啦 ———"
      });
      setTimeout(function () {
        that.setData({
          loading: false
        });
      }, 1400);
    }
  },
  handleChangeScroll({
    detail
  }) {
    this.setData({
      list: [],
      allow_load: true,
      current_scroll: detail.key,
      page: 1,
    });
    this.getList(detail.key, 1);
  },
  onReachBottom: function () {
    var that = this;
    that.data.page++;
    that.setData({
      page: that.data.page
    });
    that.getList(that.data.current_scroll, that.data.page);
  }
});