Vue.config.devtools = true;
var v = new Vue({
    el: ".container",
    data: {
        ad: [],
        cata: [],
        sortData: ['最新', '最热'],
        list: [],
        cataId: -1,
        page: 1,
        size: 10,
        finished: false,
        loading: false,//出现了 上拉到底出发了两次请求接口的情况  加一个变量控制一下
        isLogin: 0,
        memToken: '',
        memHeadImg: '',
        userJf: -1,
        total: 0,
        sortBy: 0, // 最新最热
    },
    created: function () {
        // if (!checkWebLogin()) {
        //     $('.navlogorebox').trigger('click')
        //     return false;
        // }
        var that = this;
        if ($.cookie("memToken")) {
            this.memToken = $.cookie("memToken");
            this.isLogin = 1;
        }
        if ($.cookie("memHeadImg")) {
            this.memHeadImg = $.cookie("memHeadImg");
        }
        if (!againWebLogin()) {
            return false;
        }
        this.getAdInfo();
        that.getCataList(16, 4);
        that.findUserIntegral();
    },
    mounted: function () {
        setTimeout(() => {
            this.initFn();
        }, 1000);
        window.addEventListener("scroll", this.scrollBottom, true);
        setTimeout(() => {
            $(".loading-item").fadeOut(200);
        }, 500)
    },
    methods: {
        gotoArtInfo(obj) {
            if (!againWebLogin()) {
                return false;
            }
            // if (!checkWebLogin()) {
            //     $('.navlogorebox').trigger('click')
            //     return false;
            // }
            if (obj.type == 1) {
                // $.ajax({
                //     type: 'POST',
                //     url: serverHttp + "/user/saveUserDorecWeb",
                //     data: {
                //         oprType: 1,
                //         objType: 1,
                //         objId: obj.id,
                //         source: 2,
                //         url: "/view/detail?from=web&artId=" + obj.id,
                //     },
                //     success: (res) => {
                //         // console.log(res, 'res')
                //         if (res.code == 200) {
                saveUserDorecWeb(1, 1, obj.id, obj.content, 2);
                // playMusic("阅读文章", 5)
                setTimeout(() => {
                    window.location.href = serverHttp + "/view/detail?from=web&artId=" + obj.id;

                }, 2000)
                // }
                // },
                //     error: function (data) {
                //     }
                // });
            } else if (obj.type == 2) {
                saveUserDorecWeb(1, 1, obj.id, obj.content, 2);
                setTimeout(() => {
                    window.location.href = obj.content;
                }, 2000)
            }
        },
        getAdInfo() {
            var that = this;
            $.ajax({
                type: 'POST',
                url: serverHttp + "/view/getAdInfo",
                success: function (res) {
                    if (res.code == 200) {
                        that.ad = res.data;
                    }
                },
                error: function (data) {
                }
            });
        },
        initFn() {
            var mySwiper = new Swiper('.swiper-container', {
                direction: 'horizontal', // 水平切换选项
                loop: this.ad.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',
                },
            })
        },
        chooseTab(i) {
            console.log(i);
            var that = this;
            for (var item of that.cata) {
                if (item.active == 1) {
                    item.active = 0
                }
            }
            that.cata[i].active = 1;
            that.cataId = that.cata[i].id;
            that.loading = false;
            that.page = 1;
            that.total = 0;
            that.finished = false;
            that.list = [];
            that.getArtListData();
        },
        // 切换最新最热
        chooseSort(index) {
            this.loading = false;
            this.page = 1;
            this.total = 0;
            this.sortBy = index
            this.getArtListData();
        },
        getCataList(fId, type) {
            var that = this;
            $.ajax({
                type: 'POST',
                url: serverHttp + "/course/getCataList",
                data: {
                    fId: fId,
                    type: type,
                },
                success: function (res) {
                    if (res.code == 200) {
                        var item = res.data;
                        for (var i = 0; i < item.length; i++) {
                            if (i == 0) {
                                item[i].active = 1
                                that.cataId = item[i].id;
                            } else {
                                item[i].active = 0;
                            }
                        }
                        that.cata = item;
                        that.getArtListData();
                    }
                },
                error: function (data) {

                }
            });
        },
        gotoRcmd(obj) {
            gotoRcmdPub(obj.zqFlag, obj.rcmdType, obj.rcmdObjId, obj.rcmdUrl);
        },
        // handleSizeChange(val) {
        //     this.size = val
        //     this.getArtListData()
        // },
        // handleCurrentChange(val) {
        //     this.page = val
        //     this.getArtListData()
        // },
        getArtListData() {

            var that = this;
            if (that.loading) {
                return;
            }
            that.loading = true;
            if (that.page == 1) {
                that.list = []
            }
            $.ajax({
                type: 'POST',
                url: serverHttp + "/view/getArtListData",
                data: {
                    memToken: that.memToken,
                    cataId: that.cataId,
                    page: that.page,
                    size: that.size,
                    sortBy: that.sortBy == 1 ? 'hottest' : ''
                },
                success: function (data) {
                    if (data.code == 200) {
                        if (data.data.data.length > 0) {
                            that.list = that.list.concat(data.data.data);
                            // 请求的数据未加载完成时
                            that.page = that.page + 1;
                            that.loading = false;
                        } else {
                            that.finished = true;
                        }
                    }
                },
                error: function (data) {
                }
            });
        },
        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) {
                }
            });
        },
        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.getArtListData();
            }
        }
    }
})
