Vue.config.devtools = true;
var v = new Vue({
    el:".container",
    data:{
        list:[],
        isLogin: 0,
        memToken: '',
        memHeadImg: '',
        userJf:-1,
        chooseItem:false,
        search:"",
        loading: false,
        size: 10,
        page: 1,
        totalCount:0
    },
    created:function (){
        if($.cookie("memToken")){
            this.memToken = $.cookie("memToken");
            this.isLogin=1;
        }
        if($.cookie("memHeadImg")){
            this.memHeadImg = $.cookie("memHeadImg");
        }
        this.findUserIntegral();
        if(sessionStorage.getItem('searchTitle')){
            this.search = sessionStorage.getItem('searchTitle');
        }
        if(this.search){
            this.searchFn()
        }
    },
    mounted:function (){
        setTimeout(() => {
            $(".loading-item").fadeOut(200);
        },500);
        window.addEventListener("scroll", this.scrollBottom, true);
    },
    methods:{
        scrollBottom() {
            var that = this;
            let scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
            let clientHeight = document.documentElement.clientHeight;
            let scrollHeight = document.documentElement.scrollHeight;
            let bottomOfWindow = scrollTop + clientHeight >= scrollHeight-4
            console.log(scrollHeight)
            if (scrollTop!=0 && bottomOfWindow && !that.finished) {
                that.searchFn();
            }
        },
        toObjInfo (obj){
            if(obj.obj_type == 1){
                window.location.href = serverHttp+"/course/courseDetails?from=H5&id="+obj.obj_id;
            }else if(obj.obj_type == 2){
                window.location.href = serverHttp+"/course/courseDetails?from=H5&id="+obj.obj_pid;
            }else if(obj.obj_type == 3){
                window.location.href = serverHttp+"/live/detail?from=H5&liveId="+obj.obj_id;
            }else if(obj.obj_type == 4){
                window.location.href = serverHttp+"/view/detail?from=H5&artId="+obj.obj_id;
            }else if(obj.obj_type == 5){
                window.location.href = serverHttp+"/meeting/details?from=H5&conferId="+obj.obj_id;
            }else if(obj.obj_type == 6){
                window.location.href = serverHttp+"/brand/detail?from=H5&productId="+obj.obj_id;
            }
        },
        toSearch (){
            var that = this;
            if(!that.search || !that.search.trim()){
                that.list=[];
                return false;
            }
            sessionStorage.setItem('searchTitle', that.search)
            that.page = 1;
            that.searchFn();
        },
        searchFn () {
            var that = this;
            if (that.loading) {
                return false;
            }
            that.loading = true;
            if(that.page == 1){
                that.list = []
            }
            $.ajax({
                type : 'POST',
                url : serverHttp+"/index/search",
                data : {
                    searchKw: that.search,
                    start: that.page,
                    size: that.size
                },
                success : function(data) {
                    that.loading = false;
                    if (data.code == 200) {
                        if(data.data && data.data.items && data.data.items.length > 0){
                            $.each(data.data.items, function (i,item) {
                                if(item.obj_cover_img.indexOf("http")==-1){
                                    item.obj_cover_img = "http://files.qkyxzx.com/"+item.obj_cover_img;
                                }
                                if(item.obj_type==1 || item.obj_type==2){
                                    item.obj_title = "【课程】"+item.obj_title;
                                }else if(item.obj_type==3){
                                    item.obj_title = "【直播】"+item.obj_title;
                                }else if(item.obj_type==4){
                                    item.obj_title = "【资讯】"+item.obj_title;
                                }else if(item.obj_type==5){
                                    item.obj_title = "【会议】"+item.obj_title;
                                }else if(item.obj_type==6){
                                    item.obj_title = "【品牌】"+item.obj_title;
                                }
                            })
                            that.list = that.list.concat(data.data.items);
                            that.totalCount=data.data.total;
                            // 请求的数据未加载完成时
                            that.page = that.page + 1;
                        }else {
                            that.totalCount=0;
                        }
                    }
                }
            });
        },
        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) {
                }
            });
        }

    }
})
