Vue.config.devtools = true;
var v = new Vue({
    el:".container",
    data:{
        list: [],
        isLogin: 0,
        memToken: '',
        memHeadImg: '',
        userJf:-1,
        chooseItem:false,
        search:"",
        loading: true,
        size: 12,
        totalCount: 0,
        start: 1,
    },
    created:function (){
        this.findUserIntegral();
        var myobj = getparamcommon();
        this.search = decodeURI(myobj.searchVal);
        if(sessionStorage.getItem('searchTitleweb')){
            this.search = sessionStorage.getItem('searchTitleweb');
        }
        if(this.search){
            this.searchFn()
        }

        if (window.history){
            // 支持History API
            history.replaceState(null,"",'/index/search?from=web&searchVal=')
        }
    },
    mounted:function (){
        setTimeout(() => {
            $(".loading-item").fadeOut(200);
        },500);
        // window.addEventListener("scroll", this.scrollBottom, true);
    },
    methods:{
        toObjInfo (obj){
            if(obj.obj_type == 1){
                // window.location.href = "/course/courseDetails?from=web&id="+obj.obj_id;
                if (obj.zq_flag == 0) {
                    //普通课程
                    window.location.href = serverHttp + "/course/courseDetails?from=web&id="+obj.obj_id;
                } else {
                    //专区
                    window.location.href = serverHttp + "/course/coursePrefectureList?from=web&id="+obj.obj_id;
                }
            }else if(obj.obj_type == 2){
                window.location.href = "/course/courseDetails?from=web&id="+obj.obj_pid;
            }else if(obj.obj_type == 3){
                window.location.href = "/live/detail?from=web&liveId="+obj.obj_id;
            }else if(obj.obj_type == 4){
                window.location.href = "/view/detail?from=web&artId="+obj.obj_id;
            }else if(obj.obj_type == 5){
                window.location.href = "/meeting/details?from=web&conferId="+obj.obj_id;
            }else if(obj.obj_type == 6){
                window.location.href = "/brand/detail?from=web&productId="+obj.obj_id;
            }
        },
        toSearch (){
            var that = this;
            if(!that.search || !that.search.trim()){
                that.list=[];
                return false;
            }
            sessionStorage.setItem('searchTitleweb', that.search)
            // const url = window.location
            // const href = window.location.href;
            // const newUrl = href.split('&searchVal=')[0] + '&searchVal=' + that.search;
            // url.href = newUrl;
            // history.pushState('', '', newUrl); // 不刷新页面

            that.page = 1;
            that.searchFn();
        },
        searchFn () {
            var that = this;
            if(that.page == 1){
                that.list = []
            }
            $.ajax({
                type : 'POST',
                url : "/index/search",
                data : {
                    searchKw: that.search,
                    start: that.start,
                    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 = data.data.items;
                            that.totalCount=data.data.total;
                        }else {
                            that.totalCount=0;
                        }
                    }
                }
            });
        },
        findUserIntegral (){
            var that = this;
            $.ajax({
                type : 'POST',
                url : "/user/findUserIntegral",
                data : {
                    memToken:that.memToken
                },
                success : function(data) {
                    if (data.code == 200) {
                        that.userJf = data.data;
                    }
                },
                error : function(data) {
                }
            });
        },
        handleSizeChange(val) {
            this.page = val
            this.searchFn()
        },
        handleCurrentChange(val) {
            console.log(val);
            this.start = val
            this.searchFn()
        }
    }
})
