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},
        ],
        liveId: '',
        liveInfo: {
            sysRecommendDTOs: [],
        },
        memberInfo: {
            isPurchased: 0,
            isRemind: 0,
            joinMembers: 0,
            remindMemberNum: 0
        },
        attestation: {},
        isLogin: 0,
        memToken: '',
        memHeadImg: '',
        userJf: -1,
        toast: false,
        url: "https://www.qkyxzx.com/live/detail?from=web",
        integralType: "",
        integralCount: "",
        numberTimer: null, // 人数定时器
        joinMembers: 0,
        hideBtn: 0,
        isAudit:false
    },
    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();
        this.findUserIntegral();
        this.getAttestation();
        if( typeof(fromlogin)!="undefined" && fromlogin=="1"){
            jifen();
        }
        var myobj = getparamcommon();
        if(myobj){
            this.hideBtn = myobj.hideBtn;
            if(this.hideBtn == 1){
                this.addFreeBuyApi();
            }
        }
    },
    mounted: function () {
        var that = this;
        console.log("add....mouted...");

        setTimeout(() => {
            this.findUserBaseInfo();
        }, 1000);
        setTimeout(() => {
            this.initFn();
        }, 1000);
        setTimeout(() => {
            $(".loading-item").fadeOut(200);
        }, 500)
    },
    destroy() {
        clearInterval(this.numberTimer)
    },
    watch: {
        liveInfo(val) {
            if (val.colorSwatches) {
                const len = val.colorSwatches.split(',').length;
                if (len <= 1) {
                    this.$refs.make.style.background = val.colorSwatches || '#000';
                } else {
                    this.$refs.make.style.background = 'linear-gradient(to bottom,' + val.colorSwatches + ')'
                }
            }
            if (val.colorTextSwatches) {
                this.$refs.make.style.color = val.colorTextSwatches || '#fff';
            }
        }
    },
    methods: {
        getAttestation() {
            var that = this;
            $.ajax({
                type : 'POST',
                url : serverHttp+"/popu/window/checkWindow",
                async: false,
                data : {
                    id: that.liveId,
                    idType: 1,  // 0 课程 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();
                    // }
                }
            })
        },
        copy() {
            var aux = document.createElement("input");
            aux.setAttribute('readonly', 'readonly');
            aux.value = this.url + "&liveId=" + this.liveId;
            document.body.appendChild(aux);
            aux.setSelectionRange(0, 9999);
            aux.select();
            document.execCommand("copy");
            document.body.removeChild(aux);
            this.toast = false;
        },
        showTast() {
            this.toast = true;
        },
        closeToast() {
            this.toast = false;
        },
        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() {
            if (!checkLogin()) {
                return false;
            }
            var that = this;
            $.ajax({
                type: 'POST',
                url: serverHttp + "/user/findUserBaseInfo",
                data: {
                    memToken: that.memToken
                },
                success: function (data) {
                    console.log("findUserBaseInfo..."+data);
                    that.memberInfo = data.data;
                },
                error: function (data) {
                }
            });
        },
        toPay() {
            if (!checkLogin()) {
                // goLogin();
                return false;
            }
            if (!this.parseRule()) {
                return false;
            }
            toPay(2, this.liveInfo.lvId, 1, '');
        },
        addFreeBuyApi() {
            if (!checkLogin()) {
                // goLogin();
                return false;
            }
            var that = this;
            if (!that.parseRule()) {
                return false;
            }
            $.ajax({
                type: 'POST',
                url: serverHttp + "/pay/addFreeBuyApi",
                data: {
                    memToken: that.memToken,
                    orderType: 2,
                    objId: that.liveId,
                    ispackage: 1,
                    paySource: 3,
                    fromMemCard: $.cookie("fromMemCard")
                },
                success: function (data) {
                    if (data.code == 200) {
                        that.liveInfo.isPurchased = 1;
                        that.integralType = "领取直播";
                        that.integralCount = 10;
                        $(".integral-toast").show();
                        playMusic()
                        setTimeout(function () {
                            $(".integral-toast").fadeOut(200);
                        }, 2000);
                    } else if (data.code == 10008) {
                        //去登录
                        goLogin();
                    } else {
                        alert(data.data.message);
                    }
                },
                error: function (data) {
                }
            });
        },
        parseRule() {
            var that = this;
            this.getAttestation();
            if (that.attestation.attestType == 2) {
                window.location.href = serverHttp + "/user/certificationSm?from=H5&memToken=&type=1&id=" + that.liveId;
                return false;
            } else if (that.attestation.attestType == 1) {
                window.location.href = serverHttp + "/user/certificationYs?from=H5&memToken=&type=1&id=" + that.liveId;
                return false;
            } else if (that.attestation.attestType == 4) {
                window.location.href = serverHttp + "/user/certificationSmojl?from=H5&memToken=&type=1&id=" + that.liveId;
                return false;
            } else if (that.attestation.attestType == 3) {
                window.location.href = serverHttp + "/user/certificationYsojl?from=H5&memToken=&type=1&id=" + that.liveId;
                return false;
            } else if (that.attestation.attestType == 5 && that.attestation.question) {
                window.location.href = serverHttp + "/live/liveQuestion?from=H5&memToken=&type=1&id=" + that.liveId;
                return false;
            }else if (that.attestation.attestType == 5 && !that.attestation.question && that.attestation.needAuditing == 1) {
                if(that.attestation.auditingStatus == 1){
                    that.isAudit = true
                }else if(that.attestation.auditingStatus == 3){
                    window.location.href = serverHttp + "/live/liveQuestion?from=H5&memToken=&type=1&id=" + that.liveId;
                }else{
                    return true;
                }
                return false;
            }
            return true;
        },
        closeAudit(){
            this.isAudit = false
        },
        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',
                },

            })
        },
        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;
                        var isQuestionAnswer = sessionStorage.getItem('isQuestionAnswer');
                        if (isQuestionAnswer) {
                            that.liveInfo.isQuestionAnswer = isQuestionAnswer;
                        }
                        $(document).attr("title", that.liveInfo.liveTitle);
                        $("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);
                        // 实时人数

                        this.getcounter()
                        this.numberTimer = setInterval(this.getcounter, 30000)

                        //获取问卷调查是否完成
                        that.infoByMember();
                    }
                },
                error: function (data) {
                }
            });
        },
        infoByMember() {
            var that = this;
            $.ajax({
                type: 'POST',
                url: serverHttp + "/live/infoByMember",
                data: {
                    memToken: that.memToken,
                    liveId: that.liveId
                },
                success: function (data) {
                    if (data.code == 200) {
                        that.liveInfo.isQuestionAnswer = data.data.isQuestionAnswer;
                    }
                },
                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)){
                alert('请先点击下方的参会申请！');
                return false;
            }

            if (!this.parseRule()) {
                return false;
            }

            window.location.href = serverHttp + '/live/detail?from=H5&liveId=' + obj.lvId;
        },
        gotoRcmd(obj) {
            gotoRcmdPub(obj.zqFlag, obj.rcmdType, obj.rcmdObjId, obj.rcmdUrl, "H5", obj.artType, obj.artContent);
        },
    }
})
