<template>
  <div class="textarea position-relative">
      <h3 class="weight-normal f14">{{action==1?'小题':'题目'}}{{index+1}}：<p v-html="item.title"></p></h3>
      <p v-if="item.is_hearing" :class="item.tktype==6 && 'combination'">
        <audio class="success" :src="item.tkinfo.audiofile" controls></audio>
      </p>
      <div class="test-textarea darkgray-font line-height26" v-if="item.tktype==1">
          <p class="line-height26">答题区</p>
          <textarea :name="'test'+index" class="textareabox vertical-align" v-model="value" @change="getStuff2" v-if="action==1"></textarea>
          <textarea :name="'test'+item.tkid" class="textareabox vertical-align" v-model="value" @change="getStuff" v-else></textarea>
      </div>
      <div v-else-if="item.tktype==6">
        <li v-for="(citem,i) in item.tkinfo.childtk" :key="i" class="white-bg childtk" :ref="'djump'+citem.tkid">
            <ks-radio :item="citem" :index="i" :parentid="item.tkid" :is_saveanswer="is_saveanswer" v-if="citem.tktype==2||citem.tktype==4" @ifDoexam="ifDoexam" :action="1"/>
            <ks-check :item="citem" :index="i" :parentid="item.tkid" :is_saveanswer="is_saveanswer" v-else-if="citem.tktype==3" @ifDoexam="ifDoexamCheck" :action="1"/>
            <ks-subjective-questions :index="i" :parentid="item.tkid" :is_saveanswer="is_saveanswer" :item="citem" v-else-if="citem.tktype==1" @ifDoexam="ifDoexamStuff" :action="1"/>
            <ks-stuff :data="citem" :index="i" :parentid="item.tkid" :is_saveanswer="is_saveanswer" v-else-if="citem.tktype==5" @ifDoexam="ifDoexamStuff" :action="1"/>
            <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="realtest==0&&is_tjda==1&&!citem.showAnalytical">
                <ks-button type="danger" plain @click="submitAnswer(citem,item.tkid,i)">提交看答案</ks-button>
            </div>
        </li>
      </div>
      <div class="right-box darkgray-font" v-if="action!=1">
        <span class="mr10" @click="redress(item)" v-if="item.iscorrection">
            <i class="iconfont icon-iconzhuanqu04 f20 vertical-align"></i>
            <b class="weight-normal vertical-align">纠错</b>
        </span>
        <span class="mr10" @click="setFlag(item)">
            <i :class="item.flag==0?'iconfont icon-thin-_pin_bookma f16 vertical-align':'iconfont icon-biaoji green-font f16 vertical-align'"></i>
            <b class="weight-normal vertical-align">标记</b>
        </span>
        <span @click="setCollect(item)" v-if="isshow_collect">
            <i :class="item.collect==0?'iconfont icon-shoucang f18 vertical-align':'iconfont icon-shoucang1 yellow-font f18 vertical-align'"></i>
            <b class="weight-normal vertical-align">收藏</b>
        </span>
      </div>
      <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>
        <div class="dialog-btn" style="margin-bottom: 0;margin-top: 20px;" v-if="operatstatus==3">
            <ks-button type="danger" class="ml10" @click="save">确定交卷</ks-button>
        </div>
    </ks-dialog>
  </div>
