Vue.config.devtools = true;
var v = new Vue({
    el:".container",
    data:{
        cata:[
            {name:"目录",active:1,id:1},
            {name:"详情",active:0,id:2},
            // {name:"互动",active:0,id:3},
        ],
        list:[],
        info:{},
        start:1,
        size:10,
        finished:false,
        loading:false,//出现了 上拉到底出发了两次请求接口的情况  加一个变量控制一下
        ad:[],//广告信息
        zqId:-1,
        isLogin: 0,
        memToken: '',
        memHeadImg: '',
        userJf:-1,
        toast:false,
        url:"https://www.qkyxzx.com/course/coursePrefectureList?from=web",
        integralType:"",
        integralCount:"",
        attestation: {},
    },
    created:function (){
        if($.cookie("memToken")){
            this.memToken = $.cookie("memToken");
            this.isLogin=1;
        }
        if($.cookie("memHeadImg")){
            this.memHeadImg = $.cookie("memHeadImg");
        }
        this.zqId = $("#id").val();
        this.getIndexData();
        this.getAdInfo();
        this.findUserBaseInfo();
        this.findUserIntegral();
        this.getAttestation();
        if (typeof (fromlogin) != "undefined" && fromlogin == "1") {
            jifen();
        }
    },
    mounted (){
        var that = this;
        window.addEventListener('pageshow', function(event) {
            if(event.persisted) { // ios 有效, android 和 pc 每次都是 false
                setTimeout(() =>{
                    that.findUserBaseInfo();
                }, 500);
            } else { // ios 除外
                if(sessionStorage.getItem('refresh') === 'true') {
                    setTimeout(() =>{
                        that.findUserBaseInfo();
                    }, 500);
                }
            }
            sessionStorage.removeItem('refresh');
        });
        setTimeout(() => {
            var mySwiper = new Swiper('.swiper-container', {
                direction: 'horizontal', // 水平切换选项
                loop: true, // 循环模式选项
                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);
        window.addEventListener("scroll", this.scrollBottom, true);
        setTimeout(() => {
            $(".loading-item").fadeOut(200);
        },500)
    },
    methods:{
        getAttestation() {
            var that = this;
            $.ajax({
                type : 'POST',
                url : serverHttp+"/popu/window/checkWindow",
                data : {
                    id: that.zqId,
                    idType: 3,
                    memToken: that.memToken
                },
                success : function(data) {
                    if (data.code == 200) {
                        that.attestation = data.data;
                    }
                },
                error : function(data) {
                }
            });
        },
        copy (){
            var aux = document.createElement("input");
            aux.setAttribute('readonly', 'readonly');
            aux.value = this.url+"&id="+this.zqId;
            document.body.appendChild(aux);
            aux.setSelectionRange(0, 9999);
            aux.select();
            document.execCommand("copy");
            document.body.removeChild(aux);
            this.toast = false;
        },
        showTast (){
            this.toast = true;
        },
        closeToast (){
            this.toast = false;
        },
        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) {
                }
            });
        },
        toPay(){
            if(!checkLogin()){
                // goLogin();
                return false;
            }
            var that = this;
            if(!that.parseRule()){
                return false;
            }
            toPay(1, that.info.id, 1,'');
        },
        addFreeBuyApi(){
            if(!checkLogin()){
                // goLogin();
                return false;
            }
            var that = this;
            if(!that.parseRule()){
                return false;
            }
            $.ajax({
                type : 'POST',
                url : serverHttp+"/pay/addFreeBuyApi",
                data : {
                    memToken:that.memToken,
                    orderType: 1,
                    objId: that.info.id,
                    ispackage: 1,
                    paySource: 3,
                    fromMemCard: $.cookie("fromMemCard")
                },
                success : function(data) {
                    if (data.code == 200) {
                        that.info.isBuy = 1;
                        //that.toPlay();
                        that.integralType = "领取课程";
                        that.integralCount=10;
                        $(".integral-toast").show();
                        playMusic()
                        setTimeout(function (){
                            $(".integral-toast").fadeOut(200);
                        },2000);

                    }else if(data.code == 10008){
                        //去登录
                        goLogin();
                    }else{
                        alert(data.data.message);
                    }
                },
                error : function(data) {
                }
            });
        },
        parseRule(){
            var that = this;
            if (that.attestation.attestType == 2) {
                window.location.href = serverHttp + "/user/certificationSm?from=H5&memToken=&type=3&id=" + that.zqId;
                return false;
            } else if (that.attestation.attestType == 1) {
                window.location.href = serverHttp + "/user/certificationYs?from=H5&memToken=&type=3&id=" + that.zqId;
                return false;
            } else if (that.attestation.attestType == 4) {
                window.location.href = serverHttp + "/user/certificationSmojl?from=H5&memToken=&type=3&id=" + that.zqId;
                return false;
            } else if (that.attestation.attestType == 3) {
                window.location.href = serverHttp + "/user/certificationYsojl?from=H5&memToken=&type=3&id=" + that.zqId;
                return false;
            } else if (that.attestation.attestType == 5) {
                return true;
            }
            return true;
        },
        findUserBaseInfo(){
            var that = this;
            $.ajax({
                type : 'POST',
                url : serverHttp+"/user/findUserBaseInfo",
                data : {
                    memToken:that.memToken
                },
                success : function(data) {
                    that.memberInfo=data.data;
                },
                error : function(data) {
                }
            });
        },
        getIndexData () {
            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: 'H5',
                    cataId:-1,
                    start: that.start,
                    size: that.size,
                    fId:that.zqId
                },
                success : function(data) {
                    if (data.code == 200) {
                        that.list = that.list.concat(data.data.list);
                        if (that.start == 1) {
                            that.info = data.data.info;
                            $(document).attr("title", that.info.title);
                        }
                        // 请求的数据未加载完成时
                        that.start = that.start + 1;
                        that.loading = false;

                        $("meta[name='sharetitle']").attr("content",that.info.shareTitle);
                        $("meta[name='sharedesc']").attr("content",that.info.shareDesc);
                        $("meta[name='sharepic']").attr("content",that.info.sharePic);
                        // $("meta[name='shareurl']").attr("content",that.info.shareUrl);
                    } else if (data.code == 10005) {
                        that.finished = true;
                    }
                },
            });
        },
        courseDetails (id) {
            //普通课程
            window.location.href = serverHttp + "/course/courseDetails?from=H5&id="+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();
            }
        },
        getAdInfo () {
            var that = this;
            $.ajax({
                type : 'POST',
                data:{
                    id: that.zqId,
                },
                url : serverHttp+"/course/getAdInfoNew",
                success : function(res) {
                    if (res.code == 200) {
                        that.ad = res.data;
                    }
                },
                error : function(data) {
                }
            });
        },
        chooseTab (i){
            const that = this;
            for (var item of that.cata) {
                if (item.active == 1) {
                    item.active = 0
                }
            }
            that.cata[i].active = 1;
        },
        gotoAdHtml (url){
            window.location.href = url;
        }

    }
})
