<template>
  <div>
    <div ref="wrapper">
      <div class="info" v-if="student && student.student_id">
        <div class="shape">
          <img :src="student.id_photo ? student.id_photo : 'https://zws-imgs-pub.ezijing.com/static/public/e0c63e4d4554f0852e1558426aad20d1.png'" />
        </div>
        <div class="right">
          <div class="name">{{ student.name }}</div>
          <div class="code">{{ student.examinee_number }}</div>
        </div>
      </div>
      <div class="order-scroll">
        <div class="order-num">
          <div :key="index" v-for="(group, index) in questions">
            <div class="tit">{{ group.question_item_title }}</div>
            <!-- stu1已答  stu2当前  stu3标记 -->
            <ul>
              <li
                v-for="item in group.question_list"
                :key="item.id"
                :class="{ stu1: !!item.user_answer.length, stu2: item.q_number === questionNumber, stu3: item.sign, stu4: checkAnswer(item) }"
                @click="handleChange(item)">
                {{ item.q_number }}
              </li>
            </ul>
          </div>
        </div>
      </div>
    </div>
    <ul class="flag-tips">
      <li v-for="item in tipsList" :key="item.title">
        <div :class="item.className"></div>
        <div class="txt">{{ item.title }}</div>
      </li>
    </ul>
  </div>
</template>
<script>
export default {
  props: {
    student: { type: Object, default: () => ({}) },
    questionItems: { type: Array, default: () => [] },
    questionNumber: { type: Number }
  },
  data() {
    return {}
  },
  computed: {
    questions() {
      return this.questionItems.map(group => {
        const question_list = group.question_list.reduce((result, item) => {
          if (item.list) return [...result, ...item.list]
          else return [...result, item]
        }, [])
        return { ...group, question_list }
      })
    },
    pageOptions() {
      const { show_answer = '0' } = this.$route.query
      return {
        showAnswer: show_answer == '1'
      }
    },
    tipsList() {
      const list = [
        { className: 'circle1', title: '已答' },
        { className: 'circle2', title: '未答' },
        { className: 'circle3', title: '当前' },
        { className: 'circle4', title: '标记' }
      ]
      if (this.pageOptions.showAnswer) {
        list.push({ className: 'circle5', title: '答错' })
      }
      return list
    }
  },
  methods: {
    handleChange(item) {
      this.$emit('changeNumber', item.q_number)
    },
    checkAnswer(item) {
      if (this.pageOptions.showAnswer && !!item.user_answer.length) {
        // item.user_answer 是数组，item.answer 是数组字符串
        // 需要先解析 answer，然后对两个数组排序后再比较
        let correctAnswer = item.answer
        try {
          // 如果 answer 是字符串，先解析为数组
          if (typeof correctAnswer === 'string') {
            correctAnswer = JSON.parse(correctAnswer)
          }
        } catch (error) {
          console.error('解析答案失败:', error)
          return true // 解析失败，认为答错
        }
        
        // 对两个数组进行排序后比较
        const userAnswer = [...item.user_answer].sort()
        const answer = Array.isArray(correctAnswer) ? [...correctAnswer].sort() : [correctAnswer]
        
        return JSON.stringify(userAnswer) !== JSON.stringify(answer)
      } else {
        return false
      }
    }
  }
}
</script>
<style lang="scss">
.info {
  display: flex;
  align-items: center;
  height: 100px;
  .shape {
    width: 60px;
    height: 60px;
    border-radius: 50%;
    overflow: hidden;
  }
  img {
    width: 100%;
    display: block;
  }
  .right {
    margin-left: 22px;
    .name {
      font-size: 18px;
      color: #222222;
      line-height: 25px;
    }
    .code {
      font-size: 14px;
      color: #222222;
      line-height: 20px;
      margin-top: 5px;
      width: 120px;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
    }
  }
}
.order-num {
  padding-bottom: 90px;
  .tit {
    font-size: 12px;
    color: #999999;
    line-height: 17px;
    margin-bottom: 10px;
  }
  ul {
    display: flex;
    list-style: none;
    padding: 0;
    margin: 0;
    flex-wrap: wrap;
    li {
      cursor: pointer;
      position: relative;
      border-radius: 50px;
      width: 24px;
      height: 24px;
      border: 1px solid #cccccc;
      font-size: 14px;
      color: #666666;
      line-height: 24px;
      margin-right: 20px;
      margin-bottom: 10px;
      text-align: center;
      &:nth-child(5n + 5) {
        margin-right: 0;
      }
      &.stu1 {
        background: #999;
        border: 1px solid #999;
      }
      &.stu4 {
        width: 22px;
        height: 22px;
        line-height: 22px;
        border: 2px solid #c01540;
        background: rgba(193, 21, 64, 0.1);
      }
      &.stu2 {
        width: 22px;
        height: 22px;
        line-height: 22px;
        border: 2px solid #0fc118;
        background: rgba(15, 193, 24, 0.1);
      }
      &.stu3 {
        &::after {
          content: '';
          position: absolute;
          top: -1px;
          right: -1px;
          width: 4px;
          height: 4px;
          background: #c01540;
          border-radius: 50%;
        }
      }
    }
  }
}
.flag-tips {
  width: 260px;
  position: fixed;
  bottom: 60px;
  right: 0;
  display: flex;
  justify-content: space-around;
  padding: 15px 0 10px;
  background: #fff;
  margin: 0;
  list-style: none;
  li {
    .circle1 {
      width: 24px;
      height: 24px;
      background: #999;
      border: 1px solid #999;
      border-radius: 50%;
    }
    .circle2 {
      width: 24px;
      height: 24px;
      border: 1px solid #cccccc;
      border-radius: 50%;
    }
    .circle3 {
      width: 24px;
      height: 24px;
      border: 2px solid #0fc118;
      background: rgba(15, 193, 24, 0.1);
      border-radius: 50%;
    }
    .circle4 {
      position: relative;
      width: 24px;
      height: 24px;
      border: 1px solid #cccccc;
      border-radius: 50%;
      &::after {
        content: '';
        position: absolute;
        top: -1px;
        right: -1px;
        width: 4px;
        height: 4px;
        background: #c01540;
        border-radius: 50%;
      }
    }
    .circle5 {
      width: 24px;
      height: 24px;
      border: 2px solid #c01540;
      background: rgba(193, 21, 64, 0.1);
      border-radius: 50%;
    }
    .txt {
      margin-top: 5px;
      font-size: 12px;
      color: #cccccc;
      line-height: 17px;
    }
  }
}
</style>
