<!-- eslint-disable vue/no-v-html -->
<template>
  <div class="select-preview" :style="getAreaStyle()">
    <SerialNumberPosition v-if="isEnable(data.property.sn_display_mode)" :property="data.property" />

    <div class="main">
      <ul
        class="option-list"
        :style="{ flexDirection: data.property.arrange_type === arrangeTypeList[0].value ? 'row' : 'column' }"
      >
        <li
          v-for="({ content, mark, multilingual, paragraph_list }, i) in data.option_list"
          :key="mark"
          :style="{ cursor: disabled ? 'not-allowed' : 'pointer' }"
          :class="['option-item', { active: isAnswer(mark) }, ...computedAnswerClass(mark)]"
          @click="selectAnswer(mark)"
        >
          <span class="checkbox">
            <SvgIcon icon-class="check-mark" width="10" height="7" />
          </span>
          <span class="serial-number">
            {{ i + 1 }}
          </span>
          <PinyinText
            v-if="isEnable(data.property.view_pinyin)"
            class="content"
            :paragraph-list="paragraph_list"
            :pinyin-position="data.property.pinyin_position"
            :is-preview="true"
          />
          <span
            v-else
            class="content rich-text"
            :style="{ fontSize: type === typeList[0] ? '12pt' : '' }"
            v-html="sanitizeHTML(content)"
          ></span>
          <div v-if="showLang" class="lang">
            {{ multilingual.find((item) => item.type === getLang())?.translation }}
          </div>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import { getSelectData, arrangeTypeList } from '@/views/book/courseware/data/select';

import PreviewMixin from '../common/PreviewMixin';

export default {
  name: 'SelectPreview',
  mixins: [PreviewMixin],
  data() {
    return {
      data: getSelectData(),
      arrangeTypeList,
    };
  },
  watch: {
    'answer.answer_list': {
      handler(val) {
        if (val.length === 0) {
          this.answer.is_right = false;
          return;
        }
        if (val.length !== this.data.answer.answer_list.length) {
          this.answer.is_right = false;
          return;
        }
        this.answer.is_right = val.every((item) => this.data.answer.answer_list.includes(item));
      },
      deep: true,
    },
  },
  methods: {
    isAnswer(mark) {
      return this.answer.answer_list.includes(mark);
    },
    selectAnswer(mark) {
      if (this.disabled) return;

      const index = this.answer.answer_list.indexOf(mark);
      if (index === -1) {
        this.answer.answer_list.push(mark);
      } else {
        this.answer.answer_list.splice(index, 1);
      }
    },
    computedAnswerClass(mark) {
      if (!this.isJudgingRightWrong && !this.isShowRightAnswer) {
        return [];
      }
      let isHas = this.answer.answer_list.includes(mark); // 用户是否已选中
      let isRight = this.data.answer.answer_list.includes(mark); // 是否是正确答案

      let answerClass = [];
      if (!isHas && this.isShowRightAnswer) {
        answerClass = isRight ? ['answer-right'] : [];
      }
      // 判断是否是正确答案
      if (isHas && this.isJudgingRightWrong) {
        answerClass = isRight ? ['right'] : ['wrong'];
      }
      return answerClass;
    },
  },
};
</script>

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

.select-preview {
  @include preview-base;

  .option-list {
    display: flex;
    flex-flow: column wrap;
    gap: 16px;

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

      .lang {
        // width: 100%;
        padding-left: 52px;
      }

      .checkbox {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 14px;
        height: 14px;
        margin-right: 8px;
        cursor: pointer;
        background-color: #fff;
        border: 1px solid #dcdfe6;
        border-radius: 2px;

        .svg-icon {
          display: none;
        }
      }

      .serial-number {
        font-size: 16pt;
      }

      .content {
        flex: 1;
      }

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

        .checkbox {
          color: #fff;
          background-color: $light-main-color;

          .svg-icon {
            display: block;
          }
        }
      }

      &.answer-right {
        background-color: #e8f7f2;

        &::after {
          font-size: 14px;
          color: $right-color;
          white-space: nowrap;
          content: '正确答案';
        }
      }

      &.wrong {
        background-color: $content-color;
        box-shadow: 0 0 0 1px $error-color;

        &::after {
          font-size: 14px;
          color: #a09fa6;
          white-space: nowrap;
          content: '已选';
        }
      }
    }
  }
}
</style>
