<template>
    <div class="main" v-if="loaded==true">
        <div class="test-top-box white-bg">
            <h2 class="test-title align-center f20 weight-normal">{{paperInfo.title}}</h2>
            <div class="test-intro line-height26 darkgray-font">
                <p>◇ 本卷共分为{{testList.length}}大题{{paperInfo.tk_num||0}}小题，作答时间为{{paperInfo.exam_time||0}}分钟，总分{{paperInfo.sumscore||0}}分</p>
                <p>◇ 试卷来源：{{paperInfo.copyfrom||'暂无来源'}}</p>
            </div>
            <div class="test-tips pink-bg align-center line-height28 red-font f12" v-if="subjectNum">
                本套试卷含有主观题，针对主观题请自行参考解析评分
            </div>
            <ul class="tab-btn-box clearfix gray-bg">
                <li :class="curr==item.value?'fl red-bg white-font':'fl darkgray-bg white-font'" v-for="item in option" :key="item.value" @click="getCurr(item.value)">{{item.title}}</li>
            </ul>
        </div>
        <div class="clearfix test-con">
            <div class="test-con-left fl">
                <div v-for="(item,n) in testList" :key="n" class="test-type-box">
                    <div class="test-type-tips white-bg line-height26 darkgray-font" v-if="curr==n">
                        <p>{{item.order}}、{{item.title}}（本大题共{{item.tklist.length}}小题，共{{item.dtscore}}分 {{item.dtintro}}）</p>
                    </div>
                    <ul class="test-list">
                        <li v-for="(citem,i) in item.tklist" :key="citem.tkid" class="white-bg" v-show="n==curr" :ref="'djump'+citem.tkid">
                            <ks-radio :item="citem" :index="i" :isshow_collect="isshow_collect" :is_saveanswer="is_saveanswer" :is_powertf="is_powertf" v-if="citem.tktype==2||citem.tktype==4" @flagStatus="flagStatus" @collectStatus="collectStatus" @redressStatus="redressStatus" @ifDoexam="ifDoexam"/>
                            <ks-check :item="citem" :index="i" :isshow_collect="isshow_collect" :is_saveanswer="is_saveanswer" :is_powertf="is_powertf" v-else-if="citem.tktype==3" @flagStatus="flagStatus" @collectStatus="collectStatus" @redressStatus="redressStatus" @ifDoexam="ifDoexamCheck"/>
                            <ks-subjective-questions :item="citem" :isshow_collect="isshow_collect" :is_saveanswer="is_saveanswer" :is_powertf="is_powertf" :index="i" v-else-if="citem.tktype==1" @flagStatus="flagStatus" @collectStatus="collectStatus" @redressStatus="redressStatus" @ifDoexam="ifDoexamStuff"/>
                            <ks-stuff :data="citem" :index="i" :isshow_collect="isshow_collect" :is_saveanswer="is_saveanswer" :is_powertf="is_powertf" v-else-if="citem.tktype==5" @flagStatus="flagStatus" @collectStatus="collectStatus" @redressStatus="redressStatus" @ifDoexam="ifDoexamStuff"/>
                            <div v-else-if="citem.tktype==6">
                                <ks-subjective-questions :item="citem" :isshow_collect="isshow_collect" :is_saveanswer="is_saveanswer" :is_powertf="is_powertf" :realtest="paperInfo.realtest" :is_tjda="paperInfo.is_tjda" :paperid="getParameters.paperid" :schoolid="getParameters.schoolid" :index="i" @flagStatus="flagStatus" @collectStatus="collectStatus" @redressStatus="redressStatus" @ifDoexam="ifDoexamStuff" @getResult="getResult"/>
                            </div>
                            <div v-if="citem.tktype!=6">
                                <div v-if="citem.showAnalytical==true" class="mb20">
                                    <ks-analyze-select :item="citem.datainfo" :index="i" :action="1" v-if="citem.tktype==2||citem.tktype==3||citem.tktype==4" />
                                    <ks-analyze-subjective :item="citem.datainfo" :action="1" :index="i" v-else-if="citem.tktype==1||citem.tktype==5||citem.tktype==6"/>
                                </div>
                                <div v-if="paperInfo.realtest==0&&paperInfo.is_tjda==1&&!citem.showAnalytical">
                                    <ks-button type="danger" plain @click="submitAnswer(citem)">提交看答案</ks-button>
                                </div>
                            </div>
                            
                        </li>
                    </ul>
                </div>
            </div>
            <div class="test-con-right fr">
                <div :class="isFixed==true?'test-right-inner isFixed':'test-right-inner'" ref="test-right-inner">
                    <div class="top-application white-bg align-center line-height26">
                        <p class="f20 yellow-font">{{countdown}}</p>
                        <p class="darkgray-font f14">做题用时</p>
                    </div>
                    <div class="answer-sheet-box white-bg">
                        <h2 class="align-center f16 weight-normal">答22题卡</h2>
                        <div class="answer-tips mt20 mb20">
                            <span class="mr20"><i class="red-bg vertical-align"></i><font class="vertical-align">已做</font></span>
                            <span class="mr20"><i class="green-bg vertical-align"></i><font class="vertical-align">标记</font></span>
                            <span class="mr20"><i class="yellow-bg vertical-align"></i><font class="vertical-align">收藏</font></span>
                            <span class="mr20"><i class="lightgreen-bg vertical-align"></i><font class="vertical-align">标记且收藏</font></span>
                        </div>
                        <div class="answer-sheet-inner" :style="'max-height:'+maxHeight+'px'">
                            <div class="answer-sheet" v-for="(item,n) in testList" :key="n">
                                <h3 class="weight-normal f14">{{item.title}} <span class="darkgray-font f12">(共{{item.tklist.length}}题)</span></h3>
                                <ul class="clearfix">
                                    <div v-for="(citem,i) in item.tklist" :key="i">
                                        <li v-if="citem.ifdoexam==1" class="fl align-center red-bg white-font red-border" @click="jump(citem.tkid,n)">{{i+1}}</li>
                                        <div v-else>
                                            <li v-if="citem.flag==0 && citem.collect==0" class="fl align-center" @click="jump(citem.tkid,n)">{{i+1}}</li>
                                            <li v-else-if="citem.flag==1 && citem.collect==0" class="fl align-center green-bg white-font green-border" @click="jump(citem.tkid,n)">{{i+1}}</li>
                                            <li v-else-if="citem.flag==0 && citem.collect==1" class="fl align-center yellow-bg white-font yellow-border" @click="jump(citem.tkid,n)">{{i+1}}</li>
                                            <li v-else-if="citem.flag==1 && citem.collect==1" class="fl align-center lightgreen-bg white-font lightgreen-border" @click="jump(citem.tkid,n)">{{i+1}}</li>
                                        </div>
                                    </div>
                                </ul>
                            </div>
                        </div>
                        <div v-if="preview!=1">
                            <ks-button type="danger" plain btnstyle="width:100%;" v-if="is_saveanswer==1" @click="saveTest(0)" class="mb20">保存试卷</ks-button>
                            <ks-button type="danger" btnstyle="width:100%;" @click="finish();isShow = true;powertf_switch=1">立即交卷</ks-button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <ks-dialog :visible.sync="isShow" title="结束作答" width="500px" align="center" top="30%">
            <div class="tips-con">
                <p v-if="unfinished" class="align-center weight-normal">您还有<font class="red-font">{{unfinished}}</font>道题目未作答，是否确定交卷？</p>
                <p v-else class="align-center weight-normal">是否确定交卷？</p>
                <div class="dialog-btn">
                    <ks-button type="info" class="mr10" @click="isShow=false;powertf_switch=0">继续做题</ks-button>
                    <ks-button type="danger" class="ml10" @click="save(1)">确定交卷</ks-button>
                </div>
            </div>
        </ks-dialog>
        <ks-dialog :visible.sync="isShowOpera" title="提示" width="300px" align="center" top="30%" isshowclose="1">
            <div class="tips-con">
                <i class="f50 iconfont vertical-align" :class="operatstatus==0 && 'red-font icon-shibai'||operatstatus==1 && 'green-font icon-chenggong'||operatstatus==3 && 'red-font icon-jinggao'"></i>
                <font class="f16 vertical-align ml10">{{operaTips}}</font>
            </div>
            
        </ks-dialog>
        <ks-dialog :visible.sync="isShowRedress" title="题目纠错" width="600px" align="center" top="20%" v-if="showClass">
            <div class="redress-con">
                <p class="line-height26 f14 align-left">一直在完善，从未停歇过，但有些题目可能仍然存在瑕疵，对您造成的不便我们深表歉意！</p>
                <p class="line-height28 darkgray-font align-left">您纠错的题目ID:{{redressid}}</p>
                <div class="line-height28 align-left redress-option">
                    <p class="darkgray-font">为方便我们排查错误，请您详细描述本题错误，例如：</p>
                    <div class="line-height28">
                        <label v-for="item in radioOption" :key="item.id" class="mr20"><input type="radio" :value="item.id" v-model="redressType" name="radioOption"/>{{item.name}}</label>
                    </div>
                </div>
                <div class="redress-con_con align-left">
                    <textarea rows="10" v-model="redresscon" class="redress-textarea"></textarea>
                </div>
                <div class="dialog-btn">
                    <ks-button type="danger" class="ml10" @click="submitError">提交纠错</ks-button>
                </div>
            </div>
        </ks-dialog>
        <ks-dialog :visible.sync="isPayPaper" title="提示" width="450px" align="center" top="30%" isshowclose="1">
            <div class="tips-con">
                <!-- <p class="line-height26 f15 p10">本卷需要消费资金{{paperInfo.price}}{{paperInfo.chargetype_unit}}，您当前账户可用{{paperInfo.chargetype_name}}
                    <font v-if="paperInfo.chargetype_name=='点'">{{$store.state.point}}</font>
                    <font v-if="paperInfo.chargetype_name=='资金'">{{$store.state.balance}}</font>
                    <font v-else>{{$store.state.score}}</font>
                    {{paperInfo.chargetype_unit}}！
                </p> -->
                <p class="line-height26 f15 p10">
                    {{payInfo}}
                </p>
                
                <div class="dialog-btn mt20">
                    <ks-button type="danger" size="small" class="mr10" @click="payPaper">确定</ks-button>
                    <ks-button type="info" size="small" class="ml10" @click="cancelPay">取消</ks-button>
                </div>
            </div>
        </ks-dialog>
    </div>
