<template>
  <div>
    <div class="exam-box">
      <div class="topic-box">
        <swiper ref="mySwiper" :options="swiperOptions">
          <template v-for="item in questionData.list">
            <swiper-slide :key="item.id" :data-id="item.id">
              <div id="top-view">
                <div class="tool-box">
                  <div class="time">{{ item.sheet_time }}</div>
                  <div class="tool-all">
                    <div class="icon1"></div>
                    <div class="icon2"></div>
                  </div>
                </div>
              </div>
              <div class="title">
                <div class="type">{{ other.type[item.question_type] }}</div>
                <div class="count">{{ item.num }}/{{ questionData.error_total }}</div>
              </div>
              <div class="topic-bt" v-if="item.question_type == '5'" v-html="item.common_content"></div>
              <div class="topic" v-html="item.question_content"></div>
              <ul class="option">
                <template v-for="opt in item.question_options">
                  <li
                    :key="opt.id"
                    @click="selectOptions(item, opt)"
                    :class="isAnalysis && item.question_answer.find(id => { return id === opt.id })
                    ? 'active2'
                    : item.user_answer.find(id => { return id === opt.id })
                      ? 'active'
                      : ''"
                    >{{ opt.option }}</li>
                </template>
              </ul>
              <div class="analy" v-show="isAnalysis">
                <div class="tit">答案解析</div>
                <div class="txt">正确答案：<span>A</span></div>
                <div class="txt">您的答案：<span>A</span></div>
                <div class="exp">
                  <p class="name">解析：</p>
                  <p class="nr">：</p>
                </div>
              </div>
            </swiper-slide>
          </template>
        </swiper>
      </div>
      <template>
        <div class="btn-box">
          <div class="padd">
            <div class="analysis" @click="isAnalysis = !isAnalysis" v-if="$route.query.type == 1">查看解析</div>
            <div class="btn left" @click="switchQuestions('left')">上一题</div>
            <div class="btn right" @click="switchQuestions('right')" >下一题</div>
          </div>
        </div>
      </template>
    </div>
  </div>
</template>

<script>
import * as api from '@/api/my.js'
import { Toast, Dialog } from 'vant'
import { Swiper, SwiperSlide, directive } from 'vue-awesome-swiper'
import 'swiper/css/swiper.css'
export default {
  metaInfo: {
    title: '已做试题'
  },
  components: {
    [Toast.name]: Toast,
    Swiper,
    SwiperSlide,
    [Dialog.name]: Dialog
  },
  directives: {
    swiper: directive
  },
  data() {
    const _this = this
    return {
      i: 1,
      other: {
        type: {
          1: '单选题',
          2: '多选题',
          5: '案例题',
          6: '判断题'
        }
      },
      questionData: [],
      isAnalysis: false,
      swiperOptions: {
        observer: true,
        autoHeight: true,
        on: {
          slideChangeTransitionStart: function() {
            _this.switchPages()
          }
        }
      }
    }
  },
  created() {
    parseInt(this.$route.query.type) === 3 ? this.isAnalysis = true : this.isAnalysis = false
    this.initData()
  },
  mounted() {
  },
  computed: {
    swiper() {
      return this.$refs.mySwiper.$swiper
    }
  },
  methods: {
    switchQuestions(e) {
      if (e === 'left') {
        this.swiper.slidePrev()
      } else {
        this.swiper.slideNext()
      }
    },
    switchPages() {
      parseInt(this.$route.query.type) === 3 ? this.isAnalysis = true : this.isAnalysis = false
    },
    selectOptions(item, opt) {
      if (parseInt(item.question_type) === 1 || parseInt(item.question_type) === 6) {
        item.user_answer = [opt.id]
      } else {
      }
    },
    delQuestion(id) {
      const param = {
        id: id,
        type: this.$route.query.type
      }
      api.deleteQuestion(param).then(res => {
        if (res.code === 0) {
          Toast('删除成功')
          this.initData()
        }
      })
    },
    collectQuestion() {
      api.collectQuestion({ question_id: 111 }).then(res => {
        if (res.code === 0) {
          Toast('删除成功')
          this.initData()
        }
      })
    },
    initData() {
      const datas = {
        error_total: 10,
        list: []
      }
      for (let i = 0; i < 50; i++) {
        const data = {
          num: 1,
          id: `${i}`,
          question_type: '1',
          question_content: 'xinjia',
          common_content: null,
          question_options: [
            {
              id: '667965608969004648412',
              option: 'A选项'
            },
            {
              id: '667965608969004648512',
              option: 'B选项'
            }
          ],
          question_answer: ['667965608969004648412'],
          question_analysis: '解析',
          answer_count: 1,
          user_answer: ['11111'],
          sheet_time: '2020-07xxxx'
        }
        datas.list.push(data)
      }
      this.questionData = datas
    }
  }
}
</script>

