const util = require("../../utils/util.js");
const config = require("../../config/config.js");
const Http = require("../../utils/HttpBasics");
let app = getApp();
Page({
  data: {
    tabs: [],
    list: [],
    current: "0",
    current_scroll: "0",
    page: 1,
    allow_load: true,
    loading: true, //"上拉加载"的变量，默认false，隐藏
    content: "",
    mystatus: '',
    showPage: false,
  },
  onLoad() {
    this.getList(0, 1);
  },
  onShow: function () {
    let that = this;
    wx.setStorage({
      key: 'couponNum',
      data: "couponNum1",
    })
    wx.hideTabBarRedDot({
      index: 2
    })
    Http.get({
      url: config.api.businessList,
      data: {
        pageNum: 1,
        pageSize: 15,
        type: 1
      }
    }).then(res => {
      console.log(res)
      let businessObj = [{ id: 0, title: "全部", type: 1 }];
      that.setData({
        tabs: res.data.list.concat(businessObj).sort(compare("id"))
      });
      console.log(that.data.tabs)
    })
      .catch(err => {
        wx.showToast({
          title: err.errMsg,
          icon: 'none',
          duration: 2000,
          mask: false
        });
      })
  },
  //点击跳转到券详情页面
  gotouse: function (e) {
    console.log(e.currentTarget.dataset.couponid)
    wx.navigateTo({
      url: `/pages/coupon/detail/index?couponChannelId=${e.currentTarget.dataset.quancode}&couponId=${e.currentTarget.dataset.couponid}`
    });
  },
  getList(key, pageNum) {
    var that = this;
    // 根据 key == 0 区分全部或其它tab，决定是否传参数 business
    if (key == 0) {
      var param = {
        pageNum: pageNum,
        pageSize: 6,
        targetAd: 1,
        type: "7"
      };
    } else {
      var param = {
        pageNum: pageNum,
        pageSize: 6,
        business: key,
        targetAd: 1,
        type: "7"
      };
    }
    if (that.data.allow_load) {
      that.setData({
        loading: true,
        content: "小主,我在玩命加载中...",
      });
      Http.get({
        url: config.api.couponChannelList,
        data: param
      })
        .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);
  }
})
function compare(pro) {
  return function (obj2, obj1) {
    var val1 = obj1[pro];
    var val2 = obj2[pro];
    if (val1 < val2) {
      return 1;
    } else if (val1 > val2) {
      return -1;
    } else {
      return 0;
    }
  }
}