Vue.config.devtools = true;
Vue.config.productionTip = false;
new Vue({
    el: ".index-box",
    data() {
        return {
            indexData: {

                seriesRecommends: {
                    children: [],
                }
            },
            info: {},
            // signIn: {

            // },
            queryQrCode: {},
            courseware: {},
            consulting: {},
            isLogin: false,
            memToken: '',
            memHeadImg: '',
            regCount:0,
            lineCount:0
        }
    },
    created: function () {
        // if ($.cookie("memToken")) {
        //     that.memToken = $.cookie("memToken");
        //     that.isLogin = 1;
        // }
        // if ($.cookie("memHeadImg")) {
        //     that.memHeadImg = $.cookie("memHeadImg");
        // }
        var that = this;
        if ($.cookie("memToken")) {
            that.memToken = $.cookie("memToken");
            that.isLogin = 1;
        }
        this.isLoginFn();
        if (!againWebLogin()) {
            return false;
        }
    },

    mounted() {
        this.regCountData();
        this.getPeople();
        this.getData()
        // this.getSignIn()
        this.getQueryQrCode()
        this.getCourseware()
        this.getConsulting()
        this.getUserInfo();
    },
    watch: {
        indexData() {
            this.$nextTick(() => {
                this.inswiper()
            })
        }
    },
    methods: {
        regCountData(){
            var that = this;
            setInterval(function () {
                that.getPeople();
            }, 10000)
        },
        getPeople(){
            var that = this;
            //注册人数
            $.ajax({
                type: 'get',
                url: serverHttp + "/index/peopleCoun",
                data: {
                },
                success: function (data) {
                    if (data.code == 200) {
                        that.regCount = data.data.registerPerson;
                        that.lineCount = data.data.onlinePerson;
                    }else {

                    }
                },
                error: function (data) {
                }
            });
        },
        gotoPay(item) {
            if ((item.isPurchased == 0 && item.pointFlag == 0 && item.price > 0) || (item.isPurchased == 0 && item.pointFlag == 1 && item.point > 0)) {
                this.toPay(item.id);
            } else if ((item.isPurchased == 0 && item.pointFlag == 0 && item.price == 0) || (item.isPurchased == 0 && item.pointFlag == 1 && item.point == 0)) {
                this.addFreeBuyApi(item.id);
            } else if (item.isPurchased == 1) {
                return;
            }
        },
        toPay(id) {
            if (!checkWebLogin()) {
                // $('.navlogorebox').trigger('click')
                return false;
            }
            // var that = this;
            toPayWeb(4, id, 1, '');
        },
        addFreeBuyApi(id) {
            if (!checkWebLogin()) {
                // $('.navlogorebox').trigger('click')
                return false;
            }
            var that = this;
            $.ajax({
                type: 'POST',
                url: serverHttp + "/pay/addFreeBuyApi",
                data: {
                    memToken: that.memToken,
                    memCard: that.memCard,
                    orderType: 4,
                    objId: id,
                    ispackage: 1,
                    paySource: 2,
                    fromMemCard: ''
                },
                success: function (data) {
                    if (data.code == 200) {
                        that.courseware.forEach((item, index) => {
                            if (item.id == id) {
                                item.isPurchased = 1;
                            }
                        });
                    }else {
                        alert(data.data.message);
                    }
                },
                error: function (data) {
                }
            });
        },
        downFile(id) {
            if (!checkWebLogin()) {
                // $('.navlogorebox').trigger('click')
                return false;
            }
            window.location.href = serverHttp + "/courseware/downFilebyWeb?id=" + id
        },
        jumpTo(url) {
            if (!checkWebLogin()) {
                // $('.navlogorebox').trigger('click')
                return false;
            }
            window.open(url)
        },
        jumpToLive(obj) {
            var that = this;
            if (!checkWebLogin()) {
                // $('.navlogorebox').trigger('click')
                return false;
            }
            if(obj.rcmdType == 6){
                if (!parseRule(obj.rcmdObjId,that.memToken,'2')) {
                    return false;
                }
            }
            gotoRcmdPub(obj.zqFlag, obj.rcmdType, obj.rcmdObjId, obj.rcmdUrl, "web")
        },
        isLoginFn() {
            var that = this;
            $.ajax({
                type: 'POST',
                url: "/index/isLogin",
                success: function (res) {
                    that.isLogin = res.code == 200;
                },
                error: function (data) {

                }
            });
        },
        getData() {
            var that = this;
            $.ajax({
                type: 'POST',
                url: "/web/index/homeData",
                data: {
                    indexData: that.indexData,
                    children: that.children
                },
                success: function (data) {
                    if (data.code == 200) {
                        that.indexData = data.data;
                        that.children = data.data.seriesRecommends.children

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

        },
        inswiper() {
            var mySwiper = new Swiper('.swiper', {
                direction: 'horizontal', // 水平切换选项
                loop: this.indexData.swriperRecommends.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',
                },
            })
            var mySwiper1 = new Swiper('.swiperIndex', {
                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',
                },
            })
            var mySwiper2 = 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',
                },
            })
        },
        getUserInfo() {
            var that = this;
            $.ajax({
                type: 'POST',
                url: "/user/getUserInfoWeb",

                success: function (res) {

                    if (res.code == 200) {
                        that.info = res.data;
                        // alert("走登录弹窗")
                        // if(res.data.yh){
                        //     playMusiclogin("首次登录", 200)
                        // }

                    }
                }
            })
        },
        // 个人签到
        getSignIn() {
            if (!checkWebLogin()) {
                return false;
            }
            var that = this;

            $.ajax({
                type: 'POST',
                url: "/user/singDayWeb",
                data: {
                    dayPoints: that.info.dayPoints
                },
                success: function (res) {
                    if (res.code == 200 || res.data == 2) {
                        playMusic("签到成功", that.info.dayPoints)
                        // layer.open({
                        // 	type: 1,
                        // 	area: ['300px', '180px'],
                        // 	skin: 'layui-layer-sign',
                        // 	shift: 0,
                        // 	time: 2000,
                        // 	title: false,
                        // 	anim: 1,
                        // 	closeBtn: 0,
                        // 	shadeClose: true,
                        // 	content: `<p style="margin-top:60px;text-align:center;font-size: 25px;font-weight: bold;color: #555555">签到成功</p>
                        //               <p style="margin-top:20px;text-align:center;color:#FFAC00; font-size: 20px;">
                        //               <img style="width: 25px" src="http://files.qkyxzx.com/html5/jb.png" /> +${that.info.dayPoints}</p>`,
                        // 	success: function () {
                        // 		playMusic()
                        // 	}
                        // });
                        that.getUserInfo();
                    } else {
                        alert("签到失败");
                    }
                },
                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) {
                }
            });
        },

        // 课件下载
        getCourseware() {
            var that = this;
            $.ajax({
                type: 'POST',
                url: "/courseware/getPageData?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
                    }
                },
                error: function (data) {
                }
            });
        },
        goView(str) {
            if (str == 'person') {
                window.location.href = '/user/personalCenter?from=web'
            } else if (str == 'buy') {
                window.location.href = '/user/buyRecord?from=web'
            } else if (str == 'jf') {
                sessionStorage.setItem('jf', '1')
                window.location.href = '/user/personalCenter?from=web'
            }
        }

    }
})