</template>
<script>
    import {GetPaperDetail,SetPaperAnswer,SetPaperCollect,GetPaperErrortype,SetTkCorrection,SubmitPaperAnswer,PayPaper} from '@/api/exam'
    export default {
        name:'TestPage',
        data(){
            return{
                loaded:false,
                curr:0,
                getParameters:{
                    paperid:this.$route.query.paperid,
                    categoryid:this.$route.query.categoryid,
                    answerid:this.$route.query.answerid||0,
                    preview: this.$route.query.preview || 0,
                    courseid: this.$route.query.courseid,
                    
                },
                option:[],
                testList:[],
                time:0,
                paperTime:0,
                countdown:'00:00:00',
                paperInfo:{},
                finishPaper:{},
                isFixed:false,
                isShow:false,
                isShowOpera:false,
                isShowRedress:false,
                operatstatus:0,
                operaTips:'',
                unfinished:0,
                subjectNum:0,
                categoryid:0,
                radioOption:[],
                redressid:0,
                redressType:19,
                redresscon:'',
                cloneTestList:[],
                is_saveanswer:0,
                is_powertf:0,
                powertf_switch:0,
                autosavetime:300000,
                isshow_collect:0,//判断收藏的开关
                preview:this.$route.query.preview,
                showClass:true,
                isPayPaper:false,
                payInfo:'',
                maxHeight:0
            }
        },
        watch:{
            'redressid'(){
                this.showClass = false;
                this.$nextTick(()=>{
                    this.showClass = true
                    this.redresscon = ''
                })
            }
        },
        methods:{
            /**购买试卷 */
            payPaper(){
                this.isPayPaper=false
                PayPaper({
                    paperid:this.$route.query.paperid
                }).then(res=>{
                    if(res.result){
                        this.isShowOpera = true
                        this.operatstatus = 1
                        this.operaTips = '支付成功'
                        setTimeout(()=>{
                            this.isShowOpera = false
                            this.loadPage()
                        },1500)
                    }else{
                        this.isShowOpera = true
                        this.operatstatus = 0
                        this.operaTips = res.msg
                        setTimeout(()=>{
                            this.isShowOpera = false
                        },1500)
                    }
                })
            },
            /**取消购买试卷 */
            cancelPay(){
                this.$router.push({
                    name:'PaperInfo',
                    query:{
                        paperid:this.getParameters.paperid,
                        categoryid:this.getParameters.categoryid,
                        schoolid:this.$route.query.schoolid,
                        preview:this.$route.query.preview
                    }
                })
            },
            /**选择题型 */
            getCurr(val){
                this.curr = val
            },
            /**把阿拉伯数字全部转换为大写（1，2，3.... = 一、二、三、） */
            num(n) {
                if(!/(^[1-9]\d*$)/){
                    return '非法数字';
                }
                var uppercase='千百亿千百十万千百十个';
                var nLength=n.length;
                var newStr='';
                if(uppercase.length-nLength<0){
                    return '数字过长';
                }
                uppercase=uppercase.substr(uppercase.length-nLength);
                for(var i=0;i<nLength;i++){
                    newStr +='零一二三四五六七八九'.charAt(n[i])+uppercase.charAt(i);
                };
                newStr=newStr.substr(0,newStr.length-1);
                return newStr;
            },
            /**锚点滚动 */
            scrollingAnimation(a,h,direction){
               
                setTimeout(()=>{
                     if(direction === 'up'){
                         a -= 15
                        if(a > h){
                            document.documentElement.scrollTop = a
                            this.scrollingAnimation(a,h,'up')
                        }
                    }else{
                        a += 15
                        if(a < h){
                            document.documentElement.scrollTop = a
                            this.scrollingAnimation(a,h)
                        }
                    }
                },5)
            },
            
            jump(tkid,type){
                this.curr = type
                this.$nextTick(()=>{
                    let h = this.$refs['djump'+tkid][0].offsetTop - window.innerHeight/2
                    if(h < window.scrollY){
                        this.scrollingAnimation(window.scrollY,h,'up')
                    }else{
                        this.scrollingAnimation(window.scrollY,h)
                    }
                })
            },
            /**右侧答题卡悬浮 */
            handleScroll () {
                let scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop
                let offsetTop = this.$refs['test-right-inner'].offsetTop
                scrollTop > offsetTop ? this.isFixed = true : this.isFixed = false
            },
            /**倒计时的 */
            p(t) {
                return t < 10 ? '0' + t: t;
            },
            times(){
                setTimeout(()=>{
                    this.time -= 1
                    
                    var h = Math.floor(this.time/3600)
                    var m = Math.floor((this.time- h*3600)/60)
                    var s = this.time- h*3600- m*60

                    var str = this.p(h) + ':' + this.p(m) + ':' + this.p(s);
                    this.countdown = str
                    if(this.time > 0){
                        this.times()
                    }else{
                        if(this.time>-1){
                            this.finish()
                            this.isShowOpera = true
                            this.operatstatus = 3
                            this.operaTips = '做卷时间已到'
                            setTimeout(()=>{
                                this.save(1)
                            },2000)
                        }else{
                            return false
                        }
                        
                        
                    }
                },1000)
                
            },
            /**获取试卷 */
            loadPage(){
                GetPaperDetail(this.getParameters).then(res=>{
                    if(res.result){
                        
                        this.isshow_collect = res.data.isshow_collect
                        this.is_saveanswer = res.data.is_saveanswer
                        this.autosavetime = res.data.autosavetime*60*1000
                        this.is_powertf = res.data.is_powertf
                        if(res.data.is_powertf==1){
                            this.powerProtection()
                        }
                        if(res.data.is_righttf==1){
                            this.antiCopy(true)
                        }
                        if(res.data.is_f5tf==1){
                            document.onkeydown = function (e) {
                                var ev = window.event || e;
                                var code = ev.keyCode || ev.which;
                                if (code == 116) {
                                    if(ev.preventDefault) {
                                        ev.preventDefault();
                                    } else {
                                        ev.keyCode = 0;
                                        ev.returnValue = false;
                                    }
                                }
                            }

                        }
                        let newArr = []
                        if(res.data.tmlist&&res.data.tmlist.length){
                            if(res.data.tmlist.length==1&&!res.data.tmlist[0].tklist.length){
                                this.isShowOpera = true
                                this.operatstatus = 0
                                this.operaTips = '暂无试题'
                                setTimeout(()=>{
                                    this.$router.push({
                                        name:'PaperInfo',
                                        query:{
                                            paperid:this.getParameters.paperid,
                                            categoryid:this.getParameters.categoryid,
                                            schoolid:this.$route.query.schoolid,
                                            preview:this.$route.query.preview
                                        }
                                    })
                                },1500)
                                
                            }else{
                                res.data.tmlist.map((item,i)=>{
                                    newArr.push({
                                        title:item.title,
                                        value:i
                                    })
                                })
                            }
                            
                        }
                        
                        this.option = newArr
                        res.data.tmlist.map((item,i)=>{
                            let n=(i+1).toString()
                            item.order = this.num(n)
                            
                            item.tklist.map((citem,j)=>{
                                citem.flag = 0
                                if(citem.useranswer){
                                    citem.ifdoexam = 1
                                }else{
                                    citem.ifdoexam = 0
                                }
                                
                            })
                            
                            
                        })
                        this.time = res.data.exam_time*60-res.data.user_time
                        this.paperTime = res.data.exam_time*60
                        
                        this.testList = res.data.tmlist
                        this.paperInfo = res.data
                        document.title = this.paperInfo.title
                        let n = 0
                        this.testList.map(item=>{
                            
                            item.tklist.map(citem=>{
                                if(citem.tktype==1){
                                    n++
                                }else{
                                    return n
                                }
                            })
                        })
                        this.subjectNum = n
                        this.categoryid = res.data.categoryid

                        /**这个是标记的缓存 */
                        let arr = localStorage['userid'+(this.$store.state.userid||0)+'paper'+this.getParameters.paperid]
                        if(arr){
                            arr.split(',').map(tkid=>{
                                if(tkid){
                                    tkid = JSON.parse(tkid)
                                }
                                this.testList.map(item=>{
                                    item.tklist.map(citem=>{
                                        if(citem.tkid==tkid){
                                            citem.flag=1
                                        }
                                    })
                                })
                            })
                        }
                        if(this.time){
                            this.times()
                        }
                    }else{
                        this.testList = []
                        this.payInfo = res.msg
                        if(res.returncode==1002||res.returncode==1062){
                            this.isShowOpera = true
                            this.operatstatus = 0
                            this.operaTips = res.msg
                            setTimeout(()=>{
                                window.location.href = res.url;
                            },1500)
                        }else if(res.returncode==1063||res.returncode==1064||res.returncode==1065){
                            
                            this.isPayPaper = true
                            // setTimeout(()=>{
                            //     this.$router.push({
                            //         name:'PaperInfo',
                            //         query:{
                            //             paperid:this.getParameters.paperid,
                            //             categoryid:this.getParameters.categoryid,
                            //             retrurncode:res.retrurncode,
                            //             schoolid:this.$route.query.schoolid,
                            //             preview:this.$route.query.preview
                            //         }
                            //     })
                            // },1500)
                        }else{
                            let msg
                            if(res.returncode==-1){
                                msg = '您还未登录，请先登录'
                            }else{
                                msg = res.msg
                            }
                            this.isShowOpera = true
                            this.operatstatus = 0
                            this.operaTips = msg
                            setTimeout(()=>{
                                this.$router.push({
                                    name:'PaperInfo',
                                    query:{
                                        paperid:this.getParameters.paperid,
                                        categoryid:this.getParameters.categoryid,
                                        schoolid:this.$route.query.schoolid,
                                        preview:this.$route.query.preview
                                    }
                                })
                            },1500)
                        }
                    }    
                })
                //获取纠错的类型
                GetPaperErrortype().then(res=>{
                    if(res.result){
                        this.radioOption = res.data
                    }else{
                        this.radioOption = []
                    }
                })
                this.loaded = true
                
            },
            /**纠错 */
            redressStatus(tkid){
                this.redressid = tkid
                this.isShowRedress = true
            },
            /**提交纠错 */
            submitError(){
                SetTkCorrection({
                    tkid:this.redressid,
                    categoryid:this.getParameters.categoryid,
                    content:this.redresscon,
                    errortype:this.redressType,
                    schoolid:this.getParameters.schoolid,
                    apptoken:this.$store.state.apptoken
                }).then(res=>{
                    if(res.result){
                        this.isShowRedress = false
                        this.isShowOpera = true
                        this.operatstatus = 1
                        this.operaTips = '提交成功'
                        setTimeout(()=>{
                            this.isShowOpera = false
                        },1500)
                    }else{
                        this.isShowRedress = false
                        this.isShowOpera = true
                        this.operatstatus = 0
                        this.operaTips = res.msg
                        setTimeout(()=>{
                            this.isShowOpera = false
                        },1500)
                    }
                })
            },
            /**标记 */
            flagStatus(val,tkid){
                
                let cookiename = 'userid'+(this.$store.state.userid||0)+'paper'+this.getParameters.paperid
                let cookieArr = []
                let cookieString = ''
                
                this.testList.map((item,i)=>{
                    item.tklist.map((citem,j)=>{
                        if(citem.tkid == tkid){
                            citem.flag = val
                        }
                        if(citem.flag==1){
                            cookieArr.push(citem.tkid)
                        }
                    })
                })
                
                cookieString = cookieArr.toString()
                localStorage[cookiename] = cookieString
            },
            /**收藏 */
            collectStatus(val,tkid){
                let tipstext = ''
                if(val==1){
                    tipstext = '收藏成功'
                }else{
                    tipstext = '取消收藏'
                }
                
                SetPaperCollect({
                    type:2,//收藏类型，为1表示试卷收藏，为2表示题目收藏
                    infoid:tkid,//题库id
                    categoryid:this.getParameters.categoryid,
                    schoolid:this.getParameters.schoolid,
                    apptoken:this.$store.state.apptoken,
                    collect:val
                }).then(res=>{
                    if (res.result) {
                        this.testList.map((item,i)=>{
                            item.tklist.map((citem,j)=>{
                                if(citem.tkid == tkid){
                                    citem.collect = val
                                }
                            })
                        })
                        this.isShowOpera = true
                        this.operatstatus = 1
                        this.operaTips = tipstext
                        setTimeout(()=>{
                            this.isShowOpera = false
                        },1500)
                    }else{
                        this.isShowOpera = true
                        this.operatstatus = 0
                        this.operaTips = res.msg
                        setTimeout(()=>{
                            this.isShowOpera = false
                        },1500)
                    }
                })
            },
            /**---------------判断是否已做题------------------ */
            /**判断单选 */
            ifDoexam(order,tkid){
                
                this.testList.map((item,i)=>{
                    item.tklist.map((citem,j)=>{
                        if(citem.tkid == tkid){
                            if(order){
                                citem.useranswer = [order]
                            }else{
                                citem.useranswer = []
                            }
                            
                            citem.ifdoexam = 1
                        }
                    })
                })
            },
            /**判断多选 */
            ifDoexamCheck(tkid,n,orderArr){
                this.testList.map((item,i)=>{
                    item.tklist.map((citem,j)=>{
                        if(citem.tkid == tkid){
                            if(n){
                                citem.useranswer = orderArr
                                citem.ifdoexam = 1
                            }else{
                                citem.useranswer = []
                                citem.ifdoexam = 0
                            }
                            
                            
                        }
                    })
                })
            },
            /**判断填空/主观题 */
            ifDoexamStuff(tkid,val){
                let s = 0//计算有值的个数
                this.testList.map((item,i)=>{
                    item.tklist.map((citem,j)=>{
                        if(citem.tkid == tkid){
                            if(val){
                                if(citem.tktype==5){
                                    val.map(sitem=>{
                                        if(sitem.toString()){
                                            s++
                                        }else{
                                            return s
                                        }
                                    })
                                    if(s){
                                        citem.useranswer = val
                                        citem.ifdoexam = 1
                                    }else{
                                        citem.useranswer = []
                                        citem.ifdoexam = 0
                                    }
                                }else{
                                    citem.useranswer = [val]
                                    citem.ifdoexam = 1
                                }
                            }else{
                                citem.useranswer = []
                                citem.ifdoexam = 0
                                
                            }
                            
                        }
                    })
                })
            },
            /**获取阅读题的结果 */
            getResult(val){
                if(val.isrdoexam==0&&val.iscdoexam==0&&val.issdoexam==0){
                    val.ifdoexam = 0
                }else{
                    val.ifdoexam = 1
                }
                this.testList.map((item,i)=>{
                    item.tklist.map((citem,j)=>{
                        if(citem.tkid==val.tkid){
                            citem = val
                        }
                    })
                    
                })
            },
            /**获取试卷 */
            getPaperInfo(){
                let newarr = []
                let arr = {}
                this.testList.map((item,i)=>{
                    let tklist = []
                    item.tklist.map((citem,j)=>{
                        
                        let useranswer = []
                        if(citem.tktype==1||citem.tktype==2||citem.tktype==4){
                            // console.log(citem.useranswer)
                            if(citem.useranswer){
                                if(citem.tktype==4){
                                    if(citem.useranswer[0]=='A'){
                                        useranswer.push(0)
                                    }else{
                                        useranswer.push(1)
                                    }
                                }else{
                                    useranswer = citem.useranswer
                                }
                                

                            }else{
                                useranswer = []
                            }
                            
                        }else if(citem.tktype==3||citem.tktype==5){
                            if(citem.useranswer){
                                useranswer = citem.useranswer
                            }else{
                                useranswer = []
                            }
                        }else{
                            let arr = []
                            citem.tkinfo.childtk.map(ccitem=>{
                                if(ccitem.useranswer){
                                    if(ccitem.tktype==4){
                                        if(ccitem.useranswer[0]=='A'){
                                            arr.push({
                                                tktype:ccitem.tktype,
                                                useranswer:[0]
                                            })
                                        }else{
                                            arr.push({
                                                tktype:ccitem.tktype,
                                                useranswer:[1]
                                            })
                                        }
                                    }else if(ccitem.tktype==1||ccitem.tktype==2){
                                        
                                        arr.push({
                                            tktype:ccitem.tktype,
                                            useranswer:ccitem.useranswer
                                        })
                                    }else if(ccitem.tktype==3||ccitem.tktype==5){
                                        arr.push({
                                            tktype:ccitem.tktype,
                                            useranswer:ccitem.useranswer
                                        })
                                    }
                                }else{
                                    arr.push({
                                        tktype:ccitem.tktype,
                                        useranswer:[]
                                    })
                                }
                                
                            })
                            if(citem.score){
                                citem.score.map((sitem,s)=>{
                                    arr[s].score = sitem
                                })
                            }
                            
                            useranswer = arr
                        }
                        tklist.push({
                            tkid:citem.tkid,
                            score:citem.score,
                            tktype:citem.tktype,
                            useranswer:useranswer
                        })
                        
                    })
                    newarr.push({
                        title:item.title,
                        tklist:tklist
                    })
                })
                arr = {
                    schoolid:this.getParameters.schoolid,
                    userid:this.getParameters.userid,
                    categoryid: this.getParameters.categoryid,
                    courseid: this.getParameters.courseid,
                    // time:Math.round((this.paperTime-this.time)/60),
                    time:this.paperTime-this.time,
                    paperid:this.getParameters.paperid,
                    answer:newarr,
                    workid:this.$route.query.workid,
                    stu:this.$route.query.stu

                }
                
                this.finishPaper = arr
            },
            /**计算没做的题数 */
            countNodoNum(){
                let i = 0
                this.finishPaper.answer.map(item=>{
                    item.tklist.map((citem,j)=>{
                        if(citem.tktype==6){
                            let num = 0
                            citem.useranswer.map((mitem,m)=>{
                                if(mitem.useranswer.length==0){
                                    num++
                                }else{
                                    return num
                                }
                            })
                            if(num==citem.useranswer.length){
                                i++
                            }else{
                                return i
                            }
                        }else{
                            if(citem.useranswer.length==0){
                                i++
                            }else{
                                return i
                            }
                        }
                        
                        
                    })
                })
                this.unfinished = i
            },
            /**单题交卷 */
            submitAnswer(data){
                this.$set(data,'showAnalytical',false)
                
                let useranswer = []
                if(data.tktype==1||data.tktype==2){
                    if(data.useranswer){
                        useranswer = data.useranswer
                    }else{
                        useranswer = []
                    }
                    
                }else if(data.tktype==4){
                    if(data.useranswer.toString()=='A'){
                        useranswer = ['0']
                    }else if(data.useranswer.toString()=='B'){
                        useranswer = ['1']
                    }
                    //useranswer = data.useranswer

                    // if(data.useranswer=='A'){
                    //     useranswer.push('0')
                    // }else if(data.useranswer=='B'){
                    //     useranswer.push('1')
                    // }else{
                    //     useranswer = []
                    // }
                }else{
                    
                    if(data.useranswer){
                        useranswer = data.useranswer
                    }else{
                        useranswer = []
                    }
                    
                }
                
                SubmitPaperAnswer({
                    paperid:this.getParameters.paperid,
                    schoolid:this.getParameters.schoolid,
                    userid: this.getParameters.userid,
                    courseid: this.getParameters.courseid,
                    tkid:data.tkid,
                    xttkid:0,
                    useranswer:useranswer
                }).then(res=>{
                    
                    if(res.result){
                        data.showAnalytical = true
                        if(data.tktype==2||data.tktype==3){
                            let answer = []
                            let useranswer = []
                            if(data.tktype==2){
                                data.tkinfo.content.map(item=>{
                                    if(item.real_answer==res.data.answer){
                                        answer.push(item.order)
                                    }
                                    if(item.real_answer==res.data.useranswer){
                                        useranswer.push(item.order)
                                    }
                                })
                            }else{
                                data.tkinfo.content.map(item=>{
                                    res.data.answer.map(citem=>{
                                        if(item.real_answer==citem){
                                            answer.push(item.order)
                                        }
                                    })
                                    res.data.useranswer.map(uitem=>{
                                        if(item.real_answer==uitem){
                                            useranswer.push(item.order)
                                        }
                                    })
                                        
                                })
                            }
                        
                            res.data.answer = answer
                            res.data.useranswer = useranswer
                        }
                        
                        data.datainfo = res.data
                    }else{
                        data.showAnalytical = false
                        this.isShowOpera = true
                        this.operatstatus = 0
                        this.operaTips = res.msg
                        setTimeout(()=>{
                            this.isShowOpera = false
                        },1500)
                    }
                })
            },
            /**交卷时候的提示 */
            finish(){
                this.getPaperInfo()
                this.countNodoNum()
                this.powertf_switch = 1
            },
            /**交卷 */
            save(flag){
                
                if(this.operatstatus==3){
                    this.getPaperInfo()
                    this.isShowOpera=false
                }
                this.finishPaper.answer.map(fitem=>{
                    fitem.tklist.map(ftitem=>{
                        if(ftitem.tktype==6){
                            let allScore = 0
                            ftitem.score.map(sitem=>{
                                allScore += Number(sitem)
                            })
                            ftitem.score = allScore
                        }
                    })
                })
                if(flag==1){
                    this.finishPaper.flag = flag
                }else{
                    this.finishPaper.flag = 0
                }
                // console.log(this.finishPaper)
                // return false
                SetPaperAnswer(this.finishPaper).then(res=>{
                    if(res.result){
                        // console.log(res.data)
                        if(flag!=2){
                            
                            if(flag==1){
                                this.isShow = false
                                this.operatstatus = 1
                                this.isShowOpera = true
                                this.operaTips = '交卷成功'
                                this.time = -3
                            }else{
                                this.isShowOpera = true
                                this.operatstatus = 1
                                this.operaTips = '保存成功'
                            }
                            
                            setTimeout(()=>{
                                this.isShowOpera = false
                                if(flag==1){
                                    if(res.subjective_count){
                                        this.$router.push({
                                            name:'YourselfReading',
                                            query:{
                                                answerid:res.id,
                                                title:this.paperInfo.title,
                                                paperid:this.getParameters.paperid,
                                                type:1,
                                                categoryid:this.getParameters.categoryid,
                                                dtnum:this.testList.length,
                                                tknum:this.paperInfo.tk_num,
                                                answertime:this.paperInfo.exam_time,
                                                sumscore:this.paperInfo.sumscore,
                                                schoolname:this.paperInfo.copyfrom
                                            }
                                        })
                                    }else{
                                        this.$router.push({
                                            name:'ExaminationResult',
                                            query:{
                                                answerid:res.id,
                                                paperid:this.getParameters.paperid,
                                                type:1,
                                                categoryid:this.getParameters.categoryid,
                                                dtnum:this.testList.length,
                                                tknum:this.paperInfo.tk_num,
                                                answertime:this.paperInfo.exam_time,
                                                sumscore:this.paperInfo.sumscore,
                                                schoolname:this.paperInfo.copyfrom
                                            }
                                        })
                                    }
                                    
                                }
                            },1500)
                        }
                    }else{
                        this.isShow = false
                        this.isShowOpera = true
                        this.operatstatus = 0
                        this.operaTips = res.msg
                        setTimeout(()=>{
                            this.isShowOpera = false
                        },1500)
                    }
                })
            },
            /**保存 data=0表示手动保存，data=2表示自动保存*/
            saveTest(data){
                
                this.getPaperInfo()
                this.save(data)
                if(data==0){
                    this.powertf_switch = 1
                    setTimeout(()=>{
                        this.powertf_switch = 0
                    },5000)
                }
                
            },
            /**断电保存 */
            powerProtection(){
                
                setTimeout(()=>{
                    this.saveTest(2)
                    if(this.powertf_switch==0){
                        this.powerProtection()
                    }else{
                        return false
                    }
                },this.autosavetime)  
            },
            /**
             * 防复制
             */
            
            antiCopyFn(){
                return false
            },
            /**
             * 禁止复制、右键等
             * @param {Boolean}
             */
            antiCopy(flag){

                let antiCopyStyle = document.getElementById('antiCopyStyle');
                let body = document.body;
                if(flag && !antiCopyStyle){
                    let style = document.createElement('style');
                    style.id = "antiCopyStyle";
                    let css = document.createTextNode('*{-webkit-touch-callout:none; -webkit-user-select:none;-khtml-user-select:none; -moz-user-select:none; -ms-user-select:none;   user-select:none;}input{ -webkit-user-select: auto;}img{  -webkit-user-select: none;-moz-user-select: none; -webkit-user-select:none; -o-user-select:none; user-select:none;}');
                    style.appendChild(css);
                    body.appendChild(style);
                    body.oncontextmenu = function(){
                        return false
                    }
                    body.addEventListener('dragstart',this.antiCopyFn)
                    body.addEventListener('selectstart',this.antiCopyFn)
                }else if(!flag && antiCopyStyle){
                    antiCopyStyle.remove()
                    body.oncontextmenu = function(){}
                    body.removeEventListener('dragstart',this.antiCopyFn)
                    body.removeEventListener('selectstart',this.antiCopyFn)
                }
            },
            /**
             * 浏览器全屏
             */
            setFullScreen() {
                var el = document.documentElement,
                    rfs = el.requestFullScreen || el.webkitRequestFullScreen || el.mozRequestFullScreen || el.msRequestFullScreen,
                    wscript;
                if(typeof rfs != "undefined" && rfs) {
                    rfs.call(el);
                    return;
                }

                if(typeof window.ActiveXObject != "undefined") {
                    wscript = new ActiveXObject("WScript.Shell");
                    if(wscript) {
                        wscript.SendKeys("{F11}");
                    }
                }
            },
            /**
             * 浏览器退出全屏
             */
            exitFullScreen() {
                var el = document,
                    cfs = el.cancelFullScreen || el.webkitCancelFullScreen || el.mozCancelFullScreen || el.exitFullScreen,
                    wscript;
            
                if (typeof cfs != "undefined" && cfs) {
                    cfs.call(el);
                    return;
                }
            
                if (typeof window.ActiveXObject != "undefined") {
                    wscript = new ActiveXObject("WScript.Shell");
                    if (wscript != null) {
                        wscript.SendKeys("{F11}");
                    }
                }
            },
            /**
             * 全屏
             */
            fullScreen(){
                if(this.isFullScreen){
                    this.exitFullScreen()
                    
                }else{
                    this.setFullScreen()
                }
            
            }

        },
        mounted(){
            if(this.$route.query.retrurncode==1063||this.$route.query.retrurncode==1064||this.$route.query.retrurncode==1065){
                this.isPayPaper = true
            }
            if(this.$route.query.preview){
                this.getParameters.schoolid = this.$route.query.schoolid
            }
            this.loadPage()
            window.addEventListener('scroll', this.handleScroll)
            // if(typeof CONFIG === 'object' && CONFIG.fixed_bottom && CONFIG.head_height){
            //     this.maxHeight = window.innerHeight - (CONFIG.fixed_bottom+CONFIG.head_height) - 126
            // }else{
            //     this.maxHeight = 300
            // }
            this.maxHeight = window.innerHeight - 126
            
        },
        destroyed () {
            window.removeEventListener('scroll', this.handleScroll)

        }
    }
