const navigationBarHeight = (getApp().statusBarHeight + 50) + 'px'
const util = require("../../utils/util.js");
const config = require("../../config/config.js");
const Http = require("../../utils/HttpBasics");
const imgurl = require("../../utils/imgurl");
let app = getApp();
Page({
  data: {
    mouldType: 0,
    mouldConfig:{},
    navigationBarHeight,
    tabData:[
      {
        title: '全部',
        id: ''
      },
      {
        title:'可转赠',
        id:1
      },
      {
        title: '不可转赠',
        id: 0
      }
    ],
    noCoupon: imgurl.noCoupon.url,
    couponUrl: imgurl.coupon.url,
    loadingUrl: imgurl.loading.url,
    wmhome: imgurl.wmhome.url,
    canTransferred: imgurl.canTransferred.url,
    tabs: [],
    list: [],
    current: "0",
    current_scroll: "",
    page: 1,
    allow_load: true,
    loading: true, //"上拉加载"的变量，默认false，隐藏
    content: "",
    mystatus: '',
    showPage: false,
    paramData:null,
    goHomeUrl: "",
  },
  goback: function () {
    let this_ = this
    wx.switchTab({
      url: this_.data.goHomeUrl,
    })
  },
  onLoad(options) {
    this.setData({
      goHomeUrl: app.globalData.goHomeUrl,
      mouldType: app.globalData.mouldType,
      mouldConfig: app.globalData.mouldConfig,
    })
    console.log(this.data.mouldConfig,"mouldConfig")
    this.getList(0, 1);
    if(options.fromId){
      this.setData({
        paramData:options
      },function(){
        // this.userLogin()
      })
    }
  },
  //点击跳转到卡详情页面
  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}&cardType=${e.currentTarget.dataset.type}`
    });
  },
  getList(key, pageNum) {
    var that = this;
    // 根据 key == 0 区分全部或其它tab，决定是否传参数 business
    if (!key) {
      var param = {
        pageNum: pageNum,
        pageSize: 8,
        targetAd: 5,
      };
    } else {
      var param = {
        pageNum: pageNum,
        pageSize: 8,
        supportTransfer:key,
        targetAd: 5,
      };
    }
    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
  }) {
    console.log(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);
  },
  /**
   * 用户登录
   */
  // userLogin: function () {
  //   var that = this;
  //   // 登录
  //   wx.login({
  //     success: ({
  //       code
  //     }) => {
  //       wx.getSystemInfo({
  //         success: function (res) {
  //           that.setData({
  //             systemInfo: JSON.stringify(res)
  //           })
  //         }
  //       })
  //       var usrdata = {
  //         appId: config.weapp.AppId,
  //         code: code,
  //         sceneAddress: app.globalData.sceneAddress,
  //         scene: that.data.scene,
  //         systemInfo: that.data.systemInfo
  //       };
  //       if (app.globalData.locationInfo) {
  //         usrdata = {
  //           appId: config.weapp.AppId,
  //           code: code,
  //           sceneAddress: app.globalData.sceneAddress,
  //           latitude: "" + app.globalData.locationInfo.latitude,
  //           longitude: "" + app.globalData.locationInfo.longitude,
  //           scene: that.data.scene,
  //           systemInfo: that.data.systemInfo
  //         };
  //       }
  //       Http.post({
  //         url: config.api.login,
  //         data: usrdata
  //       })
  //         .then(res => {
  //           that.setData({
  //             showPages: true
  //           })
  //           app.globalData.token = res.data.token;
  //           Http.setToken(res.data.token);
  //           that.checkuerstatus();
  //         })
  //         .catch(err => {
  //           wx.showModal({
  //             title: '提示',
  //             showCancel: false,
  //             content: '登录失败，请重新尝试',
  //             success: function (res) {
  //               if (res.cancel) {
  //                 //点击取消,默认隐藏弹框
  //               } else {
  //                 //点击确定
  //                 wx.reLaunch({
  //                   url: '/pages/index/index',
  //                 })
  //               }
  //             }
  //           })
  //         });
  //     }
  //   });
  // },
  checkuerstatus(){
    Http.get({
      url: config.api.checkPhoneStatus,
      data: {}
    })
    .then(res => {
      
    })
    .catch(err => {
      if (err.code == 11005) {
        // 用户手机未授权
        /**
         * 将值传到用户手机号授权的页面
         *
         */
        wx.redirectTo({
          url: "/pages/getphoneInfo/index?path=index&fromId=" + that.data.paramData.fromId
        });
      } else if (err.code == 11006) {
        // 用户手机已加密
        wx.redirectTo({
          url: "/pages/phoneinput/phoneinput?path=index&fromId=" + that.data.paramData.fromId
        });
      } else {
        wx.showToast({
          title: err.message,
          icon: 'none',
          duration: 2000,
          mask: false
        });
      }
    })
  }
})
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;
    }
  }
}