Vue.config.devtools = true;
var v = new Vue({
	el:".container",
	data:{
		cid:-1,
		indexData:{
			recmdTop: [],
		},
		list:[],
		page:1,
		size:10,
		finished:false,
        loading:false,//出现了 上拉到底出发了两次请求接口的情况  加一个变量控制一下
        isLogin: 0,
        memToken: '',
        memHeadImg: '',
        userJf:-1,
		integralType:"",
		integralCount:""
	},
    created:function (){
        if($.cookie("memToken")){
            this.memToken = $.cookie("memToken");
            this.isLogin=1;
        }
        if($.cookie("memHeadImg")){
            this.memHeadImg = $.cookie("memHeadImg");
        }
		if(!againLogin()){
			// goLogin();
			return false;
		}
		this.getIndexData();
		this.liveList();
        this.findUserIntegral();
	},
	mounted: function (){
		setTimeout(() => {
			this.initFn();
		}, 1000);
		window.addEventListener("scroll", this.scrollBottom, true);
		setTimeout(() => {
			$(".loading-item").fadeOut(200);
		},500)
	},
	methods:{
        findUserIntegral (){
            var that = this;
            $.ajax({
                type : 'POST',
                url : serverHttp+"/user/findUserIntegral",
                data : {
                    memToken:that.memToken
                },
                success : function(data) {
                    if (data.code == 200) {
                        that.userJf = data.data;
                    }
                },
                error : function(data) {
                }
            });
        },
		initFn(){
			var mySwiper = new Swiper('.swiper-container', {
		        direction: 'horizontal', // 水平切换选项
		        loop: this.indexData.recmdTop.length > 1, // 循环模式选项
		        observerParents: true,
		        observer: true,
				autoplay: {
					disableOnInteraction: false,  //触碰后自动轮播也不会停止
					delay: 2000,
				},
		        // 如果需要分页器
		        pagination: {
		            el: '.swiper-pagination',
		        },
		        // 如果需要前进后退按钮
		        navigation: {
		            nextEl: '.swiper-button-next',
		            prevEl: '.swiper-button-prev',
		        },
		        // 如果需要滚动条
		        scrollbar: {
		            el: '.swiper-scrollbar',
		        },
		    })
		},
		gotoRcmd(obj) {
	    		gotoRcmdPub(obj.zqFlag,obj.rcmdType,obj.rcmdObjId,obj.rcmdUrl,"H5",obj.artType,obj.artContent);
	    },
		getIndexData () {
			var that = this;
			$.ajax({
				type : 'POST',
				url : serverHttp+"/live/getIndexData",
				data : {
					memToken:that.memToken
				},
				success : function(data) {
					if (data.code == 200) {
						that.indexData = data.data.data;
					}
				},
				error : function(data) {
				}
			});
		},
		liveList () {
			var that = this;
			if (that.loading) {
                return false;
            }
            that.loading = true;
            if(that.page == 1){
                that.list = []
            }
			$.ajax({
				type : 'POST',
				url : serverHttp+"/live/livepage",
				data : {
					cId: that.cid,
					page: that.page,
					size: that.size
				},
				success : function(data) {
					that.loading = false;
					if (data.code == 200) {
						if(data.data.results.length > 0){
						    that.list = that.list.concat(data.data.results);
							// that.info = data.data.info;
							// 请求的数据未加载完成时
							that.page = that.page + 1;
						}else{
							that.finished = true;
						}
                    }
				}
			});
		},
		chooseTab (obj){
			const that = this;
			var item = $(obj);
			var tabId = item.attr("tabId");
			console.log(tabId);
			$(".active").removeClass("active");
			item.addClass("active");
			that.cid = item.attr("cid");
			that.page = 1;
			that.liveList();
		},
		gotoDetail (liveId) {
			window.location.href = serverHttp + '/live/detail?from=H5&liveId='+liveId;
		},
		scrollBottom() {
			const that = this;
			// 滚动到页面底部时
			const el = document.getElementById("list");
			const elHeight = $("#list").offset()['top'];//元素距离顶部距离 该计算可能会有一些偏差
			const windowHeight = window.screen.height*750/window.screen.width;
			const scrollTop =
				document.documentElement.scrollTop || document.body.scrollTop;
			const contentHeight = el.clientHeight;
			//316为
			const toBottom = contentHeight - windowHeight - scrollTop-elHeight;
			if (toBottom < 10 && !that.finished) {
				that.liveList();
			}
		}
	}
})
