var page = 1;

function getData() {
  _vp.homeAllRecord(
    {
      type: 2,
      page: page,
      number: 10
    },
    function(res) {
      if(res.data.length==0){
        $('.goBtn').show();
      }else {
        $('.goBtn').hide();
      }
      console.log(res);
      var ele = $(".home_kj_list");
      var result = res.data;
      ele.html("");
      for (var i = 0; i < result.length; i++) {
        ele.append(
        	"<ul>"+	
        		"<li>"+
        			"<div class='awardlist-single-all'>"+
	        			"<div class='awardlist-img'>"+
	        				"<img src="+ result[i].product_image +" />"+
	        			"</div>"+
	        			"<div class='awardlist-details'>"+
	        				"<div class='awardlist-mess-all'>"+
	        					"<div class='awardlist-name ellipsis-1'>"+ result[i].product_name +"</div>"+
	        					"<div class='awardlist-price ellipsis-1'>￥"+ result[i].price +"</div>"+
	        				"</div>"+
	        					
	        				"<div class='awardlist-phase'>期号："+ result[i].issue +"</div>"+
	        				"<div class='div_djs' id='t"+ i +"'></div>"+
	        			"</div>"+
	        		"</div>"+
        		"</li>"+
        	"</ul>"	
       /*   "<ul>" +
            '<img src="' +
            result[i].product_image +
            '" />' +
            "<li><h2>" +
            result[i].product_name +
            "</h2>" +
            "<p><span>期</span>号： " +
            result[i].issue +
            "</p>" +
            '<div class="div_djs" id="t' +
            i +
            '"></div></li>' +
            "</ul>"*/
        );
        addTimer("t" + i, result[i].finish_time);
      }
    }
  );
}

function getMore() {
  page++;
  _vp.homeAllRecord(
    {
      type: 2,
      page: page,
      number: 10
    },
    function(res) {
      if (res.data.length == 0) {
        page--;
        return;
      }
      var ele = $(".home_kj_list");
      var result = res.data;
      ele.html("");
      for (var i = 0; i < result.length; i++) {
        if (result[i].state == 1) {
          ele.append(
        	"<ul>"+	
          		"<li>"+
          			"<div class='awardlist-single-all'>"+
  	        			"<div class='awardlist-img'>"+
  	        				"<img src="+ result[i].product_image +" />"+
  	        			"</div>"+
  	        			"<div class='awardlist-details'>"+
	  	        			"<div class='awardlist-mess-all'>"+
		    					"<div class='awardlist-name ellipsis-1'>"+ result[i].product_name +"</div>"+
		    					"<div class='awardlist-price ellipsis-1'>￥"+ result[i].price +"</div>"+
		    				"</div>"+
  	        				"<div class='awardlist-phase'>期号："+ result[i].issue +"</div>"+
  	        				"<div class='div_djs' id='t"+ i +"'></div>"+
  	        			"</div>"+
  	        		"</div>"+
          		"</li>"+
          	"</ul>"	
        		  
        		  
           /* "<ul>" +
              '<img src="' +
              result[i].product_image +
              '" />' +
              "<li><h2>" +
              result[i].product_name +
              "</h2>" +
              "<p><span>期</span>号： " +
              result[i].issue +
              "</p>" +
              '<div class="div_djs" id="t' +
              i +
              '"></div></li>' +
              "</ul>"*/
          );
          addTimer("t" + i, result[i].finish_time);
        } else {
          var finishTime = timestampToTime(result[i].finish_time);
          ele.append(
            "<ul>" +
              '<img src="' +
              result[i].product_image +
              '" />' +
              "<li><h2>" +
              result[i].product_name +
              "</h2>" +
              "<p><span>期</span>号：" +
              result[i].issue +
              "</p>" +
              "<p><em>获奖</em>者： <label>潇洒哥</label>（本期参与" +
              result[i].need_number +
              "人次）</p>" +
              "<p>幸运号码： <label>" +
              result[i].winning_number +
              "</label></p>" +
              "<p>参与人次： 1</p>" +
              "<p>开奖时间： " +
              finishTime +
              "</p></li>" +
              "</ul>"
          );
        }
      }
    }
  );
}

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

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

//多个倒计时
var addTimer = (function() {
  var list = [],
    interval;

  return function(id, timeStamp) {
    if (!interval) {
      interval = setInterval(go, 1);
    }
    list.push({ ele: document.getElementById(id), time: timeStamp });
  };

  function go() {
    for (var i = 0; i < list.length; i++) {
      list[i].ele.innerHTML = changeTimeStamp(list[i].time);
      if (!list[i].time) list.splice(i--, 1);
    }
  }

  //传入unix时间戳，得到倒计时
  function changeTimeStamp(timeStamp) {
    var distancetime = timeStamp - new Date().getTime();
    if (distancetime > 0) {
      //如果大于0.说明尚未到达截止时间
      var ms = Math.floor(distancetime % 1000);
      var sec = Math.floor((distancetime / 1000) % 60);
      var min = Math.floor((distancetime / 1000 / 60) % 60);
      var hour = Math.floor((distancetime / 1000 / 60 / 60) % 24);

      if (ms < 100) {
        ms = "0" + ms;
      }
      if (sec < 10) {
        sec = "0" + sec;
      }
      if (min < 10) {
        min = "0" + min;
      }
      if (hour < 10) {
        hour = "0" + hour;
      }
      var html =
        "<h3>开奖倒计时：</h3><span>" +
        hour +
        "</span><em>:</em><span>" +
        min +
        "</span><em>:</em><span>" +
        sec +
        "</span>";
      // return hour + ":" +min + ":" +sec ;
      return html;
    } else {
      //若否，就是已经到截止时间了
      var html ="<h3>开奖倒计时：</h3><span>00</span><em>:</em><span>00</span><em>:</em><span>00</span>";
      // getData();
        $('.goBtn').show();
      return html;
    }
  }
})();
