Vue.config.devtools = true;
var v = new Vue({
    el: ".index-box",
    data: {
        cata: [
            {name: "直播日程", active: 1, id: 1},
            {name: "会议介绍", active: 0, id: 2},
            {name: "云展厅", active: 0, id: 3},
        ],
        liveId: '',
        liveInfo: {
            sysRecommendDTOs: [],
        },
        memberInfo: {
            isPurchased: 0,
            isRemind: 0,
            joinMembers: 0,
            remindMemberNum: 0
        },
        attestation: null,
        isLogin: 0,
        memToken: '',
        memHeadImg: '',
        userJf: -1,
        queryQrCode: {},
        consulting: {},
        collect: false,
        collectImgSrc: '/web/img/collect.png',
        numberTimer: null, // 人数定时器
        joinMembers: 0
    },
    created: function () {
        var that = this;
        if ($.cookie("memToken")) {
            that.memToken = $.cookie("memToken");
            that.isLogin = 1;
        }
        if ($.cookie("memHeadImg")) {
            that.memHeadImg = $.cookie("memHeadImg");
        }
        that.liveId = $("#liveId").val();
        that.getDetailData();
        that.findUserBaseInfo()
        this.getQueryQrCode()
        this.findUserIntegral();
        this.getConsulting()
        //认证信息公共部分
        // this.getAttestation()
    },
    mounted: function () {
        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(() => {
            this.initFn();
        }, 1000);
        setTimeout(() => {
            $(".loading-item").fadeOut(200);
        }, 500)
        //认证信息公共部分
        // window.getId = this.liveId;
        // window.getMemToken = this.memToken;
        // window.getRzType = '1';
    },
    destroy() {
        clearInterval(this.numberTimer)
    },
    methods: {
        getAttestation() {
            var that = this;
            $.ajax({
                type : 'POST',
                url : serverHttp+"/popu/window/checkWindow",
                data : {
                    id: that.liveId,
                    idType: 1,
                    memToken: that.memToken
                },
                success : function(data) {
                    if (data.code == 200) {
                        that.attestation = data.data;
                    }
                },
                error : function(data) {
                }
            });
        },
        // 获取人数
        getcounter() {
            $.ajax({
                type: 'GET',
                url: `${serverHttp}/livecounter/getcounter?liveId=${this.liveId}`,
                success: (res) => {
                    if (res.code == 200) {
                        this.joinMembers = res.data.count
                        // 子人数
                        this.liveInfo.sons.map((son,index )=> {
                            res.data.children.map(item => {
                                if (son.lvId == item.liveId) {
                                    son.count = item.count
                                }
                            })
                            Vue.set(this.liveInfo.sons, index, son);

                        })
                    }
                    // else if (res.code == 10008) {
                    //     $('.navlogorebox').click();
                    // }
                }
            })
        },
        collectFn() {
            var that = this;
            $.ajax({
                type: 'post',
                data: {typeId: that.liveId, type: 2, 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('添加收藏失败，请重试');
                    }
                }
            })
        },
        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) {
                }
            });
        },

        findUserBaseInfo() {
            var that = this;
            if (!checkWebLogin()) {
                // $('.navlogorebox').trigger('click')
                return false;
            }
            $.ajax({
                type: 'POST',
                url: serverHttp + "/user/findUserBaseInfo",
                data: {
                    memToken: that.memToken
                },
                success: function (data) {
                    that.memberInfo = data.data;
                },
                error: function (data) {
                }
            });
        },
        initFn() {
            var mySwiper = new Swiper('.swiper-container', {
                direction: 'horizontal', // 水平切换选项
                loop: true, // 循环模式选项
                observerParents: true,
                observer: true,
                autoplay: {
                    disableOnInteraction: false,  //触碰后自动轮播也不会停止
                    delay: 2000,
                }, autoplay: true,
                // 如果需要分页器
                pagination: {
                    el: '.swiper-pagination',
                },
                // 如果需要前进后退按钮
                navigation: {
                    nextEl: '.swiper-button-next',
                    prevEl: '.swiper-button-prev',
                },
                // 如果需要滚动条
                scrollbar: {
                    el: '.swiper-scrollbar',
                },

            })
        },
        // parseRule() {
        //     var that = this;
        //     $("#submitInfo").unbind("click");
        //     $("#submitQuestion").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: ['13%', '29%'],
        //             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: ['13%', '29%'],
        //             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) {
        //                                 // 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 {
        //                                 alert(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()
        //                                     res.data[0].name = "请选择"
        //                                     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.attestation.question) {
        //         // $(".info-collect").show();
        //
        //         var size = 0;
        //         for (var item of that.liveInfo.questions) {
        //             if (item.answerContent == undefined || item.answerContent == null || item.answerContent === '') {
        //                 size++;
        //             }
        //         }
        //         if (size == 0) {
        //             return true;
        //         }
        //         let layerIndex = layer.open({
        //             type: 1,
        //             area: ['560px', '430px'],
        //             skin: 'layui-layer-molv',
        //             shift: 0,
        //             title: false,
        //             offset: ['13%', '29%'],
        //             closeBtn: 0,
        //             shadeClose: true,
        //             content: $('#wjdc'),
        //             success: function () {
        //                 $("#writeform").empty()
        //                 var index = 0;
        //                 for (var item of that.liveInfo.questions) {
        //                     if (item.answerContent == undefined || item.answerContent == null || item.answerContent === '') {
        //                         $("#writeform").append("<div><label>" + item.quTitle + "：</label><span><input data-index='" + index + "' type=\"text\" placeholder=\"请输入\"/></span></div>");
        //                     }
        //                     index++;
        //                 }
        //                 $("#writeform").append("<div class=\"layerbtnbox\"><button id='submitQuestion' type=\"button\" class=\"btn\">提交信息</button></div>")
        //                 $("#submitQuestion").click(function () {
        //                     var check = true;
        //                     var inputs = $("#writeform input");
        //
        //                     for (let i = 0; i < inputs.length; i++) {
        //                         that.liveInfo.questions[$(inputs[i]).data("index")].answerContent = $(inputs[i]).val()
        //                     }
        //
        //                     for (var item of that.liveInfo.questions) {
        //                         if (item.answerContent == undefined || item.answerContent == null || item.answerContent === '') {
        //                             check = false;
        //                             break;
        //                         }
        //                     }
        //                     if (!check) {
        //                         layer.msg("请完善相关信息");
        //                         return false;
        //                     }
        //                     $.ajax({
        //                         type: 'POST',
        //                         url: serverHttp + "/live/saveQuestionAnswer",
        //                         data: {
        //                             liveId: that.liveId,
        //                             memToken: that.memToken,
        //                             questions: JSON.stringify(that.liveInfo.questions)
        //                         },
        //                         success: function (data) {
        //                             if (data.code == 200) {
        //                                 that.liveInfo.isQuestionAnswer = 1;
        //                                 that.attestation.question = false;
        //                                 layer.close(layerIndex)
        //                                 // that.addFreeBuyApi();
        //                             }
        //                         },
        //                         error: function (data) {
        //                         }
        //                     });
        //                 })
        //             }
        //         });
        //
        //         return false;
        //     }
        //     return true;
        // },
        toPay() {
            var that = this

            if (!checkWebLogin()) {
                // $('.navlogorebox').trigger('click')
                return false;
            }
            if (!parseRule(that.liveId,that.memToken,'1')) {
                return false;
            }

            toPayWeb(2, that.liveInfo.lvId, 1, '');
        },
        addFreeBuyApi() {
            var that = this;
            if (!checkWebLogin()) {
                // $('.navlogorebox').trigger('click')
                return false;
            }

            if (!parseRule(that.liveId,that.memToken,'1')) {
                return false;
            }

            $.ajax({
                type: 'POST',
                url: serverHttp + "/pay/addFreeBuyApi",
                data: {
                    memToken: that.memToken,
                    orderType: 2,
                    objId: that.liveId,
                    ispackage: 1,
                    paySource: 3,
                    fromMemCard: ''
                },
                success: function (data) {
                    if (data.code == 200) {
                        that.liveInfo.isPurchased = 1;
                        playMusic("积分兑换成功", 10);
                        // alert("领取成功");
                    } else if (data.code == 10008) {
                        //去登录
                        // goLogin();
                    } else {
                        alert(data.data.message);
                    }
                },
                error: function (data) {
                }
            });
        },
        getDetailData() {
            var that = this;
            $.ajax({
                type: 'POST',
                url: serverHttp + "/live/getDetailData",
                data: {
                    memToken: that.memToken,
                    liveId: that.liveId
                },
                success: (data) => {
                    if (data.code == 200) {
                        that.liveInfo = data.data;
                        $("meta[name='sharetitle']").attr("content", that.liveInfo.shareTitle);
                        $("meta[name='sharedesc']").attr("content", that.liveInfo.shareDesc);
                        $("meta[name='sharepic']").attr("content", that.liveInfo.sharePic);
                        // $("meta[name='shareurl']").attr("content",that.liveInfo.shareUrl);
                        that.collect = data.data.isController > 0 ? false : true;
                        that.collectImgSrc = data.data.isController > 0 ? '/web/img/collect.png' : '/web/img/collected.png';
                        // 实时人数

                        this.getcounter()
                        this.numberTimer = setInterval(this.getcounter, 30000)
                    }
                },
                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;
        },
        gotoDetail(obj) {
            if((this.liveInfo.price==0 && this.liveInfo.pointFlag==0 && this.liveInfo.isPurchased == 0) || (this.liveInfo.price==0 && this.liveInfo.pointFlag==1 && this.liveInfo.point==0 && this.liveInfo.isPurchased == 0)){
                layer.msg('请先点击参会申请！');
                return false;
            }

            if (!parseRule(this.liveId,this.memToken,'1')) {
                return false;
            }

            window.location.href = serverHttp + '/live/detail?from=&liveId=' + obj.lvId;
        },
        gotoRcmd(obj) {
            gotoRcmdPub(obj.zqFlag, obj.rcmdType, obj.rcmdObjId, obj.rcmdUrl);
        },
        // 查询社群二维码
        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) {
                }
            });
        },

        // 课件下载
        getCourseware() {
            var that = this;
            $.ajax({
                type: 'POST',
                url: "/courseware/getlistData?cataId=-1",
                data: {
                    courseware: that.courseware
                },
                success: function (data) {
                    if (data.code == 200) {
                        that.courseware = data.data

                    }
                },
                error: function (data) {
                }
            });
        },

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