const util = require("../../../utils/util.js");
const config = require("../../../config/config.js");
const Http = require("../../../utils/HttpBasics");
let app = getApp();
Page({
  data: {
    tabs: [{
        key: 4,
        name: "使用中"
      },
      {
        key: 5,
        name: "已失效"
      }
    ],
    list: [],
    current: "4",
    current_scroll: "4",
    page: 1,
    allow_load: true,
    loading: true, //"上拉加载"的变量，默认false，隐藏
    content: "",
    mystatus: '',
    showPage:false
  },
  onLoad() {
    this.getList(4, 1);
  },
  onShow: function () {
    let that = this;
    wx.setStorage({
      key: 'couponNum',
      data: "couponNum1",
    })
    wx.hideTabBarRedDot({
      index:2
    })
  },
  // 扫一扫去支付
  gotoPay:function(e){
    console.log(e.currentTarget.dataset.cardid)
    Http.post({
      url: config.api.orderCreate,
      data: {
        cardId: e.currentTarget.dataset.cardid,
        merchantCode: "234567451101102080",
        totalFee: "5"
      }
    }).then(res=>{
      console.log(res)
    })
  },
  //点击跳转到券详情页面
  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.cardorderList,
        data: {
          pageNum: pageNum,
          pageSize: 6,
          couponType:"7",
          couponOrderStatus: key
        }
      })
      .then(res => {
        if(res.code == 200){
          that.setData({
            showPage:true
          })
        }
        res.data.list.map(file => {
          file.expiredTime = util.fmtDate(file.expiredTime);
        });
        setTimeout(function () {
          that.setData({
            loading: false
          });
        }, 1400);
        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
        })
      })
      .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);
  }
});