</template>
<script>
  import {SubmitPaperAnswer} from '@/api/exam'
  export default {
    name: 'SubjectiveQuestions',
    props:['item','index','action','parentid','realtest','is_tjda','paperid','schoolid','isshow_collect','is_saveanswer','is_powertf'],
    data () {
      return {
        value:'',
        isShowOpera:false,
        operatstatus:0,
        operaTips:''
      }
    },
    methods:{
      redress(data){
        this.$emit('redressStatus',data.tkid)
      },
      setFlag(data){
          data.flag ? data.flag = 0 : data.flag = 1 
          this.$emit('flagStatus',data.flag,data.tkid)
      },
      setCollect(data){
          data.collect ? data.collect = 0 : data.collect = 1 
          this.$emit('collectStatus',data.collect,data.tkid)
      },
      getStuff(val){
          this.$emit('ifDoexam',this.item.tkid,val.target.value)  
      },
      getStuff2(val){
          this.$emit('ifDoexam',this.parentid,val.target.value,this.index)  
      },
      /**判断单选 */
      ifDoexam(order,tkid,i){
          this.item.tkinfo.childtk.map((citem,j)=>{
            if(j==i){
                if(order){
                    citem.useranswer = [order]
                    citem.isrdoexam = 1
                }else{
                    citem.useranswer = []
                    citem.isrdoexam = 0
                }
                
            }
              
          })
          this.$emit('getResult',this.item)  
      },
      /**判断多选 */
      ifDoexamCheck(tkid,n,orderArr,i){
          this.item.tkinfo.childtk.map((citem,j)=>{
            if(j==i){
                if(n){
                    citem.useranswer = orderArr
                    citem.iscdoexam = 1
                }else{
                    citem.useranswer = []
                    citem.iscdoexam = 0
                }
            }
              
          })
          this.$emit('getResult',this.item) 
      },
      /**判断填空 */
      ifDoexamStuff(tkid,val,i){
          let s=0
          this.item.tkinfo.childtk.map((citem,j)=>{
            if(j==i){
                if(val){
                    if(citem.tktype==1){
                        citem.useranswer = [val]
                        citem.issdoexam = 1
                    }else{
                        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 = []
                    citem.issdoexam = 0
                }
                
            }
          })
          this.$emit('getResult',this.item) 
      },
      /**单题交卷 */
      submitAnswer(data,tkid,index){
          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){
                //useranswer = data.useranswer
                if(data.useranswer.toString()=='A'){
                    useranswer = ['0']
                }else if(data.useranswer.toString()=='B'){
                    useranswer = ['1']
                }
            //   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.paperid,
              schoolid:this.schoolid,
              tkid:tkid,
              xttkid:index,
              useranswer:useranswer
          }).then(res=>{
              if(res.result){
                    data.showAnalytical = true
                    data.datainfo = res.data
              }else{
                    data.showAnalytical = false
                    data.datainfo = {}

                    this.isShowOpera = true
                    this.operatstatus = 0,
                    this.operaTips = res.msg
                    setTimeout(()=>{
                        this.isShowOpera = false
                    },1500)
              }
          })
      },
    },
    mounted(){
        if(this.item.tktype==6){
            let i = 0
            this.item.tkinfo.childtk.map(citem=>{
                if(citem.useranswer&&citem.useranswer.length){
                    i++
                }else{
                    return i
                }
            })
            if(i){
                this.item.ifdoexam = 1
            }else{
                this.item.ifdoexam = 0
            }
        }else{
            if(this.item.useranswer && this.item.useranswer.length){
                this.item.ifdoexam = 1
            }else{
                this.item.ifdoexam = 0
            }
        }
        if(this.item.useranswer){
            this.value = this.item.useranswer.toString()
        }


        // if(this.is_saveanswer==1||this.is_powertf==1){
        //     if(this.item.tktype==6){
        //         let i = 0
        //         this.item.tkinfo.childtk.map(citem=>{
        //             if(citem.useranswer&&citem.useranswer.length){
        //                 i++
        //             }else{
        //                 return i
        //             }
        //         })
        //         if(i){
        //             this.item.ifdoexam = 1
        //         }else{
        //             this.item.ifdoexam = 0
        //         }
        //     }else{
        //         if(this.item.useranswer && this.item.useranswer.length){
        //             this.item.ifdoexam = 1
        //         }else{
        //             this.item.ifdoexam = 0
        //         }
        //     }
        //     if(this.item.useranswer){
        //         this.value = this.item.useranswer.toString()
        //     }
        // }
    }
  }
</script>
<style scoped>
    .textarea{
        line-height: 22px;
        padding-bottom: 40px;
    }
    .textarea h3{
        margin-bottom: 25px;
    }
    .test-textarea .textareabox{
        width: 100%;
        height: 100px;
        resize: none;
        border-radius: 5px;
        border: 1px solid #ddd;
        padding: 15px;
        box-sizing: border-box;
    }
    .right-box{
        position: absolute;
        right: 0;
        bottom: 0;
    }
    .right-box span{
        cursor: pointer;
    }
    .success{
        margin-top: 20px;
        width: 400px;
    }
    .combination{
        margin-top:0;
        margin-bottom:20px;
    }
    .combination .success{
        margin-top:0;
    }
    .childtk{
        margin-bottom: 40px;
    }
    .childtk .textarea,.childtk .radio,.childtk .stuff,.childtk .check{
        padding-bottom:20px !important;
    }
    .childtk h3{
        margin-bottom: 10px !important;
    }
    .f50{
        font-size: 50px;
    }
    .tips-con{
        line-height: 50px;
        letter-spacing: 1px;
    }
</style>