var page = 1;
var type = 1; //1人气推荐 2最新商品 3即将开奖

var toast = new auiToast();

//1人气推荐 2最新商品 3即将开奖
function getGoodList(e) {
  type = e;
  _vp.indexList(
    {
      type: e,
      page: 1,
      number: 20
    },
    function(res) {
      console.log(res);
      if (res.status == 1) {
        var ele = $(".home_cp_box");
        var result = res.data;
        ele.html("");
        for (var i = 0; i < result.length; i++) {
          var allNum = result[i].need_number;
          var joinedNum = result[i].participate_number;
          var percent = ((joinedNum / allNum)*100).toFixed(2);
          ele.append(
            "<ul>" +
              '<a href="CommodityDetails.html?id=' +
              result[i].id +
              '"><li><img src="' +
              result[i].product_image +
              '" /></li></a>' +
              "<dl>" +
              "<dt class='index-mess'>"+
              "<div class='index-name ellipsis-1'>"+ result[i].product_name + "</div>" +
              "<div class='index-price'>￥"+ result[i].price +"</div>"+
              "</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() {
  page++;
  _vp.indexList(
    {
      type: type,
      page: page,
      number: 20
    },
    function(res) {
      if (res.status == 1) {
        if (res.data.length == 0) {
          page--;
          return;
        }
        var ele = $(".home_cp_box");
        var result = res.data;
        for (var i = 0; i < result.length; i++) {
          var allNum = result[i].need_number;
          var joinedNum = result[i].participate_number;
          var percent = ((joinedNum / allNum)*100).toFixed(2);
          ele.append(
            "<ul>" +
              '<a href="CommodityDetails.html?id=' +
              result[i].id +
              '"><li><img src="' +
              result[i].product_image +
              '" /></li></a>' +
              "<dl>" +
              "<dt class='index-mess'>"+
              "<div class='index-name ellipsis-1'>"+ result[i].product_name + "</div>" +
              "<div class='index-price'>￥"+ result[i].price +"</div>"+
              "</dt>" +
             /* "<dt>" +
              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 getImg() {
  _vp.carouselMap({}, function(res) {
    var ele = $(".aui-slide-wrap");
    var result = res.data;
    console.log(result)
    ele.html('');
    for (var i = 0; i < result.length; i++) {
      var href;
      if (result[i].state == 2) {
        href = "CommodityDetails.html?id=" + result[i].product_id;
      } else if (result[i].state == 1) {
        href = "#";
      }
      ele.append(
        '<div class="aui-slide-node bg-dark"><a href="' + href + '"><img src="' + result[i].image + '" /></a></div>'
      );
    }
    var slide = new auiSlide({
      container: document.getElementById("topimg"), //容器
      // "width":300, //宽度
      height: 240, //高度
      speed: 500, //速度
      autoPlay: 3000, //自动播放
      loop: true, //是否循环
      pageShow: true, //是否显示分页器
      pageStyle: "dot", //分页器样式，分dot,line
      dotPosition: "center" //当分页器样式为dot时控制分页器位置，left,center,right
    });
  });
}

function getWord(){
  _vp.indexList(
    {
      type: 4,
      page: 1,
      number: 20
    },
    function(res) {
      var ele = $(".hezi");
      var result = res.data;
      for(var i=0;i<res.data.length;i++){
        ele.append(
          '<p>恭喜<span>'+result[i].lucky_number+'</span>获得'+result[i].product_name+'</p>'
        )
      }
      var length = res.data.length;
      var num=0;
      setInterval(function(){
        num++;
        if(num==length){
          $('.hezi').css({'top':'0px'});          
          num=0;
        }
        $('.hezi').stop().animate({'top':-32*num+'px'},1500)
       /* $('.hezi').stop().animate({'top':-0.6*num+'rem'},1500)*/
      },5000)
    }
  )
}

$(function() {
  getGoodList(1);
  getImg();
  getWord();
});

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