Vue.config.devtools = true;
var v = new Vue({
    el: ".container",
    data: {
        list: [],
        cata: [],
        oneCata: [],
        twoCata: [],
        threeCata: [],
        start: 1,
        size: 12,
        finished: false,
        loading: false,//出现了 上拉到底出发了两次请求接口的情况  加一个变量控制一下
        active: -1,
        ad: [],//广告信息
        isLogin: 0,
        memToken: '',
        memHeadImg: '',
        userJf: -1,
        chooseItem: false,
        sortBy: '',
        isShowNewest: true,
        total: 0,
        pageLoaded: false
    },
    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.getCataList(1, 1);
        this.getAdInfo();
        this.findUserIntegral();
        // 滚动加载
        // window.addEventListener("scroll", this.scrollBottom, true);
    },
    mounted() {
        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);
    },
    methods: {
        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) {
                }
            });
        },
        getIndexData(id) {
            //id：分类id id = -1 是全部分类
            var that = this;
            if (that.loading) {
                return;
            }
            that.loading = true;
            if (that.start == 1) {
                that.list = []
            }
            $.ajax({
                type: 'POST',
                url: serverHttp + "/course/getIndexData",
                data: {
                    from: 'web',
                    cataId: that.active,
                    start: that.start,
                    size: that.size,
                    fId: -1,
                    memToken: that.memToken,
                },
                success: function (data) {
                    if (data.code == 200) {
                        // that.list = that.list.concat(data.data.results);
                        that.list = data.data.results;
                        // 请求的数据未加载完成时
                        that.total = data.data.total
                        that.loading = false;


                    } else if (data.code == 10005) {
                        that.finished = true;
                    }
                    that.pageLoaded = true
                },
                error: function (data) {
                    that.pageLoaded = true


                }
            });
        },
        coursePrefectureList(i) {
            window.location.href = serverHttp + "/course/courseDetails?from=H5&id=" + this.list[i].id;
        },
        webcoursePrefectureList(i) {
            const type = this.list[i].zq;
            console.log(type);
            if (type == 1) {
                //普通课程
                window.location.href = serverHttp + "/course/courseDetails?from&id=" + this.list[i].id;
            } else {
                //专区
                window.location.href = serverHttp + "/course/coursePrefectureList?from&id=" + this.list[i].id;
            }
        },
        // 滚动加载
        // 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) {
        //         that.getIndexData();
        //     }
        // },

        hideCata() {
            this.chooseItem = false;
        },
        confirm() {
            var that = this;
            that.loading = false;
            that.start = 1;
            that.getIndexData();
        },
        chooseCata(i, type) {
            if (type == 1) {
                //取二级菜单
                var that = this;
                for (var item of that.oneCata) {
                    if (item.active == 1) {
                        item.active = 0
                    }
                }
                that.twoCata = [];
                that.oneCata[i].active = 1;
                that.getCataList(that.oneCata[i].id, 2);
                var cataId = that.oneCata[i].id;
                that.active = cataId;
            } else if (type == 2) {
                //取三级级菜单
                var that = this;
                for (var item of that.twoCata) {
                    if (item.active == 1) {
                        item.active = 0
                    }
                }
                that.threeCata = [];
                that.twoCata[i].active = 1;
                that.getCataList(that.twoCata[i].id, 3);
                var cataId = that.twoCata[i].id;
                that.active = cataId;
            } else {
                //三级直接查
                var cataId = that.threeCata[i].id;
                that.active = cataId;
            }
        },
        getCataList(fId, type) {
            var that = this;
            $.ajax({
                type: 'POST',
                url: serverHttp + "/course/getCataListForThreeLevel",
                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
                            } else {
                                item[i].active = 0;
                            }
                        }
                        if (type == 1) {
                            that.cata = item;
                            that.oneCata = item;
                        }
                        if (type == 2) {
                            that.twoCata = item;
                        }
                        if (type === 3) {
                            if (item.length !== 0) {
                                that.threeCata = item;
                                that.tagChanged(item[0].id);
                            } else {
                                that.tagChanged(fId);
                            }
                        }

                    }

                },
                error: function (data) {

                }
            });
        },
        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) {
                }
            });
        },
        chooseTab(i) {
            var that = this;
            for (var item of that.cata) {
                if (item.active == 1) {
                    item.active = 0
                }
            }
            that.cata[i].active = 1;
            var cataId = that.cata[i].id;
            that.active = cataId;
            that.loading = false;
            that.start = 1;
            that.twoCata = [];
            that.threeCata = [];
            that.getIndexData();
        },
        chooseWebTab(i) {
            this.chooseCata(i, 1);
            var that = this;
            for (var item of that.cata) {
                if (item.active == 1) {
                    item.active = 0
                }
            }
            that.cata[i].active = 1;
            var cataId = that.cata[i].id;
            that.active = cataId;
            that.loading = false;
            that.start = 1;
            that.twoCata = [];
            that.threeCata = [];
            that.getWebIndexData(i);
        },
        getWebIndexData(id) {

            // console.log(id)
            //id：分类id id = -1 是全部分类
            this.start = 1
            var that = this;
            if (id == 222222) {
                that.sortBy = 'hottest'
                this.isShowNewest = false
            } else if (id = 111111) {
                that.sortBy = 'newest'
                this.isShowNewest = true
            }
            if (that.loading) {
                return;
            }
            that.loading = true;
            // if (that.start == 1) {
            //     that.list = []
            // }
            $.ajax({
                type: 'POST',
                url: serverHttp + "/course/getIndexData",
                data: {
                    from: 'web',
                    cataId: that.active,
                    start: that.start,
                    size: that.size,
                    fId: -1,
                    memToken: that.memToken,
                    sortBy: that.sortBy
                },
                success: function (data) {
                    if (data.code == 200) {
                        that.list = data.data.results;
                        that.total = data.data.total
                        // 请求的数据未加载完成时
                        that.loading = false;
                    } else if (data.code == 10005) {
                        that.finished = true;
                        that.loading = false;
                        that.total = 0;
                        that.list = []
                    }

                },
                error: function (data) {

                }
            });
        },
        chooseWebCata(i, type) {

            // console.log(i, type)
            if (type == 1) {
                //取二级菜单
                var that = this;
                for (var item of that.oneCata) {
                    if (item.active == 1) {
                        item.active = 0
                    }
                }
                that.twoCata = [];
                that.oneCata[i].active = 1;
                that.getCataList(that.oneCata[i].id, 2);
                var cataId = that.oneCata[i].id;
                that.active = cataId;
            } else if (type == 2) {
                //取三级级菜单
                var that = this;
                for (var item of that.twoCata) {
                    if (item.active == 1) {
                        item.active = 0
                    }
                }
                that.threeCata = [];
                that.twoCata[i].active = 1;
                that.getCataList(that.twoCata[i].id, 3);
                var cataId = that.twoCata[i].id;
                that.active = cataId;
            } else if (type == 3) {
                //取三级级菜单
                var that = this;
                for (var item of that.threeCata) {
                    if (item.active == 1) {
                        item.active = 0
                    }
                }
                that.threeCata[i].active = 1;
                var cataId = that.threeCata[i].id;
                that.tagChanged(cataId);
                //三级直接查
                // var cataId = that.threeCata[i].id;
                // that.active = cataId;
                // that.getCataList(that.threeCata[i].id,4);
            }
        },
        tagChanged(cataId) {
            this.active = cataId;
            this.loading = false;
            this.getWebIndexData();
        },
        gotoAdHtml(url) {
            window.location.href = url;
        },
        showChooseItem() {
            if (this.chooseItem) {
                this.chooseItem = false;
            } else {
                this.chooseItem = true;
            }
        },
        handleSizeChange(val) {
            this.page = val
            this.getIndexData()
        },
        handleCurrentChange(val) {
            this.start = val
            this.getIndexData()
        },
    }
})
