Vue.config.devtools = true;
var v = new Vue({
    el: ".index-box",
    data: {
        cid: -1,
        indexData: {
            recmdTop: [],
        },
        currentPage: 1, // 当前页
        total: 0, // 总数
        pageSizes: [12, 24, 48, 96],
        pageLoaded: false,
        pageSize: 12, // 每页数量
        list: [],
        page: 1,
        size: 10,
        finished: false,
        loading: false,//出现了 上拉到底出发了两次请求接口的情况  加一个变量控制一下
        isLogin: 0,
        memToken: '',
        memHeadImg: '',
        userJf: -1,
        queryQrCode: {},
        consulting: {},
        ad: [],//广告信息
    },
    created: function () {
        if ($.cookie("memToken")) {
            this.memToken = $.cookie("memToken");
            this.isLogin = 1;
        }
        if ($.cookie("memHeadImg")) {
            this.memHeadImg = $.cookie("memHeadImg");
        }
        if (!againWebLogin()) {
            return false;
        }
        this.getIndexData();
        this.liveList();
        this.getQueryQrCode()
        this.getConsulting()
        this.findUserIntegral();
        this.getAdInfo();
    },
    mounted: function () {
        setTimeout(() => {
            this.initFn();
        }, 1000);
        window.addEventListener("scroll", this.scrollBottom, true);
    },
    methods: {
        /**
         * 分页功能 - 检测当前页变化
         */
        currentPageChange(val) {
            this.pageLoaded = false
            this.currentPage = val
            this.liveList()
        },
        /**
         * 分页功能 - 检测PageSize变化
         */
        pageSizeChange(val) {
            this.pageLoaded = false
            this.pageSize = val
            this.liveList()
        },
        getAdInfo() {
            var that = this;
            $.ajax({
                type: 'POST',
                url: serverHttp + "/course/getAdInfo",
                success: function (res) {
                    if (res.code == 200) {
                        that.ad = res.data;
                    }
                },
                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) {
                }
            });
        },
        initFn() {
            setTimeout(() => {
                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',
                    },

                })
            }, 1000);
        },
        gotoRcmd(obj) {
            gotoRcmdPub(obj.zqFlag, obj.rcmdType, obj.rcmdObjId, obj.rcmdUrl);
        },
        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;
            }
            that.loading = true;
            if (that.start == 1) {
                that.list = []
            }
            $.ajax({
                type: 'POST',
                url: serverHttp + "/live/livepage",
                data: {
                    cId: that.cid,
                    page: that.currentPage,
                    size: that.pageSize
                },
                success: function (data) {
                    that.loading = false;
                    that.pageLoaded = true
                    if (data.code == 200) {
                        if (data.data.results.length > 0) {
                            that.list = data.data.results;
                            that.total = data.data.total
                            // 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=&liveId=' + liveId;
        },
        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.liveList();
            }
        },
        // 查询社群二维码
        getQueryQrCode() {
            var that = this;
            $.ajax({
                type: 'POST',
                url: "/user/getQrImg",
                data: {
                    queryQrCode: that.queryQrCode
                },
                success: function (data) {
                    if (data.code == 200) {
                        // that.queryQrCode = data.data
                        that.queryQrCode = [
                            {
                                "id": 1,
                                "name": "赋能全科医生交流群",
                                "qrImg": "/web/img/fnqkysjl.png",
                                "stauts": 0
                            }
                        ]
                    }
                },
                error: function (data) {
                }
            });
        },

        // 咨询推荐
        getConsulting() {
            var that = this;
            $.ajax({
                type: 'POST',
                url: "/web/index/information",
                data: {
                    consulting: that.consulting
                },
                success: function (data) {
                    if (data.code == 200) {
                        that.consulting = data.data[1].rcmdData
                        console.log(that.consulting)
                    }
                },
                error: function (data) {
                }
            });
        }
    },
})