</script>
<style scoped>
    .test-tips{
        margin-bottom: 35px;
    }
    .test-con-left{
        width: 920px;
    }
    .test-type-tips{
        margin:17px 0;
        padding: 20px;
    }
    .test-list li{
        padding:28px 44px;
        margin-bottom: 26px;
    }
    .test-option{
        margin: 14px 7px;
    }
    .test-con-right,.test-right-inner{
        width: 256px;
    }
    .top-application{
        margin: 17px 0;
        padding: 20px;
    }
    .top-application li{
        width: 33.33%;
    }
    .top-application li p{
        height: 26px;
    }
    .answer-sheet-box{
        padding: 20px 25px;
    }
    .answer-sheet-box h2{
        margin-bottom: 20px;
    }
    .answer-sheet-box h3{
        margin-bottom: 16px;
    }
    .answer-sheet ul li{
        min-width: 20px;
        height: 20px;
        border: 1px solid #bbb;
        line-height: 20px;
        margin-right: 10px;
        margin-bottom: 10px;
        box-sizing: border-box;
        cursor: pointer;
    }
    .answer-sheet{
        margin-bottom: 28px;
    }
    .isFixed{
        position:fixed;
        top:0;
        z-index:999;
        height:100vh;
        overflow:auto;
    }
    .tips-con p {
        margin-bottom: 20px;
        line-height: 50px;
        letter-spacing: 1px;
    }
    .dialog-btn{
        margin-bottom: 30px;
    }
    .f50{
        font-size: 50px;
    }
    .redress-option{
        margin-bottom: 15px;
        padding-top: 15px;
        border-top: 1px solid #eee;
        margin-top: 15px;
    }
    .redress-con_con{
        padding-top: 20px;
        border-top:1px solid #eee;
        margin-bottom: 20px;
    }
    .redress-con_con textarea{
        border: none;
        width: 100%;
        background: #f8f8f8;
        -webkit-box-shadow: inset 0px 0px 25px #efefef;
        box-shadow: inset 0px 0px 25px #efefef;
        border-radius: 4px;
        padding: 10px;
        box-sizing: border-box;
    }
    .answer-sheet-inner{
        max-height: 300px;
        overflow-y: auto;
        margin-left: -25px;
        margin-right: -25px;
        padding: 0 25px;
        margin-bottom: 20px;
    }
    .answer-tips{
        margin-left: -25px;
        background: #f9f9f9;
        margin-right: -25px;
        padding: 10px 25px;
    }
    .answer-tips span{
        line-height: 25px;
    }
    .answer-tips span i{
        display: inline-block;
        width: 10px;
        height: 10px;
        border-radius: 2px;
        margin-right: 5px;
    }
</style>
