Vue.config.devtools = true;
function getQueryVariable(variable) {
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i=0;i<vars.length;i++) {
        var pair = vars[i].split("=");
        if(pair[0] == variable){return pair[1];}
    }
    return(false);
}
var v = new Vue({
    el:".container",
    data:{
        isFirst:1,
        isFinish:1,
        paper: '',
        questionList: [],
        optionArr: [],
        paper2: '',
        questionList2: [],
        optionArr2: [],
        memToken:null,
        id:null,
        objId:null,
        type:1,
        scoreAll:'',
        scoreAll2:''
    },
    created:function (){
        this.memToken = getQueryVariable("memToken") + '==';
        this.id = getQueryVariable("id");
        this.objId = getQueryVariable("objId");
        this.getQuestion();
    },
    mounted:function (){

    },
    methods: {
        getAnswer(id,key){
            for (var i = 0; i < this.optionArr.length; i++) {
                if (this.optionArr[i].quId == id) {
                    this.optionArr[i].optionId = key.optionId
                    this.optionArr[i].answerContent = key.optionTitle
                    this.optionArr[i].score = key.score
                }
            }
            console.log(this.optionArr)
        },
        getAnswer2(id,key){
            for (var i = 0; i < this.optionArr2.length; i++) {
                if (this.optionArr2[i].quId == id) {
                    this.optionArr2[i].optionId = key.optionId
                    this.optionArr2[i].answerContent = key.optionTitle
                    this.optionArr2[i].score = key.score
                }
            }
            console.log(this.optionArr2)
        },
        getQuestion(){
            var that = this;
            $.ajax({
                type : 'get',
                url : serverHttp+"/app/QuestionnaireApp/findQuestions",
                data :{
                    type:that.type,
                    objId:that.objId
                },
                headers:{
                    "memToken":that.memToken
                },
                success:function (res) {
                    console.log(res)
                    // 获取键值对  第几套试题
                    let values = Object.values(res.data)
                    let keys = Object.keys(res.data)
                    that.paper =  keys[0];
                    that.questionList =  values[0];
                    that.paper2 =  keys[1];
                    that.questionList2 =  values[1];
                    // 设置数组，用来判断是否全部答题完成  提交
                    var newArr = [];
                    for (var i = 0; i < that.questionList.length; i++) {
                        var optionId = '';
                        var answerContent = '';
                        var score = '';
                        var answerArr = that.questionList[i].option
                        for (var j = 0; j < answerArr.length; j++) {
                            if (answerArr[j].checked == true) {
                                optionId = answerArr[j].optionId
                                answerContent = answerArr[j].optionTitle
                                score = answerArr[j].score
                            }
                        }
                        newArr.push({
                            patientId: that.id,
                            type: that.type,
                            objId: that.objId,
                            quId: that.questionList[i].quId,
                            quTitle: that.questionList[i].quTitle,
                            optionId: optionId,
                            answerContent: answerContent,
                            score: score,
                            frequency:2
                        })
                    }
                    that.optionArr = newArr
                    //问卷2
                    var newArr2 = [];
                    for (var i = 0; i < that.questionList2.length; i++) {
                        var optionId2 = '';
                        var answerContent2 = '';
                        var score2 = '';
                        var answerArr2 = that.questionList2[i].option
                        for (var j = 0; j < answerArr2.length; j++) {
                            if (answerArr2[j].checked == true) {
                                optionId2 = answerArr2[j].optionId
                                answerContent2 = answerArr2[j].optionTitle
                                score2 = answerArr2[j].score
                            }
                        }
                        newArr2.push({
                            patientId: that.id,
                            type: that.type,
                            objId: that.objId,
                            quId: that.questionList2[i].quId,
                            quTitle: that.questionList2[i].quTitle,
                            optionId: optionId2,
                            answerContent: answerContent2,
                            score: score2,
                            frequency:3
                        })
                    }
                    that.optionArr2 = newArr2
                }
            })
        },
        submit(){
            var that = this;
            var check = true;
            for (var i = 0; i < that.optionArr.length; i++) {
                if (that.optionArr[i].optionId == '') {
                    check = false;
                    break;
                }
            }
            if (!check) {
                alert("请完成问卷")
                return false;
            }
            $.ajax({
                type : 'POST',
                url : serverHttp+"/app/QuestionAnswer/addQuestionAnswer",
                data : JSON.stringify(that.optionArr),
                headers:{
                    "memToken":that.memToken,
                    "Content-Type":"application/json;charset=UTF-8"
                },
                success:function (res) {
                    if (res.code == 200) {
                        //切换第二个问卷
                        $('html,body,.container').animate({ scrollTop: 0 }, 0);
                        that.isFirst = 2
                    } else {
                        alert(res.message);
                    }
                }
            })
        },
        submit2(){
            var that = this;
            var check = true;
            for (var i = 0; i < that.optionArr2.length; i++) {
                if (that.optionArr2[i].optionId == '') {
                    check = false;
                    break;
                }
            }
            if (!check) {
                alert("请完成问卷")
                return false;
            }
            $.ajax({
                type : 'POST',
                url : serverHttp+"/app/QuestionAnswer/addQuestionAnswer",
                data : JSON.stringify(that.optionArr2),
                headers:{
                    "memToken":that.memToken,
                    "Content-Type":"application/json;charset=UTF-8"
                },
                success:function (res) {
                    if (res.code == 200) {
                        that.getFinish();
                    } else {
                        alert(res.message);
                    }
                }
            })
        },
        getFinish(){
            var that = this;
            $.ajax({
                type : 'GET',
                url : serverHttp+"/app/QuestionAnswer/findQuestionScore",
                data : {
                    patientId: that.id,
                    objType: that.type,
                    objId: that.objId,
                },
                headers:{
                    "memToken":that.memToken,
                },
                success:function (res) {
                    that.scoreAll = res.data[0].score;
                    that.scoreAll2 = res.data[1].score;
                    that.isFinish = 2;
                    console.log(res)
                }
            })
        }
    }
})
