const navigationBarHeight = (getApp().statusBarHeight + 44) + 'px'
let config = require("../../config/config.js");
let Http = require("../../utils/HttpBasics");
const util = require("../../utils/util");
const app = getApp();
const imgurl = require("../../utils/imgurl");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    navigationBarHeight,
    linesUrl: imgurl.lines.url,
    loadingUrl: imgurl.loading.url,
    wmhome: imgurl.wmhome.url,
    wmgift: imgurl.wmgift.url,
    list: [],
    page: 1,
    allow_load: true,
    day: "",
    hour: "",
    minute: "",
    loading: true, //"上拉加载"的变量，默认false，隐藏
    content: "",
    showPage:false,
    goHomeUrl:"",
  },
  goback: function () {
    let this_ = this
    wx.switchTab({
      url: this_.data.goHomeUrl,
    })
  },
  onLoad: function (options) {
    this.setData({
      goHomeUrl: app.globalData.goHomeUrl,
    })
  },
  //列表
  getList: function (page) {
    let that = this;
    app.couponChannelListCallback = token => {
      Http.setToken(token);
      /**
       * 判断用户是否加载完成
       */
      if (that.data.allow_load) {
        that.setData({
          loading: true,
          content: '小主,我在玩命加载中...'
        })
        Http.get({
          url: config.api.couponChannelList,
          data: {
            pageNum: page,
            pageSize: 8,
            targetAd: 2
          }
        })
        .then(res => {
          if(res.code == 200){
            that.setData({
              showPage:true
            })
          }
          if (page >= res.data.pages) {
            that.setData({
              allow_load: false
            });
            setTimeout(function () {
              that.setData({
                loading: false,
              })
            }, 1400);
          }

          setTimeout(function () {
            that.setData({
              loading: false,
            })
          }, 1400);
          var tmpArr = that.data.list;
          tmpArr.push.apply(tmpArr, res.data.list);
          that.setData({
            list: tmpArr
          })
          for (let i = page*8-8; i < that.data.list.length; i++) {
            var startTime = util.formatTime(that.data.list[i].endTime, "yyyy-MM-dd hh:mm:ss");
            var beginTimes = util.formatTime(that.data.list[i].beginTime, "yyyy-MM-dd hh:mm:ss");
            var alsell = Math.floor((that.data.list[i].inventory - (that.data.list[i].remainInventory))/(that.data.list[i].inventory)*100);
            /**
             * 修改list的endtime
             * 渲染到页面
             */
            var endtime = 'list[' + i + '].endtime';
            var beginTime = 'list[' + i + '].beginTime';
            var flags = 'list[' + i + '].flags';
            var alsells = 'list[' + i + '].alsells';
            //活动未开始
            if (that.data.list[i].activityStatus==0){
              if (util.timechuo(beginTime).indexOf('-') == 0) {
                that.setData({
                  [flags]: "end",
                  [beginTime]: util.timechuo(beginTimes),
                  [alsells]: alsell
                });
              }
              else {
                that.setData({
                  [beginTime]: util.timechuo(beginTimes),
                  [alsells]: alsell
                });
              }
            } if (that.data.list[i].activityStatus != 0) {
              //活动已经开始
              if (util.timechuo(startTime).indexOf('-') == 0) {
                that.setData({
                  [flags]: "end",
                  [endtime]: util.timechuo(startTime),
                  [alsells]: alsell
                });
              }
              else {
                that.setData({
                  [endtime]: util.timechuo(startTime),
                  [alsells]: alsell
                });
              }
            }
            
          }

        })
        .catch(err => {
          wx.showModal({
            title: '提示',
            content: err.errMsg,
            showCancel:false
          })
        })
      } else {
        that.setData({
          loading: true,
          content: "——— 再拉裤子就掉了啦 ———"
        })
        setTimeout(function () {
          that.setData({
            loading: false,
          })
        }, 1400)
      }
    };
    if (app.globalData.token && app.globalData.token != null) {
      app.couponChannelListCallback(app.globalData.token);
    }
  },
  onReady: function () {
    let that = this;
    that.setData({
      list: []
    });
    that.getList(1);
  },
  //限时抢购的详情页面
  gotodetail: function (e) {
    wx.navigateTo({
      url: `/pages/coupon/detail/index?couponChannelId=${
        e.currentTarget.dataset.couponchannelid
      }&couponId=${e.currentTarget.dataset.couponid}`
    });
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let that = this;
    that.data.page++;
    that.getList(that.data.page);
  },
});