
var app = getApp();
const { hostname } = app.globalData;
Page({

  /**
   * 页面的初始数据
   */
  data: {
      imgUrlBefore:''
  },

  //进秒杀详情页
  goGroupGoodsDetail: function (e) {
      var goodsid = e.currentTarget.dataset.goodsid;
      wx.navigateTo({
          url: '../../index/spike_details/spike_details?id=' + goodsid,
      })
  },

  //获取秒杀列表
  getGroupGoodsList: function (that) {
      var groupGoodsHasMore = (that.data.groupGoodsHasMore == undefined ? true : that.data.groupGoodsHasMore);
      var groupPage = (that.data.groupPage == undefined ? 1 : that.data.groupPage);
      var groupGoodsList = (that.data.groupGoodsList == undefined ? [] : that.data.groupGoodsList);
      if (groupGoodsHasMore) {
          that.setData({
              groupGoodsHasMore: false
          })
          wx.showLoading({
              title: '正在加载',
          })
          wx.request({
              url: hostname + 'seckill_goods_list',
              data: { page: groupPage },
              header: { 'content-type': 'application/x-www-form-urlencoded' },
              method: 'POST',
              dataType: 'json',
              success: function (groupRes) {
                  console.log(groupRes, 'groupRes');
                  if (groupRes.data.code == 200) {
                      var newGroupGoodsList = groupRes.data.data;
                      that.setData({
                          groupGoodsList: groupGoodsList.concat(newGroupGoodsList),
                          groupGoodsHasMore: true,
                          groupPage: ++groupPage,
                          newGroupGoodsList: newGroupGoodsList,
                      })
                      that.countInit(that);
                  } else {
                      if (groupPage == 1) {
                          that.setData({
                              groupGoodsList: [],
                          })
                      }
                      that.setData({
                          groupGoodsHasMore: false,
                      })
                  }
              },
              fail: function (groupRes) { },
              complete: function (groupRes) {
                  setTimeout(function () {
                      wx.hideLoading()
                  }, 1000)
              },
          })
      }
  },
  //倒计时
  countInit(that) {
      var clockData = that.data.clockData || [];
      var newGroupGoodsList = that.data.newGroupGoodsList;
      var currentTimeTamp = Math.floor(new Date().getTime() / 1000);
      if (newGroupGoodsList.length == 0) { return false }
      
      newGroupGoodsList.forEach((item, index) => {
          var temp = { start_time: item.start_time, end_time: item.end_time };
          if (item.start_time - currentTimeTamp > 0) {
              temp.time_type = "notStart";
              temp.micro_second = item.start_time - currentTimeTamp;
              temp.time_left = app.dateformat(temp.micro_second);
          } else if ((currentTimeTamp - item.start_time > 0) && (item.end_time - currentTimeTamp > 0)) {
              temp.time_type = "hasStart";
              temp.micro_second = item.end_time - currentTimeTamp;
              temp.time_left = app.dateformat(temp.micro_second);
          } else if (currentTimeTamp - item.end_time > 0) {
              temp.time_type = "hasStop";
          }
          clockData.push(temp);
      })
      that.setData({
          clockData: clockData,
      })
      if (!that.data.hasTimer) {
          that.setData({
              hasTimer: true
          })
          app.countdown(that)
      }

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.getGroupGoodsList(that);
    
    that.setData({
        imgUrlBefore: app.globalData.imgUrlBefore
    })
    console.log(app.globalData.imgUrlBefore)
  },
  
 
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },


})