<script setup>
import Questions from "./QuestionsItemSelect.vue";
import uvParse from "@/uni_modules/uv-parse/components/uv-parse/uv-parse.vue";
import { ref, watchEffect } from "vue";
import { router } from "../../utils/router";

const dataList = ref([]);
const props = defineProps({
  data: {
    type: Array,
    default: () => [],
  },
  isRight: {
    type: Boolean,
    default: true,
  },
  onClick: {
    type: Function,
  },
  catalogue_id: {
    type: Number,
    default: 0,
  },
});
const TopicMapList = ["A", "B", "C", "D", "E"];

watchEffect(() => {
  dataList.value = props.data.map((item, ind) => {
    let questions = [];
    const ans = item.correct_answer.split(",");
    const ansList = [];
    for (let i = 0; i < item.question_count; i++) {
      const current = TopicMapList[i];
      if (ans.includes(current)) {
        ansList.push({
          label: current,
          value: i,
        });
      }
      const v =
        item[`select_${current.toLowerCase()}`].replace(/<br\s*\/?>/g, "") ||
        "";
      questions.push({
        label: v,
        value: current,
        checked: false,
        index: i,
      });
    }

    return {
      ...item,
      questions, // 题目
      ansList, // 正确答案
      selectAns: !item.user_answer ? [] : item.user_answer.split(","), // 选择的答案
      isRight: !!item.is_correct, // 是否正确
      isImage: item.title.includes("<img"),
      ind,
    };
  });
});

const handleClick = (item) => {
  if (props.onClick) {
    props.onClick(item);
    return;
  }
  router.push({
    url: "/pages/learn/rightPractice",
    params: {
      catalogue_id: props.catalogue_id,
      isRight: props.isRight,
    },
  });
  console.log(item);
};
</script>
<template>
  <!-- 问题内容 -->
  <view
    class="topic-content"
    v-for="(item, parindex) in dataList"
    :key="parindex"
    v-if="dataList.length"
  >
    <uvParse
      v-if="item.isImage"
      :content="item.title"
      class="question-text"
    ></uvParse>
    <rich-text v-else :nodes="item.title" class="question-text"></rich-text>
    <uvParse
      :content="item.questions_ex"
      v-if="item.style === 6 && item.isImage"
      class="question-text"
    ></uvParse>
    <rich-text
      v-else
      :nodes="item.questions_ex"
      class="question-text"
      :style="{
        lineHeight: 2,
      }"
    ></rich-text>
    <questions
      v-if="item.style !== 6"
      v-for="(question, index) in item.questions"
      :key="index"
      :answerList="item.ansList"
      :index="index"
      :styleCount="item.style"
      :question="question"
      :showResult="item.showResult"
      :parindex="parindex"
    />
    <view class="other" v-else>
      <questions
        v-for="(question, index) in item.questions"
        :key="index"
        :answerList="item.ansList"
        :index="index"
        :styleCount="item.style"
        :question="question"
        :showResult="item.showResult"
        :parindex="parindex"
      />
    </view>
    <div class="footer">
      <view v-if="isRight" class="c-primary" @click="handleClick(item)">{{
        onClick ? "答题" : "查看"
      }}</view>
      <view v-else class="c-error" @click="handleClick(item)">重做</view>
    </div>
  </view>
</template>
<style lang="scss" scoped>
@import "@/uni.scss";
.other {
  display: flex;
  gap: 12px;
  margin-top: 20rpx;
}
.footer {
  display: flex;
  align-items: center;
  justify-content: center;
}
.c-primary {
  padding: 2rpx 10rpx;
  border-radius: 4rpx;
  border: 1rpx solid $primary;
  color: $primary;
  font-size: 24rpx;
}

.c-error {
  padding: 2rpx 10rpx;
  border-radius: 4rpx;
  border: 1rpx solid $error;
  color: $error;
  font-size: 24rpx;
}
.content {
  font-family: PingFang SC, PingFang SC;
  font-weight: 500;
  font-size: 28rpx;
  color: #333333;
  display: flex;
  flex-direction: column;
  gap: 24rpx;
  padding: 0 30rpx;

  .ques-type {
    display: flex;
    gap: 16rpx;
    flex-direction: column;
  }
}
.grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 12px;
}
.bottom-modal {
  display: flex;
  flex-direction: column;
  gap: 20rpx;
  // padding: 20rpx;
}
.title {
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  .closeempty {
    position: absolute;
    right: 0;
  }
}
.parsing-text {
  height: 381rpx;
  white-space: normal;
}
.answer-content {
  display: flex;
  flex-direction: column;
  gap: 45rpx;
  font-family: PingFang SC, PingFang SC;
  font-weight: 500;
  font-size: 32rpx;
  color: #666666;
}
.topic-container {
  width: 100vw;
  overflow: hidden;
  position: relative;
}

.topic-item {
  display: flex;
  flex-direction: column;
  gap: 12px;
  position: relative;
  box-sizing: border-box;
}

.topic-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.topic-header-left {
  display: flex;
  gap: 8px;
  align-items: center;
}

.item {
  width: 72rpx;
  height: 72rpx;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1rpx solid #dddddd;
  &.success {
    border-color: $primary;
    color: $primary;
  }
}

.topic-type {
  border: 1px solid $uni-primary;
  padding: 0 8px;
  border-radius: 6px;
  color: $uni-primary;
  font-weight: 600;
  font-size: 14px;
}

.topic-count {
  color: #333;
  font-size: 14px;
}

.topic-content {
  display: flex;
  flex-direction: column;
  gap: 8px;
  background-color: #fff;
  margin-top: 16rpx;
  padding: 24rpx;
  border-radius: 16rpx;
}

.question-text {
  font-weight: bold;
  font-size: 14px;
  white-space: normal;
}

.answer-section {
  border-radius: 16rpx;
  border: 1px solid #ddd;
  padding: 24rpx;
  display: flex;
  flex-direction: column;
  gap: 32rpx;
}

.answer-row {
  font-size: 14px;
  display: flex;
  align-items: center;
  gap: 12px;
}

.answer-item {
  display: flex;
  gap: 8px;
  align-items: center;
  padding-right: 12px;
}

.tip {
  margin-left: auto;
}

.border-r-primary {
  border-right: 2px solid $uni-primary;
}

.answer-text {
  color: $uni-primary;
}

.button-group {
  display: flex;
  gap: 8px;
  position: sticky;
  bottom: 0;
  margin-top: auto;
}

.prev-btn {
  flex: 1;
  background-color: $uni-primary-light;
  color: $uni-primary;
}

.next-btn {
  flex: 1;
  background-color: $uni-primary;
  color: #fff;
}

.star-icon {
  display: flex;
  flex-direction: column;
  align-items: center;
  font-weight: 500;
  font-size: 20rpx;
  color: #000000;
}
</style>
