Vue.config.devtools = true;
var v = new Vue({
    el: ".index-box",
    data: function () {
        return {
            isVip: localStorage.getItem('isVip') == 1,
            cata: [
                {name: "详情", active: 0, id: 2},
                {name: "课程列表", active: 1, id: 1},

            ],
            ad: [],
            list: [],
            info: {},
            isLogin: 0,
            memToken: '',
            memHeadImg: '',
            userJf: -1,
            conferId: -1,
            playIndex: -1,
            queryQrCode: {},
            consulting: {},
            peopleList: [{
                name: '',
                mobile: '',
                questionAnswers: []
            }], // 参会人
            questionAnswers: [], // 问题列表
            isPayPeoples: [], // 已缴费人员
            dialogVisible: false, // 参与人弹窗
            src: '', // 内容网址
            active: 0,
            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");
        }
        if (!checkWebLogin()) {
            // $('.navlogorebox').trigger('click')
            return false;
        }
        this.conferId = $("#conferId").val();
        this.findUserIntegral();
        this.getDetailData();
        this.getQueryQrCode()
        this.getConsulting()
        this.getAdInfo()
    },
    watch: {
        consulting() {
            this.$nextTick(() => {
                this.inswiper()
            })
        }
    },
    methods: {
        inswiper() {
            var mySwiper = new Swiper('.adboxrightSwiper', {
                direction: 'horizontal', // 水平切换选项
                loop: true, // 循环模式选项

                observerParents: true,
                observer: true,
                autoplay: {
                    disableOnInteraction: false, //触碰后自动轮播也不会停止
                    delay: 2000,
                },
                // 如果需要分页器
                pagination: {
                    el: '.swiper-pagination',
                },

                // 如果需要前进后退按钮
                navigation: {
                    nextEl: '.swiper-button-next',
                    prevEl: '.swiper-button-prev',
                },
                // 如果需要滚动条
                scrollbar: {
                    el: '.swiper-scrollbar',
                },
            })
        },
        collectFn() {
            var that = this;
            $.ajax({
                type: 'post',
                data: {typeId: that.conferId, type: 3, 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('添加收藏失败，请重试');
                    }
                }
            })
        },
        // 添加人
        addPeople() {
            this.peopleList.push({
                name: '',
                mobile: '',
                questionAnswers: this.questionAnswers
            })
        },
        deleteMenu(index) {
            this.peopleList.splice(index, 1)
        },
        // 提交
        submit() {
            let flag = true
            if (this.peopleList.length == 0) {
                alert("请添加参会人")
            } else {
                this.peopleList.map((item, index) => {
                    if (item.name == '' || item.mobile == '') {
                        alert(`请填写第${index + 1}个参会人姓名或手机号`)
                        flag = false
                    } else {
                        let myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
                        if (!myreg.test(item['mobile'])) {
                            alert(`第${index + 1}个参会人手机号格式错误`);
                            flag = false
                        }
                    }
                })
                if (flag) {
                    // 判断参会人是否已报名
                    $.ajax({
                        type: 'POST',
                        url: serverHttp + "/meeting/pursePeopleBmWeb",
                        data: {
                            conferId: this.conferId,
                            peopleList: JSON.stringify(this.peopleList)
                        },
                        success: (data) => {
                            if (data.code == 200) {
                                this.save()
                            }
                        },
                        error: function (data) {
                        }
                    });
                }

            }

        },
        save() {
            $.ajax({
                type: 'POST',
                url: serverHttp + "/meeting/peopleBmWeb",
                data: {
                    conferId: this.conferId,
                    peopleList: JSON.stringify(this.peopleList)
                },
                success: (data) => {
                    if (data.code == 200) {
                        let orderId = data.data;
                        console.log(this.info, 'info')
                        // 免费订阅
                        if ((this.info.price == 0 && this.info.pointFlag == 0)
                            || (this.info.price == 0 && this.info.pointFlag == 1 && this.info.point == 0)) {
                            this.addFreeBuyApi(3, this.info.id, this.peopleList.length, orderId);
                        } else { // 付费
                            toPayWeb(3, this.info.id, this.peopleList.length, orderId);
                        }

                    }
                },
                error: function (data) {
                }
            });
        },
        addFreeBuyApi(orderType, objId, objCount, orderId) {
            $.ajax({
                type: 'POST',
                url: serverHttp + "/pay/addFreeBuyApi",
                data: {
                    memToken: this.memToken,
                    orderId: orderId,
                    orderType: orderType,
                    objId: objId,
                    objCount: objCount,
                    ispackage: 1,
                    paySource: 3,
                    fromMemCard: ''
                },
                success: (data) => {
                    if (data.code == 200) {
                        alert("报名成功");
                        this.peopleList = [{
                            name: '',
                            mobile: '',
                            questionAnswers: []
                        }] // 参会人
                        this.dialogVisible = false

                        window.location.href = serverHttp + "/meeting/finish?from=web&conferId=" + this.conferId;
                    } else if (data.code == 10008) {
                        //去登录
                        $('.navlogorebox').trigger('click')
                    } else {
                        alert(data.data.message);
                    }
                },
                error: function (data) {
                }
            });
        },
        // 订阅
        subscribe() {
            if (!checkWebLogin()) {
                // $('.navlogorebox').trigger('click')
                return false;
            }
            this.getPeopleList() // 获取已缴费人员信息
            this.dialogVisible = true
        },
        getPeopleList() {
            $.ajax({
                type: 'POST',
                url: serverHttp + "/meeting/getPeopleList",
                data: {
                    memToken: this.memToken,
                    conferId: this.conferId,
                },
                success: (data) => {
                    if (data.code == 200) {
                        this.isPayPeoples = data.data.isPayPeoples
                    }
                },
                error: function (data) {
                }
            });
        },
        // 内容网址
        chooseMenus(data, index) {
            this.active = index
            this.src = `${data.menuUrl}`

            console.log(this.info.menus)
            // this.info.menus = this.info.menus
        },
        toPay() {
            if (!checkWebLogin()) {
                // $('.navlogorebox').trigger('click')
                return false;
            }
            toPayWeb(3, this.info.id, 1, '');

        },
        share() {
            // window.location.href = serverHttp + "/weixin/getOwnPoster?objType=3&objId=" + this.info.id;
            window.location.href = serverHttp + "/weixin/getShareQrCode?objType=3&objId=" + this.info.id;

        },
        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;
                        that.collect = data.data.isController > 0 ? false : true;
                        that.collectImgSrc = data.data.isController > 0 ? '/web/img/collect.png' : '/web/img/collected.png';
                    }
                },
                error: function (data) {
                }
            });
        },
        copyPhone() {
            document.execCommand("18600766707");
            alert("复制成功")
        },
        getDetailData() {
            //id：分类id id = -1 是全部分类
            $.ajax({
                type: 'POST',
                url: serverHttp + "/meeting/getDetailDataWeb",
                data: {
                    conferId: this.conferId
                },
                success: (data) => {
                    if (data.code == 200) {
                        this.info = data.data;
                        this.info.bigenTime = tsFormatTime(this.info.bigenTime, 'Y.M.D')
                        this.info.endTime = tsFormatTime(this.info.endTime, 'Y.M.D')
                        document.title = this.info.title;
                        this.collect = data.data.isController > 0 ? false : true;
                        this.collectImgSrc = data.data.isController > 0 ? '/web/img/collect.png' : '/web/img/collected.png';
                        this.info.menus.length > 0 ? this.src = this.info.menus[0].menuUrl : null
                        //this.getQuestionList() // 获取问题列表
                    }
                },
                error: function (data) {

                }
            });
        },
        getQuestionList() {
            $.ajax({
                type: 'POST',
                url: serverHttp + "/meeting/getQuestionList",
                data: {
                    conferId: this.conferId
                },
                success: (data) => {
                    if (data.code == 200) {
                        this.questionAnswers = data.data.peopleAttributes.questionAnswers
                        this.peopleList[0].questionAnswers = this.questionAnswers
                    }
                },
                error: function (data) {

                }
            });

        },
        gotoAdHtml(url) {
            window.location.href = url;
        },
        addUser(id) {
            if (!againWebLogin()) {
                return false;
            }
            window.location.href = serverHttp + "/meeting/addUser?from=web&conferId=" + id;
        },
        getAdInfo() {
            var that = this;
            $.ajax({
                type: 'POST',
                url: serverHttp + "/meeting/getAdInfo",
                success: function (res) {
                    if (res.code == 200) {
                        that.ad = res.data;
                    }
                },
                error: function (data) {
                }
            });
        },
        chooseTab(i) {
            const that = this;
            for (var item of that.cata) {
                if (item.active == 1) {
                    item.active = 0
                }
            }
            that.cata[i].active = 1;
        },
        // 查询社群二维码
        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
                    }
                },
                error: function (data) {
                }
            });
        },
        jumpTo(url) {
            window.open(url)
        },
        jumpToLive(obj) {
            gotoRcmdPub(obj.zqFlag, obj.rcmdType, obj.rcmdObjId, obj.rcmdUrl, "web")
        },
    }
})
