<template>
  <div class="exam-paper">
    <div class="paper-main"  v-loading="page_is_loading">
      <div class="aq-card" v-if="mode === 5">
        <AQCard :questions="questions" :answers="answers" @smooth="aqItemOnClick" :mode="mode" @submit="submitMistakes"
          @cancel="cancelMistakeExam"></AQCard>
      </div>
      <div class="questions-area">
        <QuestionsList :questions="questions" @answers="userAnswerChanged" :mode="mode" :paperStyle="0"
          ref="questionList">
        </QuestionsList>
      </div>
    </div>
    <ActionBar v-if="[4,1].includes(mode)" noCencel center confirmTxt="退出" style="margin-bottom:10px;" @onConfirm="$router.go(-1)"></ActionBar>
  </div>
</template>

<script>
import QuestionsList from '../../online-test/begin-online-exam/components/QuestionsList.vue'
import { getMistakesByclassifyId, patchMistakes } from '@/api/assessment-evaluation/mistake'
import AQCard from '../../online-test/begin-online-exam/components/AQCard.vue'
import ActionBar from '@/components/layout/ActionBar.vue'
export default {
  components: {
    QuestionsList,
    AQCard,
    ActionBar
  },
  data: () => ({
    questions: [],
    answers: [],
    mode: 1,
    page_is_loading: false
  }),

  activated () {
    this.questions = []
    this.answers = []
    if (this.$route.params.mode === '1') {
      this.getMistakesList(1)
      this.mode = 4
    } else if (this.$route.params.mode === '2') {
      this.getMistakesList(0)
      this.mode = 5
    } else if (this.$route.params.mode === '3') {
      this.getMistakesList(1)
      this.mode = 1
    }
  },
  methods: {
    // 当用户单击提交按钮时调用的方法。
    submitMistakes () {
      this.page_is_loading = true
      patchMistakes(this.$route.params.classifyId, this.answers).then(res => {
        const { data } = res
        const { pass, faild } = data
        this.$alert(`本次答题正确${pass}道，错误${faild}道。`, '提示', {
          callback: () => {
            this.$router.go(-1)
          }
        })
      }).finally(_ => {
        this.page_is_loading = false
      })
    },
    // 当用户单击取消按钮时调用的方法。
    cancelMistakeExam () {
      this.$router.go(-1)
    },
    // 当用户单击 AQCard 组件中的问题时调用的方法。
    aqItemOnClick (question) {
      const item = document.querySelector(`[tag="${question.id}"]`)
      item.scrollIntoView({
        block: 'start',
        behavior: 'smooth'
      })
      item.classList.add('shine')
      setTimeout(() => {
        item.classList.remove('shine')
      }, 2000)
    },
    // 当用户更改答案时调用的方法。
    userAnswerChanged (nv) {
      this.answers = nv
    },
    // 激活组件时调用的方法。它用于从服务器获取问题。
    getMistakesList (showUserAnswer = 1) {
      this.page_is_loading = true
      getMistakesByclassifyId(this.$route.params.classifyId, showUserAnswer).then(res => {
        this.questions = res.data
      }).finally(_ => { this.page_is_loading = false })
    }
  }
}
</script>

<style lang="scss" scoped>
.exam-paper {
  display: flex;
  height: 100%;
  position: relative;
  flex-direction: column;
  .paper-main{
    flex-grow:1;
    display: flex;
    overflow: auto;
  }

  .aq-card {
    flex-basis: 240px;
    flex-shrink: 0;
    width: 240px;
    padding-right: 15px;
    border-right: 1px solid #efefef;
    height: 100%;
    overflow-y: scroll;
  }

  .tips-card {
    flex-basis: 200px;
    width: 200px;
    padding-left: 15px;
    border-left: 1px solid #efefef;
    flex-shrink: 0;
  }

  .questions-area {
    height: 100%;
    flex-grow: 1;
    overflow-y: scroll;
    padding: 0 20px;
  }
}
</style>
