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},
            // {name: "相关课程", active:0, id: 4}
        ],
        activeId: 1,
        transLoaded: true,
        list: [],
        info: {},
        start: 1,
        size: 10,
        finished: false,
        loading: false,//出现了 上拉到底出发了两次请求接口的情况  加一个变量控制一下
        ad: [],//广告信息
        zqId: -1,
        isLogin: 0,
        memToken: '',
        memHeadImg: '',
        userJf: -1,
        memberInfo: {
            isPurchased: 0,
            isRemind: 0,
            joinMembers: 0,
            remindMemberNum: 0
        },
        attestation: null,
        pageLoaded: false,
        queryQrCode: {},
        consulting: {},
        comments: [],
        correlatedList: [],
        collect: false,
        collectImgSrc: '/web/img/collect.png'
    },
    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.getQueryQrCode();
        // 咨询推荐
        this.getConsulting();
        // this.getAttestation();
    },
    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');
        });
        var mySwiper = new Swiper('.swiper-container', {
            direction: 'horizontal', // 水平切换选项
            loop: true, // 循环模式选项
            autoplay: {
                disableOnInteraction: false,  //触碰后自动轮播也不会停止
                delay: 2000,
            },
            // 如果需要分页器
            pagination: {
                el: '.swiper-pagination',
            },

            // 如果需要前进后退按钮
            navigation: {
                nextEl: '.swiper-button-next',
                prevEl: '.swiper-button-prev',
            },

            // 如果需要滚动条
            scrollbar: {
                el: '.swiper-scrollbar',
            },

        })
        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: 0,
                    memToken: that.memToken
                },
                success : function(data) {
                    if (data.code == 200) {
                        that.attestation = data.data;
                    }
                },
                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) {
                }
            });
        },
        collectFn() {
            var that = this;
            $.ajax({
                type: 'post',
                data: {typeId: that.zqId, 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('添加收藏失败，请重试');
                    }
                }
            })
        },
        // 咨询推荐
        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) {
                }
            });
        },
        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() {


            var that = this
            if (!checkWebLogin()) {
                // $('.navlogorebox').trigger('click')
                return false;
            }
            if (!parseRule(that.zqId,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.zqId,that.memToken,'3')) {
                            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;
                                    playMusic("积分兑换成功", 10);
                                    // that.toPlay();
                                } else if (data.code == 10008) {
                                    //去登录
                                    $('.navlogorebox').trigger('click')
                                } 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){
        //                                             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) {
        //                             console.log(res);
        //                             if (res.code == 200) {
        //                                 layer.msg(res.message)
        //                                 $.ajax({
        //                                     type: 'POST',
        //                                     url: serverHttp + "/user/findUserBaseInfo",
        //                                     data: {
        //                                         memToken: ''
        //                                     },
        //                                     success: function (data) {
        //                                         that.memberInfo = data.data;
        //                                         that.attestation.physician = false;
        //                                         layer.close(index);
        //                                         playMusic('医师认证成功', 500);
        //                                         if(that.attestation.question){
        //                                             setTimeout(that.parseRule, 1500)
        //                                         }
        //                                     },
        //                                     error: function (data) {
        //                                     }
        //                                 });
        //                             } else if (res.code == 10014) {
        //                                 alert("已完成医师认证")
        //                             }else if (res.code == 10016) {
        //                                 layer.msg(res.message);
        //                             }
        //                         }
        //                     })
        //                 })
        //
        //                 $("#professional").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;
        //     }
        //     return true;
        // },
        findUserBaseInfo() {
            var that = this;
            if (!checkWebLogin()) {
                // $('.navlogorebox').trigger('click')
                return false;
            }
            // $.ajax({
            //     type: 'post',
            //     url: serverHttp + "/index/isLogin",
            //     async: false,
            //     success: function (res) {
            //         if (res.code == 200) {
            $.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: {
                    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;
                        }
                        that.comments = data.data.comments;
                        that.correlatedList = data.data.cataList;
                        that.collect = data.data.info.isController > 0 ? false : true;
                        that.collectImgSrc = data.data.info.isController > 0 ? '/web/img/collect.png' : '/web/img/collected.png';
                        // 请求的数据未加载完成时
                        that.start = that.start + 1;
                        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);
                    } else if (data.code == 10005) {
                        that.finished = true;
                    }
                },
            });
        },
        courseDetails(id) {
            //普通课程
            window.location.href = serverHttp + "/course/courseDetails?from=web&id=" + id;
        },
        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) {
            this.transLoaded = false;
            setTimeout(() => {
                this.activeId = this.cata[i].id
                this.transLoaded = true
            }, 400)
        },
        gotoAdHtml(url) {
            window.location.href = url;
        }
    }
})
