const navigationBarHeight = (getApp().statusBarHeight + 44) + 'px'
const format = require("../../../utils/util.js");
const config = require("../../../config/config.js");
const app = getApp();
const Http = require("../../../utils/HttpBasics");
const imgurl = require("../../../utils/imgurl");
Page({
  data: {
    navigationBarHeight,
    noCoupon:imgurl.noCoupon.url,
    couponUrl: imgurl.coupon.url,
    linessUrl: imgurl.liness.url,
    loadingUrl: imgurl.loading.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,
    goHomeUrl: "",
  },
  onLoad() {
    this.setData({
      goHomeUrl: app.globalData.goHomeUrl
    })
    this.getList(0, 1);
  },

  //点击跳转到券详情页面
  gotouse: function (e) {
    wx.navigateTo({
      url: `/pages/passCar/couponDetail/couponDetail?quancode=${
        e.currentTarget.dataset.quancode
        }`,
      success: function (res) {
        // success
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    });
  },
  getList(key, pageNum) {
    var that = this;
    if (that.data.allow_load) {
      wx.showLoading({
        title: "加载中"
      });
      Http.get({
        url: config.api.couponOrderCarList + "?type=5",
        data: {
          pageNum: pageNum,
          pageSize: 8,
          couponOrderStatus: key
        }
      }).then(res => {
        res.data.list.map(file => {
          file.expiredTime = format.formatTime(
            file.expiredTime,
            "yyyy-MM-dd hh:mm:ss"
          );
        });
        setTimeout(function () {
          wx.hideLoading();
        }, 1200);
        if (pageNum > res.data.pages) {
          that.setData({
            allow_load: false
          });
        }
        /**
         * 先赋值后渲染页面
         * concat 不会改变原数组值
         * push   会改变原数组值，但不会一条一条插入，而是整个数组插入
         */
        that.data.list = that.data.list.concat(res.data.list);
        that.setData({
          list: that.data.list
        });
      })
      .catch(err => {
        setTimeout(function () {
          wx.hideLoading();
        }, 1200);
        wx.showToast({
          title: err.errMsg,
          icon: 'none',
          duration: 2000,
          mask: false
        });
      })
    } else {
    }
  },
  handleChange({ detail }) {
    this.setData({
      current: detail.key
    });
  },
  handleChangeScroll({ detail }) {
    this.setData({
      list: [],
      allow_load: true,
      current_scroll: detail.key
    });
    this.getList(detail.key, 1);
    this.setData({
      current_scroll: detail.key
    });
  },
  onReachBottom: function () {
    var that = this;
    that.data.page++;
    that.setData({
      page: that.data.page
    });
    that.getList(that.data.current_scroll, that.data.page);
  }
});
