let config = require("../../config/config.js");
let Http = require("../../utils/HttpBasics");
const util = require("../../utils/util");
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    page: 1,
    allow_load: true,
    day: "",
    hour: "",
    minute: "",
    loading: true, //"上拉加载"的变量，默认false，隐藏
    content: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {},
  //列表
  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: 5,
            targetAd: 2
          }
        }).then(res => {
          console.log(res);
          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 = 0; i < that.data.list.length; i++) {
            var startTime = util.formatTime(that.data.list[i].endTime, "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);
            util.timechuo(startTime);
            console.log(alsell);
            /**
             * 修改list的endtime
             * 渲染到页面
             */
            var endtime = 'list[' + i + '].endtime';
            var flags = 'list[' + i + '].flags';
            var alsells = 'list[' + i + '].alsells';
            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
              });
            }
          }

        });
      } else {
        console.info("allow_load==false 已禁止加载");
        that.setData({
          loading: true,
          content: "——— 再拉裤子就掉了啦 ———"
        })
        setTimeout(function () {
          that.setData({
            loading: false,
          })
        }, 1400)
      }
    };
    if (app.globalData.token && app.globalData.token != null) {
      console.log("couponChannelList init 1");
      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++;
    console.log(that.data.page);
    that.getList(that.data.page);
    console.log("这是第:" + that.data.page);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {}
});