const util = require("../../../utils/util.js");
const config = require("../../../config/config.js");
const Http = require("../../../utils/HttpBasics");
Page({
  data: {
    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: true, //"上拉加载"的变量，默认false，隐藏
    content: "",
    mystatus: ''
  },
  onLoad() {
    this.getList(0, 1);
  },
  onShow: function () {
    let that = this;
    wx.setStorage({
      key: 'couponNum',
      data: "couponNum1",
    })
    wx.hideTabBarRedDot({
      index:2
    }),
    wx.getScreenBrightness({
      success:function(res){
        that.setData({
          sight:res.value
        })
      }
    });
  },
  //点击跳转到券详情页面
  gotouse: function (e) {
    console.log(e.currentTarget.dataset.couponorderstatus);
    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}&sight=${this.data.sight}`
    });
  },
  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: 6,
          couponOrderStatus: key
        }
      }).then(res => {
        console.log(res);
        res.data.list.map(file => {
          file.expiredTime = util.fmtDate(file.expiredTime);
        });
        setTimeout(function () {
          that.setData({
            loading: false
          });
        }, 1400);
        console.log(pageNum);
        if (pageNum >= res.data.pages) {
          that.setData({
            allow_load: false
          });
        }
        if (pageNum == 1) {
          that.setData({
            list: []
          })
        }
        var tmpArr = that.data.list;
        tmpArr.push.apply(tmpArr, res.data.list);
        that.setData({
          list: tmpArr
        })
      });
    } 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
    });
    console.log(that.data.page+"页数");
    console.log(that.data.current_scroll+"点击的tab数")
    that.getList(that.data.current_scroll, that.data.page);
  }
});