Vue.config.devtools = true;
var v = new Vue({
    el:".container",
    data:{
        professional:-1,
        professionalTwo:-1,
        businessLicense:"",
        isLogin: 0,
        memToken: '',
        memHeadImg: '',
        userJf:-1,
        liveId: '',
        videoType: '',
        liveInfo: '',
        integralType:"",
        integralCount:"",
        attestation: {},
        needAuditing:'',
        isAudit:false,
        isAuditSumbit:false,
    },
    created:function (){
        if(!checkLogin()){
            // goLogin();
            return false;
        }
        if($.cookie("memToken")){
            this.memToken = $.cookie("memToken");
            this.isLogin=1;
        }
        if($.cookie("memHeadImg")){
            this.memHeadImg = $.cookie("memHeadImg");
        }
        var myobj = getparamcommon();
        if(myobj){
            this.liveId = myobj.id;
            this.videoType = myobj.type;
        }

        this.getDetailData();
        this.getAttestation();

    },
    mounted:function (){
        setTimeout(() => {
            $(".loading-item").fadeOut(200);
        },500)
    },
    methods:{
        getDetailData() {
            var that = this;
            $.ajax({
                type: 'POST',
                url: serverHttp + "/live/getDetailData",
                data: {
                    memToken: that.memToken,
                    liveId: that.liveId
                },
                success: function (data) {
                    if (data.code == 200) {
                        that.liveInfo = data.data;
                        that.initQuestion();
                    }
                },
                error: function (data) {
                }
            });
        },
        getAttestation() {
            var that = this;
            $.ajax({
                type: 'POST',
                url: serverHttp + "/popu/window/checkWindow",
                data: {
                    id: that.liveId,
                    idType: 1,  // 0 课程 1直播
                    memToken: that.memToken
                },
                success: function (data) {
                    if (data.code == 200) {
                        that.needAuditing = data.data.needAuditing;
                        if(that.needAuditing == 1){
                            if(data.data.auditingStatus == 3){
                                $(".refusalReason").html('驳回原因：' + data.data.refusalReason + '，请重新提交审核。');
                                $(".refusalReason").show();
                            }else if(data.data.auditingStatus == 1){
                                $(".auditSumbitTip").html("已提交审核，请等待审核结果")
                                $(".auditSumbitBox").show();
                            }else if(data.data.auditingStatus == 2){
                                $(".auditSumbitTip").html("审核已通过，请关闭返回直播页面")
                                $(".auditSumbitBox").show();
                            }
                        }
                    }
                },
                error: function (data) {
                }
            });
        },
        closeAuditSumbit() {
            window.location.href = serverHttp + "/live/detail?from=H5&memToken=&hidBtn=1&liveId=" + this.liveId;
        },
        initQuestion() {
            var that = this;
            var questionTooltip = that.liveInfo.questionTooltip;
            if(that.liveInfo.questionTooltip == '' || that.liveInfo.questionTooltip == null){
                questionTooltip = ''
            }
            $(".questionTooltip").html(questionTooltip);
            $("#writeform").empty()
            var index = 0;
            for (var item of that.liveInfo.questions) {
                var getAnswerContent = item.answerContent
                if(item.answerContent == null){
                    getAnswerContent = '';
                }
                // if (item.answerContent == undefined || item.answerContent == null || item.answerContent === '') {
                    $("#writeform").append("<div class='item'>" +
                        "<span>" + item.quTitle + "：</span>" +
                        "<div class='itemVal'><input value='" + getAnswerContent + "' data-index='" + index + "' type=\"text\" placeholder=\"请输入\"/>" +
                        "</div></div>");
                // }
                index++;
            }
        },
        save() {
            var that = this;
            var check = true;
            var check2 = true;
            var inputs = $("#writeform input");

            for (let i = 0; i < inputs.length; i++) {
                that.liveInfo.questions[$(inputs[i]).data("index")].answerContent = $(inputs[i]).val().trim();
            }

            for (var item of that.liveInfo.questions) {
                if (item.answerContent == undefined || item.answerContent == null || item.answerContent === '') {
                    check = false;
                    break;
                }
                if(item.quTitle.indexOf("手机号") !=-1 || item.quTitle.indexOf("电话") !=-1 || item.quTitle.indexOf("联系方式") !=-1){
                    var regPhone = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
                    if(!regPhone.test(item.answerContent)){
                        check2 = false;
                        break;
                    }
                }
            }
            if (!check) {
                alert("请完善相关信息");
                return false;
            }
            if (!check2) {
                alert("请输入正确的手机号码");
                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) {
                        sessionStorage.setItem('isQuestionAnswer', 1);
                        if(that.videoType == 1){
                            if(that.needAuditing == 0){
                                window.location.href = serverHttp + "/live/detail?from=H5&memToken=&hidBtn=1&liveId=" + that.liveId;
                            }else{
                                $(".auditSumbitBox").show();
                            }
                        }else{
                            window.location.href = serverHttp + "/course/courseDetails?from=H5&memToken=&hidBtn=1&id=" + that.liveId;
                        }
                    }
                },
                error: function (data) {
                }
            });
        }
    }
})
