Vue.config.devtools = true;

var v = new Vue({
    el: ".container",
    data: function () {
        return {
            isVip: localStorage.getItem('isVip') == 1,
            cata: [
                {name: "目录", active: 1, id: 1},
                {name: "课程详情", active: 0, id: 2},
                {name: "互动", active: 0, id: 3},
                {name: "", active: 0, id: 4},
            ],
            memberInfo: {
                isPurchased: 0,
                isRemind: 0,
                joinMembers: 0,
                remindMemberNum: 0
            },
            list: [],
            cataList: [],
            comments: [],
            info: {},
            start: 1,
            size: 10,
            finished: false,
            loading: false,//出现了上拉到底触发了两次请求接口的情况  加一个变量控制一下
            ad: [],//广告信息
            id: -1,
            tqCourseCourse: {},
            vodInfo: {},
            currentTime: 0,
            learnTime: 0,
            timer: null,
            canSave: true,
            content: "",
            isLogin: 0,
            memToken: '',
            memHeadImg: '',
            userJf: -1,
            playIndex: -1,
            attestation: null,

            pageLoaded: false,
            activeId: 1,
            transLoaded: true,
            queryQrCode: {},
            consulting: {},
            collect: false,
            collectImgSrc: '/web/img/collect.png',
            questionFlag: false, // 是否展示调查问卷
            chkCode: false,  //是否提交提取码
            checkI: 0

        }
    },
    created: function () {
        if ($.cookie("memToken")) {
            this.memToken = $.cookie("memToken");
            this.isLogin = 1;
        }
        if ($.cookie("memHeadImg")) {
            this.memHeadImg = $.cookie("memHeadImg");
        }
        this.id = $("#id").val();
        this.getIndexData();
        this.getAdInfo();
        this.findUserBaseInfo();
        this.findUserIntegral();

        this.getQueryQrCode();
        this.getConsulting();
        // this.getAttestation();
    },
    filters: {
        date(time) {
            let oldDate = new Date(time)
            let newDate = new Date()
            var dayNum = "";
            var getTime = (newDate.getTime() - oldDate.getTime()) / 1000;

            if (getTime < 60 * 5) {
                dayNum = "刚刚";
            } else if (getTime >= 60 * 5 && getTime < 60 * 60) {
                dayNum = parseInt(getTime / 60) + "分钟前";
            } else if (getTime >= 3600 && getTime < 3600 * 24) {
                dayNum = parseInt(getTime / 3600) + "小时前";
            } else if (getTime >= 3600 * 24 && getTime < 3600 * 24 * 30) {
                dayNum = parseInt(getTime / 3600 / 24) + "天前";
            }
            if (dayNum != "") {
                return dayNum;
            }
            let year = oldDate.getFullYear();
            let month = oldDate.getMonth() + 1;
            let day = oldDate.getDate();
            let hour = oldDate.getHours();
            let minute = oldDate.getMinutes();
            let second = oldDate.getSeconds();
            return year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
        }
    },
    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);
        window.addEventListener('beforeunload', e => this.beforeunloadHandler(e))
    },
    methods: {
        getAttestation() {
            var that = this;
            $.ajax({
                type : 'POST',
                url : serverHttp+"/popu/window/checkWindow",
                data : {
                    id: that.id,
                    idType: 0,
                    memToken: that.memToken
                },
                success : function(data) {
                    if (data.code == 200) {
                        that.attestation = data.data;
                    }
                },
                error : function(data) {
                }
            });
        },
        addLiveComment() {
            var that = this;
            var content = $("textarea[name='commtent_content']").val();
            if (!content) {
                alert("请输入您的观点");
                return false;
            }

            if (!checkWebLogin()) {
                // $('.navlogorebox').trigger('click')
                return false;
            }
            $.ajax({
                type: 'POST',
                url: serverHttp + "/course/saveCommon",
                data: {
                    memToken: that.memToken,
                    id: that.info.id,
                    content: content
                },
                success: function (data) {
                    if (data.code == 200) {
                        // that.websocketsend(content);
                        $("textarea[name='commtent_content']").val("");
                        that.getIndexData();
                    } else if (data.code == 10008) {
                        //去登录
                        // goLogin();
                    } else {
                        alert(data.data.message);
                    }
                },
                error: function (data) {
                }
            });

        },
        // 查询社群二维码
        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
                    }
                },
                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) {
                }
            });
        },
        confirm() {
            var that = this;
            if (that.content.replace(/(^s*)|(s*$)/g, "").length == 0) {
                alert('请输入内容');
                return;
            }
            $.ajax({
                type: 'POST',
                url: serverHttp + "/course/saveCommon",
                data: {
                    memToken: that.memToken,
                    content: that.content,
                    id: that.id,
                },
                success: function (data) {
                    if (data.code == 200) {
                        alert("保存成功");
                        $(".common").hide("slow");
                        that.getIndexData();
                    }
                },
                error: function (data) {
                }
            });
        },
        collectFn() {
            var that = this;
            $.ajax({
                type: 'post',
                data: {typeId: that.id, type: 1, isDel: that.collect ? 1 : 2},
                url: '/user/saveCollectionWeb',
                success: function (res) {
                    if (res.code == 200) {
                        that.collect = !that.collect;
                        if (that.collect) {
                            that.collectImgSrc = '/web/img/collected.png';
                            layer.msg('已收藏，可在我的收藏中查看');
                        } else {
                            that.collectImgSrc = '/web/img/collect.png';
                            layer.msg('已取消收藏');
                        }
                    } else if (res.code == 10008) {
                        $('.navlogorebox').click();
                    } else {
                        layer.msg('添加收藏失败，请重试');
                    }
                }
            })
        },
        commonClose() {
            $(".common").hide("slow");
        },
        toPay() {
            var that = this
            if (!checkWebLogin()) {
                // $('.navlogorebox').trigger('click')
                return false;
            }
            if (!parseRule(that.id,that.memToken,'3')) {
                return false;
            } else {
                toPayWeb(1, that.info.id, 1, '');
            }
        },
        addFreeBuyApi() {
            var that = this;

            $.ajax({
                type: 'post',
                url: serverHttp + "/index/isLogin",
                async: false,
                success: function (res) {
                    if (res.code == 200) {
                        if (!parseRule(that.id,that.memToken,'3')) {
                            return false;
                        }
                        // if (!checkLogin()) {
                        //     $('.navlogorebox').trigger('click')
                        //     return false;
                        // }
                        $.ajax({
                            type: 'POST',
                            url: serverHttp + "/pay/addFreeBuyApi",
                            data: {
                                memToken: that.memToken,
                                orderType: 1,
                                objId: that.info.id,
                                ispackage: 1,
                                paySource: 1,
                                fromMemCard: ''
                            },
                            success: function (data) {
                                if (data.code == 200) {
                                    that.info.isBuy = 1;
                                    playMusic("积分兑换成功", 10);
                                } else if (data.code == 10008) {
                                    $.ajax({
                                        type: 'post',
                                        url: serverHttp + "/index/isLogin",
                                        async: false,
                                        success: function (res) {
                                            if (res.code == 200) {
                                                alert('领取成功！')
                                            } else {
                                                $('.navlogorebox').trigger('click')
                                            }
                                        },
                                        error: function (data) {
                                            return false;
                                        }
                                    });
                                } else {
                                    alert(data.data.message);
                                }
                            },
                            error: function (data) {
                            }
                        });
                    } else {
                        $('.navlogorebox').trigger('click')
                        return false;
                    }
                }
            })
        },
        // parseRule() {
        //     var that = this;
        //     $("#submitInfo").unbind("click");
        //     $("#professional").unbind("click");
        //     $("#submitInformation").unbind("click");
        //     if (that.attestation.realName) {
        //         var index = layer.open({
        //             type: 1,
        //             area: ['600px', '600px'],
        //             skin: 'layui-layer-molv',
        //             shift: 0,
        //             title: false,
        //             offset: ['8%', 'calc(50% - 260px)'],
        //             closeBtn: 0,
        //             shadeClose: true,
        //             content: $('#smrz'),
        //             success: function () {
        //                 $("#submitInfo").click(function () {
        //
        //                     var userName = $("#userName").val()
        //                     var card = $("#card").val()
        //                     var unitName = $("#unitName").val()
        //                     var address = $("#address").val()
        //                     var sex = $("input[type='radio']:checked").val();
        //                     var experience = $("#experience").val()
        //                     var department = $("#department").val()
        //
        //                     if (!userName || !card || !unitName || !address || !sex || !experience || !department) {
        //                         alert("请完善相关信息");
        //                         return
        //                     }
        //                     $.ajax({
        //                         type: 'POST',
        //                         url: "/user/saveUserInfoWithSmWeb",
        //                         data: {
        //                             userName: userName,
        //                             card: card,
        //                             unitName: unitName,
        //                             address: address,
        //                             sex: sex,
        //                             experience: experience,
        //                             department: department
        //                         },
        //                         success: function (res) {
        //                             if (res.code == 200) {
        //                                 layer.msg(res.message)
        //                                 $.ajax({
        //                                     type: 'POST',
        //                                     url: serverHttp + "/user/findUserBaseInfo",
        //                                     data: {
        //                                         memToken: that.memToken
        //                                     },
        //                                     success: function (data) {
        //                                         that.memberInfo = data.data;
        //                                         that.attestation.realName = false;
        //                                         layer.close(index);
        //                                         playMusic('实名认证成功', 200);
        //                                         if(that.attestation.physician || that.attestation.question || (!that.chkCode && that.info.extractedCode)){
        //                                             setTimeout(that.parseRule, 1500)
        //                                         }
        //                                     },
        //                                     error: function (data) {
        //                                     }
        //                                 });
        //                             } else if (res.code == 10014) {
        //                                 layer.msg("已实名认证")
        //                             } else {
        //                                 layer.msg(res.message);
        //                             }
        //                         }
        //                     })
        //                 })
        //             }
        //         });
        //         return false;
        //     } else if (that.attestation.physician) {
        //         var index = layer.open({
        //             type: 1,
        //             area: ['600px', '600px'],
        //             skin: 'layui-layer-molv',
        //             shift: 0,
        //             title: false,
        //             offset: ['8%', 'calc(50% - 260px)'],
        //             closeBtn: 0,
        //             shadeClose: true,
        //             content: $('#ysrz'),
        //             success: function () {
        //
        //                 getCataList(51, 1);
        //                 getCataListZw(150, 1);
        //
        //                 $("#submitInformation").click(function () {
        //                     var professional = $("#professional").val()
        //                     var professionalTwo = $("#professionalTwo").val()
        //                     var businessLicense = $("#businessLicense").val()
        //                     var post = $("#post").val()
        //                     if (!professional || !professionalTwo || !businessLicense || post == -1 || !post) {
        //                         layer.msg("请完善相关信息");
        //                         return;
        //                     }
        //                     if (!checkyishi(businessLicense)) {
        //                         layer.msg("执业编码填写错误");
        //                         return false;
        //                     }
        //                     $.ajax({
        //                         type: 'POST',
        //                         url: serverHttp + "/user/saveUserInfoWithYsWeb",
        //                         data: {
        //                             professional: professional,
        //                             businessLicense: businessLicense,
        //                             professionalTwo: professionalTwo,
        //                             post: post
        //                         },
        //                         success: function (res) {
        //                             if (res.code == 200) {
        //                                 $.ajax({
        //                                     type: 'POST',
        //                                     url: serverHttp + "/user/findUserBaseInfo",
        //                                     data: {
        //                                         memToken: ''
        //                                     },
        //                                     success: function (data) {
        //                                         that.memberInfo = data.data;
        //                                         layer.close(index)
        //                                         that.attestation.physician = false;
        //                                         playMusic('医师认证成功', 500);
        //                                         if (that.attestation.question || ((!that.chkCode && that.info.extractedCode))) {
        //                                             setTimeout(that.parseRule, 1500)
        //                                         }
        //                                     },
        //                                     error: function (data) {
        //                                     }
        //                                 });
        //                             } else if (res.code == 10014) {
        //                                 layer.msg("已完成医师认证")
        //                             }else if (res.code == 10016) {
        //                                 layer.msg(res.message);
        //                             }
        //                         }
        //                     })
        //                 })
        //
        //                 $("#professional").click(function () {
        //                     professionalChange(1)
        //                 })
        //
        //                 // $("#professionalTwo").click(function () {
        //                 //     professionalChange(1)
        //                 // })
        //
        //                 function professionalChange(type) {
        //                     if (type == 1) {
        //                         var professional = $("#professional").val()
        //                         if (professional == -1) {
        //                             // that.selectTwoData = [];
        //                         } else {
        //                             getCataList(professional, 2);
        //                         }
        //                     }
        //                 }
        //
        //                 function getCataList(fId, type) {
        //                     $.ajax({
        //                         type: 'POST',
        //                         url: serverHttp + "/course/getCataList",
        //                         data: {
        //                             fId: fId,
        //                             type: 5,
        //                         },
        //                         success: function (res) {
        //                             if (res.code == 200) {
        //                                 if (type == 1) {
        //                                     $("#professional").empty()
        //                                     res.data[0].name = "请选择"
        //                                     var selectOneData = res.data;
        //                                     for (let i = 0; i < selectOneData.length; i++) {
        //                                         $("#professional").append("<option value='" + selectOneData[i].id + "'>" + selectOneData[i].name + "</option>")
        //                                     }
        //                                 } else {
        //                                     $("#professionalTwo").empty()
        //                                     var selectTwoData = res.data;
        //                                     for (let i = 0; i < selectTwoData.length; i++) {
        //                                         $("#professionalTwo").append("<option value='" + selectTwoData[i].id + "'>" + selectTwoData[i].name + "</option>")
        //                                     }
        //                                 }
        //                             }
        //                         },
        //                         error: function (data) {
        //
        //                         }
        //                     });
        //                 }
        //
        //                 function getCataListZw(fId, type) {
        //                     $.ajax({
        //                         type: 'POST',
        //                         url: serverHttp + "/course/getCataList",
        //                         data: {
        //                             fId: fId,
        //                             type: 5,
        //                         },
        //                         success: function (res) {
        //                             res.data[0].name = "请选择";
        //                             if (res.code == 200) {
        //                                 var zw = res.data;
        //                                 for (let i = 0; i < zw.length; i++) {
        //                                     $("#post").append("<option value='" + zw[i].id + "'>" + zw[i].name + "</option>")
        //                                 }
        //                             }
        //                         },
        //                         error: function (data) {
        //
        //                         }
        //                     });
        //                 }
        //             }
        //         });
        //         return false;
        //     }else if (!that.chkCode && that.info.extractedCode){
        //         let flag = layer.open({
        //             type: 1,
        //             area: ['400px', '300px'],
        //             skin: 'layui-layer-molv',
        //             shift: 0,
        //             title: false,
        //             offset: ['8%', 'calc(50% - 260px)'],
        //             closeBtn: 1,
        //             shadeClose: true,
        //             content: $('#takeCode'),
        //             success: () => {
        //                 $('#takeCodeBtn').click(() => {
        //                     that.checkCode(flag, that.checkI)
        //                 })
        //             }
        //         })
        //         return false;
        //     }
        //     return true;
        // }

        findUserBaseInfo() {

            if (!checkWebLogin()) {
                // $('.navlogorebox').trigger('click')
                return false;
            }
            $.ajax({
                type: 'POST',
                url: serverHttp + "/user/findUserBaseInfo",
                data: {
                    memToken: this.memToken
                },
                success: (data) => {
                    this.memberInfo = data.data;
                },
                error: function (data) {
                }
            });
        },
        updateTime(e) {
            var video = document.getElementById('video');
            this.currentTime = Math.floor(video.currentTime) * 1000;
        }
        ,
        videoPlay() {
            this.startTiming();
        }
        ,
        videoPause() {
            clearInterval(this.timer)
        }
        ,
        startTiming() {
            var that = this;
            that.timer = setInterval(function () {
                that.learnTime = that.learnTime + 1;
                console.log(that.learnTime);
                if (that.learnTime == 600) {
                    // that.integralType = "学习课程10分钟";
                    // that.integralCount = 10;
                    $(".integral-toast").show();
                    playMusic('学习课程10分钟', 10)
                    // setTimeout(function () {
                    //     $(".integral-toast").fadeOut(200);
                    // }, 2000);
                }
                if (that.learnTime % 30 == 0) {
                    //1分钟的倍数
                    that.addVideoPlayRecorde();
                }
            }, 1000)
        }
        ,
        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: {
                    cataId: -1,
                    start: that.start,
                    size: that.size,
                    fId: that.id,
                    memToken: that.memToken
                },
                success: function (data) {
                    if (data.code == 200) {
                        that.list = that.list.concat(data.data.list);
                        that.info = data.data.info;
                        that.tqCourseCourse = data.data.info;

                        if (that.tqCourseCourse.qkjVodinfo) {
                            that.newPlayer();
                        }

                        that.cataList = data.data.cataList;
                        if (data.data.comments && data.data.comments) {
                            that.cata[2].name = "互动";
                            that.comments = data.data.comments;
                        }
                        that.collect = data.data.info.isController > 0 ? false : true;
                        that.collectImgSrc = data.data.info.isController > 0 ? '/web/img/collect.png' : '/web/img/collected.png';
                        if (data.data.list) {
                            that.finished = true;
                        }
                        that.loading = false;

                        that.pageLoaded = true;

                        $("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);
                        document.title = that.info.title;
                    } else if (data.code == 10005) {
                        that.finished = true;
                    }
                },
            });
        }
        ,
        formatDuring(mss) {
            var secondTime = parseInt(mss) // 秒
            var minuteTime = 0 // 分
            var hourTime = 0 // 小时
            var dayTime = 0 // 天
            var result = ''
            if (mss < 60) {
                result = secondTime + '秒'
            } else {
                if (secondTime >= 60) { // 如果秒数大于60，将秒数转换成整数
                    // 获取分钟，除以60取整数，得到整数分钟
                    minuteTime = parseInt(secondTime / 60)
                    // 获取秒数，秒数取佘，得到整数秒数
                    secondTime = parseInt(secondTime % 60)
                    // 如果分钟大于60，将分钟转换成小时
                    if (minuteTime >= 60) {
                        // 获取小时，获取分钟除以60，得到整数小时
                        hourTime = parseInt(minuteTime / 60)
                        // 获取小时后取佘的分，获取分钟除以60取佘的分
                        minuteTime = parseInt(minuteTime % 60)
                        if (hourTime >= 24) {
                            // 获取天数， 获取小时除以24，得到整数天
                            dayTime = parseInt(hourTime / 24)
                            // 获取小时后取余小时，获取分钟除以24取余的分；
                            hourTime = parseInt(hourTime % 24)
                        }
                    }
                }
                if (secondTime > 0) {
                    secondTime = parseInt(secondTime) >= 10 ? secondTime : '0' + secondTime
                    result = '' + secondTime + '秒'
                }
                if (minuteTime > 0) {
                    minuteTime = parseInt(minuteTime) >= 10 ? minuteTime : '0' + minuteTime
                    result = '' + minuteTime + '分' + result
                }
                if (hourTime > 0) {
                    result = '' + parseInt(hourTime) + '小时' + result
                }
                if (dayTime > 0) {
                    result = '' + parseInt(dayTime) + '天' + result
                }
            }
            return result
        }
        ,
        coursePrefectureList(i) {
            const type = this.list[i].zq;
            if (type == 1) {
                //普通课程
                window.location.href = serverHttp + "/course/courseDetails?from=web&id=" + this.list[i].id;
            } else {
                //专区
                window.location.href = serverHttp + "/course/coursePrefectureList?from=web&id=" + this.list[i].id;
            }
        }
        ,
        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) {
            this.transLoaded = false;
            setTimeout(() => {
                this.activeId = this.cata[i].id
                this.transLoaded = true
            }, 400)
        }
        ,
        gotoAdHtml(url) {
            window.location.href = url;
        }
        ,
        findTqCourseCourseInfo(item, i) {
            const that = this;
            that.canSave = false;
            that.questionFlag = item.questionFlag == 1;
            that.checkI = i;
            that.addVideoPlayRecorde();
            $("#takeCodeBtn").unbind("click");

            if (!checkWebLogin()) {
                // $('.navlogorebox').trigger('click')
                return false;
            } else {
                if (!parseRule(that.id,that.memToken,'3')) {
                    return false;
                }
                coursecodestatus = $.cookie("coursecodestatus" + this.list[i].id);
                if (coursecodestatus == "ok") {
                    this.startVideo(i);
                } else {
                    // 是否为定向人员课程
                    if (that.list[i].isSpecialCourse == 1 && !that.chkCode) { // 1为定向课程

                        let flag = layer.open({
                            type: 1,
                            area: ['400px', '300px'],
                            skin: 'layui-layer-molv',
                            shift: 0,
                            title: false,
                            offset: ['8%', 'calc(50% - 260px)'],
                            closeBtn: 1,
                            shadeClose: true,
                            content: $('#takeCode'),
                            success: () => {
                                $('#takeCodeBtn').click(() => {
                                    this.checkCode(flag, i)
                                })
                            }
                        })
                    } else {
                        this.startVideo(i)
                    }
                }
            }
        },
        // || this.info.price == 0 || this.info.point == 0
        startVideo(i) {
            if (this.list[i].isTrailer == 1 || this.info.isBuy == 1 ) {
                // layer.msg("课程播放结束后需要有自评"); //msg：消息提示
                if (this.questionFlag) {
                    let flag = layer.open({
                        type: 1,
                        area: ['300px', '300px'],
                        skin: 'layui-layer-molv',
                        shift: 0,
                        title: false,
                        offset: ['8%', 'calc(50% - 260px)'],
                        closeBtn: 0,
                        shadeClose: true,
                        content: $('#ziping'),
                        success: () => {
                            $('#zipingBtn').click(() => {
                                $('#ziping').hide()
                                layer.close(flag)
                                this.tqCourseCourse = this.list[i];

                                if (this.tqCourseCourse.qkjVodinfo) {
                                    this.newPlayer();
                                }

                                this.playIndex = i;
                            })
                        }
                    })
                } else {
                    this.tqCourseCourse = this.list[i];

                    if (this.tqCourseCourse.qkjVodinfo) {
                        this.newPlayer();
                    }

                    this.playIndex = i;
                }

            } else {
                // this.toPay();
                //未购买弹框提示，不能直接观看课程
                var getInfo = this.info;
                if((getInfo.price==0 && getInfo.pointFlag==0) || (getInfo.price==0 && getInfo.pointFlag==1 && getInfo.point==0)){
                    var buyText = '<p style="color: #000;font-size: 16px;">领取后观看</p>';
                    var buyBtn = '<button id="goFreeBtn" class="buy-btn">免费领取</button>';
                }else if((getInfo.pointFlag==1 && getInfo.point > 0) || (info.price>0)){
                    var buyText = '<p style="color: #000;font-size: 16px;">购买后观看</p>'
                    var buyBtn = '<button id="goBuyBtn" class="buy-btn">立即购买</button>';
                }
                $("#buyBox .buyCont").empty();
                $("#buyBox .buyCont").append(buyText);
                $("#buyBox .buyCont").append(buyBtn);
                let buyPage = layer.open({
                    type: 1,
                    area: ['300px', '200px'],
                    skin: 'layui-layer-molv',
                    shift: 0,
                    title: false,
                    offset: ['8%', 'calc(50% - 200px)'],
                    closeBtn: 1,
                    shadeClose: true,
                    content: $('#buyBox'),
                    success: () => {
                        $('#goBuyBtn').click(() => {
                            this.toPay();
                            layer.close(buyPage)
                        })
                        $('#goFreeBtn').click(() => {
                            this.addFreeBuyApi();
                            layer.close(buyPage)
                        })
                    }
                })
            }

            clearInterval(this.timer);
        },

        newPlayer() {
            var that = this
            document.getElementById('dplayer').style.display = ""
            const dp = new DPlayer({
                container: document.getElementById('dplayer'),
                video: {
                    url: that.tqCourseCourse.qkjVodinfo.originalUrl,
                    pic: 'http://files.qkyxzx.com/' + this.tqCourseCourse.coverImg
                },
                autoplay: true,
                hotkey: true,

            });
            dp.on("timeupdate", () => {
                this.currentTime = Math.floor(dp.video.currentTime) * 1000;
            });
            dp.on("play", () => {
                this.videoPlay()
            });
            dp.on("pause", () => {
                this.videoPause()
            });
            dp.on("pause", () => {
                this.videoPause()
            });
            dp.on("ended", () => {
                this.videoEnded()
            });
            document.getElementById('dplayer').oncontextmenu = function (event) {
                event.preventDefault();
                return false;
            }

        },

        // 提取码
        checkCode(flag, i) {
            if ($("#tqm").val() == '' || $("#tqm").val() == null) {
                alert('请输入提取码')
            } else {
                $.ajax({
                    type: 'GET',
                    url: serverHttp + "/course/checkCode",
                    async: false,
                    data: {
                        code: $("#tqm").val(),
                        id: this.list[i].id,

                    },
                    success: (data) => {
                        if (data.code == 200) {
                            $('#takeCode').hide()
                            layer.close(flag)
                            $.cookie("coursecodestatus" + this.list[i].id, "ok");
                            this.startVideo(i);
                            this.chkCode = true;
                        } else {
                            alert(data.message)
                        }
                    },
                });
            }
        },

        beforeunloadHandler() {
            this.addVideoPlayRecorde();
        },
        addVideoPlayRecorde() {
            var that = this;
            if (that.canSave) {
                return;
            }
            if (that.playIndex == -1) {
                return;
            }
            $.ajax({
                type: 'POST',
                url: serverHttp + "/course/addVideoPlayRecorde",
                async: false,
                data: {
                    playTime: that.currentTime,
                    totalLearnTime: that.learnTime * 1000,
                    objType: 1,
                    objId: that.list[that.playIndex].id,
                    memToken: that.memToken
                },
                success: function (data) {
                    if (data.code == 200) {
                        // that.currentTime = 0;
                        // that.learnTime = 0;
                    }else if(data.code == 10008){
                        $('.navlogorebox').trigger('click');
                    }else if(data.code == 10029){
                        //在另一设备登录，重新登录
                        layer.open({
                            title:"提示",//标题信息
                            content: '您的账号在另一台设备登录，如需继续使用，请重新登录',//内容区域
                            shift: 0,
                            offset: ['calc(50% - 80px)', 'calc(50% - 200px)'],
                            shadeClose: true,
                            btn: ['登录'],
                            btn1: function(index){
                                layer.close(index)
                                $('.navlogorebox').trigger('click');
                            },
                            // 右上角关闭按钮的点击事件
                            cancel: function(index){
                                layer.close(index)
                                // $('.navlogorebox').trigger('click');
                                window.location.href = serverHttp + '/index/index?from=web'
                            }
                        });
                    }
                },
            });
        },
        addCommon() {
            $(".common").show("slow");
        },
        // 分享
        share() {
            // window.location.href = serverHttp + "/weixin/getOwnPoster?objType=3&objId=" + this.info.id;
            window.location.href = serverHttp + "/weixin/getShareQrCode?objType=1&objId=" + this.info.id;

        },
        // 视频播放完成
        videoEnded() {
            if (this.questionFlag) {
                let flag = layer.open({
                    type: 1,
                    area: ['500px', '500px'],
                    skin: 'layui-layer-molv',
                    shift: 0,
                    title: false,
                    offset: ['8%', 'calc(50% - 260px)'],
                    closeBtn: 0,
                    shadeClose: true,
                    content: $('#wd'),
                    success: () => {
                        $('#submitWd').click(() => {
                            if ($("#sfxh").val() == '') {
                                alert('请填写答案')
                            } else {
                                $.ajax({
                                    type: 'POST',
                                    url: serverHttp + "/live/saveQuestionAnswer",
                                    data: {
                                        questions: JSON.stringify(
                                            [{
                                                objType: 3,
                                                objId: this.info.id,
                                                quTitle: '您是否学会本课程，为什么？',
                                                answerContent: $("#sfxh").val()

                                            }])
                                    },

                                    success: (data) => {
                                        if (data.code == 200) {
                                            $("#wd").hide();
                                            layer.msg("提交成功")
                                            layer.close(flag)
                                            // that.liveInfo.isQuestionAnswer = 1;
                                        }
                                    },
                                    error: function (data) {
                                    }
                                });
                            }
                        })
                    }
                })
            }


        },
    }
})
