const navigationBarHeight = (getApp().statusBarHeight + 44) + 'px';
const util = require("../../../utils/util.js");
const config = require("../../../config/config.js");
const Http = require("../../../utils/HttpBasics");
let app = getApp();
const imgurl = require("../../../utils/imgurl");
Page({
  data: {
    navigationBarHeight,
    noCoupon: imgurl.noCoupon.url,
    couponUrl: imgurl.coupon.url,
    linessUrl: imgurl.liness.url,
    wmhome: imgurl.wmhome.url,
    wmdiscount: imgurl.wmdiscount.url,
    loadingUrl: imgurl.loading.url,
    canTransferred: imgurl.canTransferred.url,
    weixinTitle: imgurl.weixinTitle.url,
    shixiao: imgurl.shixiao.url,
    tabs: [{
      key: 4,
      name: "使用中"
    },
    {
      key: "5,6,7",
      name: "已失效"
    }
    ],
    list: [],
    current: "4",
    current_scroll: "4",
    page: 1,
    allow_load: true,
    loading: true, //"上拉加载"的变量，默认false，隐藏
    content: "",
    mystatus: '',
    showPage: false,
    goHomeUrl: "",
  },
  goback: function () {
    let this_ = this
    wx.switchTab({
      url: this_.data.goHomeUrl,
    })
  },
  onLoad: function () {
    this.setData({
      goHomeUrl: app.globalData.goHomeUrl
    })
    let that = this;
    if (that.data.current_scroll == "4") {
      that.getList(4, 1);
    } else {
      that.getList('5,6,7', 1);
    }
    wx.setStorage({
      key: 'couponNum2',
      data: "couponNum3",
    })
    // wx.hideTabBarRedDot({
    //   index: 3
    // })
  },
  userule: function () {
    wx.navigateTo({
      url: '/pages/cardorder/userule/userule',
    })
  },
  // 扫一扫去支付
  gotoPay: function (e) {
    app.globalData.previewFlag = true
    /* wx.navigateTo({
      url: `/pages/scanPay/scanPay`,
    })
    return */
    let that = this;
    wx.scanCode({
      success: (res) => {
        if (util.isJSON(res.result)) {
          let value = JSON.parse(res.result);
          if (value.END == 'B' && value.TYPE == 'merchant' && value.ID) {
            Http.get({
              url: config.api.findByCode,
              data: {
                merchantCode: value.ID,
              }
            })
              .then(res => {
                if (res.code == 200) {
                  let merChantDetail = JSON.stringify(res.data);
                  if (merChantDetail && e.currentTarget.dataset.cardid && e.currentTarget.dataset.remainingamount) {
                    merChantDetail = encodeURIComponent(merChantDetail)
                    wx.navigateTo({
                      url: `/pages/scanPay/scanPay?merChant=${merChantDetail}&cardid=${e.currentTarget.dataset.cardid}&remainingAmount=${e.currentTarget.dataset.remainingamount}`,
                    })
                  }
                }
              })
              .catch(err => {
                console.log(err)
              })
          } else {
            wx.showToast({
              title: "未识别到商户二维码",
              icon: "none",
              mask: false
            })
          }
        } else {
          wx.showToast({
            title: "未识别到商户二维码",
            icon: "none",
            mask: false
          })
        }
      },
      fail: (res) => {
        wx.showToast({
          title: "未识别到商户二维码",
          icon: "none",
          mask: false
        })
      }
    })
  },
  // 跳转到详情
  gotoConsumeDetail: function (e) {
    console.log(e, 'e');
    if (e.currentTarget.dataset.id) {
      // wx.navigateTo({
      //   url: `/pages/ConsumeDetail/ConsumeDetail?cardId=${e.currentTarget.dataset.id}`,
      // })
      wx.navigateTo({
        url: `/pages/cardorder/cardUse/cardUse?cardId=${e.currentTarget.dataset.id}&showImg=${e.currentTarget.dataset.showimg}`,
      })
    }
  },

  //点击跳转到券详情页面
  gotouse: function (e) {
    if (this.data.mystatus == '' || this.data.mystatus == 'undefined') {
      var mystatus = e.currentTarget.dataset.couponorderstatus;
    } else {
      var mystatus = this.data.mystatus;
    }
    if (e.currentTarget.dataset.quancode) {
      wx.navigateTo({
        url: `/pages/couponorder/detail/index?quancode=${e.currentTarget.dataset.quancode}&couponorderstatus=${mystatus}`
      });
    }
  },
  gotoBuy: function () {
    wx.navigateTo({
      url: '/pages/discountCardList/discountCardList',
    })
  },
  getList(key, pageNum) {
    var that = this;
    // if (that.data.allow_load) {
    that.setData({
      loading: true,
      content: "小主,我在玩命加载中...",
    });
    if (key == "4") {
      var data = {
        pageNum: pageNum,
        pageSize: 6,
        couponType: "100",
        couponOrderStatus: 4
      }
    } else if (key == '5,6,7') {
      var data = {
        pageNum: pageNum,
        pageSize: 6,
        couponType: "100",
        statusStr: "5,6,7"
      }
    }
    Http.get({
      url: config.api.cardorderList,
      data: data
    })
      .then(res => {
        console.log(res)
        if (res.code == 200) {
          that.setData({
            showPage: true
          })
        }

        if (res.data.list && res.data.list.length > 0) {
          res.data.list.map(file => {
            file.expiredTime = util.formatTime(file.expiredTime, "yyyy.MM.dd")
            if (file.couponOrderStatus == 5 || file.couponOrderStatus == 6 || file.couponOrderStatus == 7 || file.couponOrderStatus == 8) {
              file.background = 'rgba(179,180,181,1)';
              file.showImg = true;
            } else if (file.couponOrderStatus == 4) {
              if (file.remainingAmount > 0 && 29900 >= file.remainingAmount) {
                file.background = '#7184E2'
              } else if (file.remainingAmount >= 30000 && 49900 >= file.remainingAmount) {
                file.background = '#63AAE6'
              } else if (file.remainingAmount >= 50000 && 99900 >= file.remainingAmount) {
                file.background = '#E2A471'
              } else if (file.remainingAmount >= 100000) {
                file.background = '#E67663'
              }
            }

          });
        }
        setTimeout(function () {
          that.setData({
            loading: false
          });
        }, 1400);
        if (pageNum >= res.data.pages && pageNum != 1) {
          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.message ? err.message : err.data,
          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);
  },

  onShow() {
    const that = this;
    this.getList(that.data.current_scroll, that.data.page);
  },

  onReachBottom: function () {
    const that = this;
    that.data.page++;
    that.setData({
      page: that.data.page
    });
    that.getList(that.data.current_scroll, that.data.page);
  },
  /**
 * 页面相关事件处理函数--监听用户下拉动作
 */
  onPullDownRefresh() {
    const that = this;
    this.getList(that.data.current_scroll, that.data.page);
  },
});