<script setup lang="ts">
import { choiceEnum } from '@/types/enum'
const props = withDefaults(
  defineProps<{
    optionList: [string[]]
    answer?: string | null
    submitAnswer?: string
    reportFlag: 0 | 1
    isDeadline?: boolean
  }>(),
  { isDeadline: true },
)

const emit = defineEmits(['submit'])

const selectChoice = ref<string | null>(null)
// 点击选项
function handleSelect(idx: number) {
  if (props.reportFlag) return
  if (selectChoice.value !== choiceEnum[idx]) {
    selectChoice.value = choiceEnum[idx]
    // 提交答案
    emit('submit', { submitAnswer: choiceEnum[idx], toNext: true })
    // selectChoice.value = null
  }
}

watch(
  () => props.submitAnswer,
  () => {
    if (props.submitAnswer) {
      selectChoice.value = props.submitAnswer
    } else {
      selectChoice.value = null
    }
  },
  {
    immediate: true,
  },
)

const content = computed(() => {
  return (str: string) => {
    return str?.replace(/http:\/\//gi, 'https://') || ''
  }
})
</script>
<template>
  <div>
    <div class="single-choice choice-box">
      <div
        v-for="(i, idx) in optionList?.[0]"
        :key="idx"
        :class="{
          'single-choice-item': true,
          pointer: true,
          active: selectChoice === choiceEnum[idx],
          success: isDeadline && reportFlag && answer === choiceEnum[idx],
          // && submitAnswer === choiceEnum[idx],
          fail: isDeadline && reportFlag && submitAnswer === choiceEnum[idx],
        }"
        @click="handleSelect(idx)"
      >
        <div class="item">{{ choiceEnum[idx] }}</div>
        <div class="content hidden_scroll_x">
          <latex-text :text="content(i)"></latex-text>
        </div>
      </div>
    </div>
    <slot name="markAnswer"></slot>
  </div>
</template>
<style scoped lang="scss">
.single-choice {
  // height: 100%;
  // overflow-y: auto;
  &-item {
    // width: 100%;
    min-height: 10.33vh;
    height: 100%;
    flex-shrink: 0;
    border-radius: 1.67vh;
    border: 0.33vh solid #bed5fe;
    background: #f7faf9;
    display: flex;
    // align-items: stretch;
    margin-bottom: 2vh;
    overflow: hidden;

    .item {
      width: 78px;
      // height: 100%;
      // line-height: 124px;
      display: flex;
      align-items: center;
      justify-content: center;
      flex-shrink: 0;
      background-color: #f1f6fe;
      border-right: 0.33vh solid #bed5fe;
      text-align: center;
      color: #669cfe;
      font-family: Arial;
      font-size: 3.33vh;
      font-style: normal;
      font-weight: 400;
    }
    .content {
      flex: 1;
      color: $font-color;
      font-family: Arial;
      font-size: 2.67vh;
      font-style: normal;
      font-weight: 400;
      line-height: 4vh; /* 150% */
      padding-left: 32px;
      display: flex;
      align-items: center;
      flex-wrap: wrap;
      white-space: pre-wrap;
    }
    &.active {
      border-color: #669cfe;
      .item {
        color: #fff;
        background-color: #a5c5fe;
        border-color: #669cfe;
      }
    }
    &.fail {
      border-color: #f00a0a;
      .item {
        color: #f00a0a;
        background-color: #febebf;
        border-color: #f00a0a;
      }
    }
    &.success {
      border-color: #8fcc00;
      .item {
        color: #8fcc00;
        background-color: #ddff8c;
        border-color: #8fcc00;
      }
    }
  }
}
</style>
