<!-- eslint-disable vue/no-v-html -->
<template>
  <div class="select-preview">
    <div class="stem">
      <span class="question-number">{{ data.property.question_number }}.</span>
      <span v-html="sanitizeHTML(data.stem)"></span>
    </div>
    <div v-if="data.property.is_enable_description" class="description">{{ data.description }}</div>
    <AudioPlay
      v-if="data.property.is_enable_listening && data.file_id_list.length > 0"
      :file-id="data.file_id_list[0]"
    />
    <ul class="option-list">
      <li
        v-for="({ content, mark }, i) in data.option_list"
        :key="mark"
        :class="['option-item', { active: isAnswer(mark) }]"
        @click="selectAnswer(mark)"
      >
        <span class="selectionbox"></span>
        <span>{{ computeOptionMethods[data.option_number_show_mode](i) }}. </span>
        <span v-html="sanitizeHTML(content)"></span>
      </li>
    </ul>
  </div>
</template>

<script>
import { computeOptionMethods, selectTypeList } from '@/views/exercise_questions/data/common';

import PreviewMixin from './components/PreviewMixin';

export default {
  name: 'SelectPreview',
  mixins: [PreviewMixin],
  data() {
    return {
      computeOptionMethods,
    };
  },
  methods: {
    isAnswer(mark) {
      return this.answer.select_list.indexOf(mark) !== -1;
    },
    selectAnswer(mark) {
      const index = this.answer.select_list.indexOf(mark);
      if (this.data.property.select_type === selectTypeList[0].value) {
        this.answer.select_list = [mark];
      }
      if (this.data.property.select_type === selectTypeList[1].value) {
        if (index === -1) {
          this.answer.select_list.push(mark);
        } else {
          this.answer.select_list.splice(index, 1);
        }
      }
    },
  },
};
</script>

<style lang="scss" scoped>
@use '@/styles/mixin.scss' as *;

.select-preview {
  @include preview;

  .option-list {
    display: flex;
    flex-direction: column;
    row-gap: 16px;

    .option-item {
      display: flex;
      column-gap: 8px;
      align-items: center;
      padding: 12px 24px;
      color: #706f78;
      cursor: pointer;
      background-color: #f9f8f9;
      border-radius: 40px;

      .selectionbox {
        width: 14px;
        height: 14px;
        margin-right: 8px;
        border: 2px solid #dcdbdd;
        border-radius: 50%;
      }

      &.active {
        color: #34343a;
        background-color: #e7eeff;

        .selectionbox {
          border-color: #306eff;
          border-width: 4px;
        }
      }
    }
  }
}
</style>
