const navigationBarHeight = (getApp().statusBarHeight + 44) + 'px'
let config = require("../../../config/config.js");
let Http = require("../../../utils/HttpBasics");
const util = require("../../../utils/util");
let app = getApp();
const imgurl = require("../../../utils/imgurl");
Page({
  data: {
    navigationBarHeight,
    orangeImg: imgurl.orange.url,
    blueImg: imgurl.blue.url,
    grayImg: imgurl.gray.url,
    noCoupon: imgurl.noCoupon.url,
    dingdanUrl: imgurl.dingdan.url,
    loadingUrl: imgurl.loading.url,
    wmhome: imgurl.wmhome.url,
    wmdiscount: imgurl.wmdiscount.url,
    wmgive: imgurl.wmgive.url,
    key:0,
    tabs: [
      {
        key: 0,
        name: "报名成功 "
      },
      {
        key: 1,
        name: "报名中"
      },
      {
        key: 2,
        name: "报名失败"
      }
    ],
    list: [],
    current: "",
    loading: true, //"上拉加载"的变量，默认false，隐藏
    current_scroll: "0",
    allow_load: true,
    page: 1,
    content: "",
    goHomeUrl:"",
  },
  goback: function () {
    let this_ = this
    wx.switchTab({
      url: this_.data.goHomeUrl,
    })
  },
  onLoad(options) {
    this.setData({
      goHomeUrl: app.globalData.goHomeUrl,
      mouldConfig: app.globalData.mouldConfig,
    })
    this.getList(0, 1);
    this.setData({
      current_scroll: 0
    });
  },
  //活动详情
  gotoDetail:function(e){
    wx.navigateTo({
      url: `/pages/radetail/index?id=${e.currentTarget.dataset.id}`
    })
  },
  gotopay: function (e) {
    wx.navigateTo({
      url: `/pages/order/detail/index?orderId=${e.currentTarget.dataset.id}&flag='pay'`
    })
  },
  getList(key, pageNum) {
    let that = this;
    let variable;
    if(key == 0){
      that.setData({
        key:0
      })
      variable = {
        pageNum: pageNum,
        pageSize: 15,
        // statusStr:"0,1,2"
        status:1
      };
    }else if(key == 1){
      that.setData({
        key: 1
      })
      variable = {
        pageNum: pageNum,
        pageSize: 15,
        status: 0
      };
    }else if(key==2){
      that.setData({
        key: 2
      })
      variable = {
        pageNum: pageNum,
        pageSize: 15,
        status: 2
      };
    }
  
    if (that.data.allow_load) {
      that.setData({
        loading: true,
        content: '小主,我在玩命加载中...'
      })

      Http.get({
        url: config.api.activityList,
        data: variable
      }).then(res => {
        if (pageNum >= res.data.pages) {
          that.setData({
            allow_load: false
          });
          setTimeout(function () {
            that.setData({
              loading: false,
            })
          }, 1400);
        }
        setTimeout(function () {
          that.setData({
            loading: false,
          })
        }, 1400);
        if (pageNum == 1) {
          that.setData({
            list: []
          })
        }
        var tmpArr = that.data.list;
        tmpArr.push.apply(tmpArr, res.data.list);
        // 将砍价的状态过滤出来
        that.setData({
          list: tmpArr
        })
        let tmpeArr = that.data.list
        tmpeArr.map(item=>{
          if(item.activityEndTime<new Date().getTime()){
            item.past = true
          }
          item.startTime = util.formatTime(item.startTime, "yyyy-MM-dd hh:mm:ss");
          item.endTime = util.formatTime(item.endTime, "yyyy-MM-dd hh:mm:ss");
        })
        that.setData({
          list: tmpeArr
        })
        for (let i = 0; i < that.data.list.length; i++) {
          var activityStartTime1 = util.formatTime(that.data.list[i].activityStartTime, "yyyy-MM-dd hh:mm:ss" );
          var activityEndTime1 = util.formatTime(that.data.list[i].activityEndTime, "yyyy-MM-dd hh:mm:ss");
          /**
           * 修改list的endtime
           * 渲染到页面
           */
          var activityStartTime = 'list[' + i + '].activityStartTime'
          var activityEndTime = 'list[' + i + '].activityEndTime'
          
          that.setData({
            [activityStartTime]: activityStartTime1,
            [activityEndTime]: activityEndTime1,
          });
        }
      })
      .catch(err => {
        wx.showToast({
          title: err.errMsg,
          icon:"none",
          duration:2000
        })
      })
    } 
    else {
      that.setData({
        loading: true,
        content: "——— 再拉裤子就掉了啦 ———"
      })
      setTimeout(function () {
        that.setData({
          loading: false,
        })
      }, 1400)
    }
  },
  handleChange({
    detail
  }) {
    this.setData({
      current: detail.key
    });
  },
  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);
  },
});