var page = 1;
var toast = new auiToast();

function getData() {
  toast.loading({
    title: "加载中",
    duration: 2000
  });
  _vp.homeAllRecord(
    {
      type: 3,
      page: page,
      number: 10
    },
    function(res) {
      toast.hide();
      console.log(res);
      if (res.status == 1) {
        var ele = $(".clearfixd");
        var result = res.data;
        ele.html("");
        for (var i = 0; i < result.length; i++) {
          var needNum = result[i].need_number;
          var joinedNum = result[i].participate_number;
          var percent = ((joinedNum / needNum)*100).toFixed(2);
          ele.append(
            "<ul>" +
              '<li onclick="test(&quot;CommodityDetails.html?id=' +
              result[i].id +
              '&quot;)"><img src="' +
              result[i].product_image +
              '" /></li><dl><dt class="ellipsis-2">' +
              result[i].product_name +
              '</dt><dd><span><em style="width:' +
              percent +
              '%;"></em></span><a href="CommodityDetails.html?id=' +
              result[i].id +
              '">立即夺宝</a>' +
              "<p>开奖进度 <em>" +
              percent +
              "%</em></p></dd></dl>" +
              "</ul>"
          );
        }
        $(".home_cp_bot").hide();
      } else if (res.status == -1) {
        toast.fail({
          title: res.msg,
          duration: 2000
        });
      } else {
        toast.fail({
          title: "服务器异常",
          duration: 2000
        });
      }
    }
  );
}

function getMore() {
  toast.loading({
    title: "加载中",
    duration: 2000
  });
  page++;
  $(".home_cp_bot").show();
  _vp.homeAllRecord(
    {
      type: 3,
      page: page,
      number: 10
    },
    function(res) {
      toast.hide();
      if (res.data.length == 0) {
        page--;
        return;
      }
      var ele = $(".clearfixd");
      var result = res.data;
      for (var i = 0; i < result.length; i++) {
        var needNum = result[i].need_number;
        var joinedNum = result[i].participate_number;
        var percent = ((joinedNum / needNum)*100).toFixed(2);
        ele.append(
          "<ul>" +
            '<li onclick="test(&quot;CommodityDetails.html?id=' +
            result[i].id +
            '&quot;)"><img src="' +
            result[i].product_image +
            '" /></li>' +
            "<dl><dt class='ellipsis-2'>" +
            result[i].product_name +
            '</dt><dd><span><em style="width:' +
            percent +
            '%;"></em></span><a href="CommodityDetails.html?id=' +
            result[i].id +
            '">立即夺宝</a>' +
            "<p>开奖进度 <em>" +
            percent +
            "%</em></p></dd></dl>" +
            "</ul>"
        );
      }
      $(".home_cp_bot").hide();
    }
  );
}

window.onscroll = function() {
  //监听事件内容
  if (getScrollHeight() == getWindowHeight() + getDocumentTop()) {
    //当滚动条到底时,这里是触发内容
    //异步请求数据,局部刷新dom
    getMore();
  }
};

$(function() {
  getData();
});