<style lang="scss">
.exam-box{
  .swiper-wrapper{
    padding-bottom: env(safe-area-inset-bottom);
    .swiper-slide{
      padding-bottom: 1.2rem;
    }
  }
  .head{
    color: #222;
    display: flex;
    padding: .4rem 0;
    align-items: center;
    i{
      font-size: .35rem;
      margin-left: .28rem;
    }
    .title{
      margin-left: .03rem;
      font-size: .3rem;
    }
  }
  .tool-box{
    width: 6.7rem;
    border-bottom: .01rem solid rgba(238,238,238,1);
    padding:  .35rem 0;
    display: flex;
    align-items: center;
    margin: 0 auto;
    color: #222;
    .tool-all{
      margin-left: auto;
      display: flex;
      .icon1{
        width: .44rem;
        height: .44rem;
        background: url('../../assets/images/del.png');
        background-size: 100% 100%;
        margin-right: .53rem;
      }
      .icon2{
        width: .41rem;
        height: .44rem;
        background: url('../../assets/images/collect.png');
        background-size: 100% 100%;
      }
    }
    .time{
      font-size: .3rem;
    }
    .tag-box{
      margin-left: auto;
      display: flex;
      span{
        padding: .1rem .2rem;
        font-size: .2rem;
        color: #fff;
        background: #67A8FF;
        border-radius: .25rem;
        margin-left: .2rem;
      }
      .active{
        background: #FF8000;
      }
    }
  }
  .topic-box{
    width: 100%;
    margin: 0 auto;
    font-size: .3rem;
    color: #222;
    overflow-y: scroll;
    .title{
      padding: .4rem;
      display: flex;
      align-items: center;
      .count{
        margin-left: auto;
      }
    }
    .topic{
      font-size: .26rem;
      line-height: .4rem;
      padding: 0 .4rem;
      span{
        width: 100%;
        display: block;
      }
      img{
        width: 100%;
        display: block;
      }
    }
    .topic-bt{
      font-size: .26rem;
      line-height: .4rem;
      padding: 0 .4rem;
      font-weight: bold;
      margin-bottom: .2rem;
      span{
        width: 100%;
        display: block;
      }
      img{
        width: 100%;
        display: block;
      }
    }
    .option{
      list-style: none;
      margin: .35rem 0 0 0;
      padding: 0 .4rem;
      li{
        background:rgba(247,247,247,1);
        border-radius: .2rem;
        font-weight: bold;
        padding: .35rem .2rem;
        margin-bottom: .2rem;
        word-wrap:break-word;
      }
      .active{
        color: #fff;
        background:#67A8FF;
      }
      .active2{
        color: #fff;
        background:#67C23A;
      }
    }
    .analy{
      font-size: .3rem;
      color: #222;
      padding: 0.2rem .4rem 1.2rem .4rem;
      .tit{
        font-weight: bold;
        line-height: 100%;
      }
      .txt{
        line-height: 100%;
        margin-top: .4rem;
        span{
          font-weight: bold;
        }
      }
      .exp{
        margin-top: .4rem;
        overflow: hidden;
        p{
          float: left;
        }
        .nr{
          width: 5.8rem;
          p{
            width: 100%;
          }
          img{
            width: 100%;
          }
        }
      }
    }
  }
  .topic-box.hide{
    overflow: hidden;
  }
  .btn-box{
    position: fixed;
    bottom: 0;
    left: 0;
    height: 1rem;
    box-shadow:0px 0px 6px 0px rgba(0,0,0,0.05);
    background: #fff;
    color: #222;
    font-size: .3rem;
    width: 100%;
    z-index: 99;
    padding-bottom: env(safe-area-inset-bottom);
    .padd{
      display: flex;
      padding: 0 .4rem;
      align-items: center;
      height: 100%;
    }
    .analysis{
      white-space: nowrap;
      margin-right: .3rem;
    }
    .btn{
      width:2.4rem;
      height:.7rem;
      background:rgba(43,124,233,1);
      border-radius:.12rem;
      text-align: center;
      line-height: .7rem;
      color: #fff;
    }
    .left{
      margin-right: .4rem;
    }
    .right{
      margin-left: auto;
      // background:rgba(255,103,103,1);
    }
  }
  .exam_submit{
    position: fixed;
    top: 0;
    left: 0;
    z-index: 999999;
    width: 100%;
    height: 100%;
    background: rgba(0,0,0,0.6);
    .pop{
      width: 5.9rem;
      position: absolute;
      top: 50%;
      left: 50%;
      -webkit-transform: translate(-50%,-50%);
      background:rgba(255,255,255,1);
      border-radius:.12rem;
      padding: 0.4rem 0;
      .tit{
        font-weight:bold;
        color:rgba(34,34,34,1);
        font-size:.3rem;
        text-align: center;
        line-height: 100%;
      }
      .txt{
        color:rgba(34,34,34,1);
        font-size:.3rem;
        text-align: center;
        line-height: 100%;
        margin-top: .8rem;
      }
      .btn_box{
        padding:0 0.2rem;
        display: flex;
        margin-top: .8rem;
        .btn{
          width:2.6rem;
          height:.7rem;
          background:rgba(43,124,233,1);
          border-radius:.12rem;
          text-align: center;
          line-height: .7rem;
          color: #fff;
          font-size: .3rem;
        }
        .btn2{
          margin-left: auto;
        }
      }
    }
  }
  .exam-end-pop{
    position: fixed;
    top: 0;
    left: 0;
    z-index: 999999;
    width: 100%;
    height: 100%;
    background: rgba(0,0,0,0.6);
    .pop{
      width: 5.9rem;
      position: absolute;
      top: 50%;
      left: 50%;
      -webkit-transform: translate(-50%,-50%);
      background:rgba(255,255,255,1);
      border-radius:.12rem;
      padding: .4rem 0;
      .tit{
        font-size: .3rem;
        color: #222;
        text-align: center;
        font-weight: bold;
      }
      img{
        width: 1.59rem;
        height: 1.35rem;
        display: block;
        margin: 0.4rem auto 0.2rem auto;
      }
      .txt{
        color: #222;
        font-size: .3rem;
        line-height: 100%;
        text-align: center;
      }
      .btn{
        margin: .4rem auto 0 auto;
        width:5.5rem;
        height:.7rem;
        background:rgba(255,103,103,1);
        border-radius:.12rem;
        text-align: center;
        line-height: .7rem;
        color: #fff;
        font-size: .3rem;
      }
    }
  }
}
</style>
