Vue.config.devtools = true;
var v = new Vue({
    el:".index-box",
    data:{
        cata:[
            {name:"全部",active:1,id:-1},
            {name:"课程",active:0,id:1},
            {name:"直播",active:0,id:2},
            {name:"会议",active:0,id:3},
            {name:"课件",active:0,id:4},
        ],
        list:[],
        isLogin: 0,
        memToken: '',
        memHeadImg: '',
        userJf:-1,
        start:1,
        size:10,
        finished:false,
        loading:false,//出现了 上拉到底出发了两次请求接口的情况  加一个变量控制一下
        type:-1,
    },
    created:function (){
        if (!checkWebLogin()) {
            // $('.navlogorebox').trigger('click')
            return false;
        }
        // if(!checkLogin()){
        //     goLogin();
        //     return false;
        // }
        var that = this;
        // if($.cookie("memToken")){
        //     that.memToken = $.cookie("memToken");
        //     that.isLogin=1;
        // }
        // if($.cookie("memHeadImg")){
        //     that.memHeadImg = $.cookie("memHeadImg");
        // }
        that.findUserOrderList(-1);
        // that.findUserIntegral();
        // window.addEventListener("scroll", this.scrollBottom, true);
    },
    mounted:function (){
        setTimeout(() => {
            $(".loading-item").fadeOut(200);
        },500)
    },
    methods:{
        gotoDetails (item){
            if(item.status != 1){
                layer.msg('当前商品已下架！')
                return false;
            }
            if (item.orderType == 1) {
                //课程item.qkjTqcourse
                if (item.qkjTqcourse.zq == 1) {
                    //普通课程
                    window.location.href = serverHttp + "/course/courseDetails?from=web&id=" +item.qkjTqcourse.id;
                } else {
                    //专区
                    window.location.href = serverHttp + "/course/coursePrefectureList?from=web&id=" +item.qkjTqcourse.id;
                }
            } else if (item.orderType == 2) {
                //直播item.qkjLive.liveCoverImg
                window.location.href = '/live/detail?from=web&liveId='+item.qkjLive.id;
            } else if (item.orderType == 3) {
                //会议item.qkjConference.coverImg
                window.location.href= "/meeting/details?from=web&conferId="+item.qkjConference.id;
            }
        },
        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) {
                }
            });
        },
        findUserOrderList (type){
            var that = this;
            // if (that.loading) {
            //     return ;
            // }
            // that.loading = true;
            // if(that.start == 1){
            //     that.list = []
            // }
            let url = '/user/findUserAllOrderListWeb' //全部
            if (type > 0){
                url = '/user/findUserOrderListWeb' //分类
            }
            $.ajax({
                type : 'POST',
                url,
                data : {
                    start:that.start,
                    size:that.size,
                    type:type,
                },
                success:function (data) {
                    if (data.code == 200) {
                        that.list = that.list.concat(data.data);
                        console.log(that.list);
                        // 请求的数据未加载完成时
                        // that.start = that.start + 1;
                        // that.loading = false;
                    } else if (data.code == 10005) {
                        // that.finished = true;
                        // that.loading = false;
                    }
                }
            })
        },
        chooseTab (i){
            const that = this;
            for (var item of that.cata) {
                if (item.active == 1) {
                    item.active = 0
                }
            }
            that.cata[i].active = 1;
            that.start = 1;
            if (i == 0) {
                i = -1;
            }
            that.list = [];
            that.type = i;
            that.findUserOrderList(i);
        },
        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) {
                var type = that.type;
                that.findUserOrderList(type);
            }
        },
    }
})
