<script setup lang="ts">
import OptionsList from './OptionsList.vue'
import dayjs from '@/plugins/dayjs'
// import { useIntersectionObserver } from '@vueuse/core';
import { TypeSet } from '@/hooks'

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const props = withDefaults(
  defineProps<{
    correctFlag: 0 | 1
    subjectiveFlag?: 0 | 1
    data: anyObj
    time: string
    activeAudio: string
  }>(),
  {
    subjectiveFlag: 1,
  },
)

const emit = defineEmits(['startTrain', 'play', 'playVideo', 'showPic'])

// const componentEnum = getDict('t_subject_title_channel_type', true).enum;

const OSS_URL = import.meta.env.VITE_OSS_URL

const expand = ref(false)
function handleExpand() {
  expand.value = !expand.value
}

const quesItemRef = ref()
// 元素进入可视区域后再添加事件及mathjax渲染
// const { stop } = useIntersectionObserver(
//     quesItemRef,
//     async ([{ isIntersecting }], _observerElement) => {
//         if (isIntersecting) {
//             stop();
//             await nextTick();
//             document
//                 .querySelectorAll(`.ques-item-${props.data.id} .isTypeSet img`)
//                 .forEach((i: anyObj) => {
//                     i?.addEventListener(
//                         'click',
//                         () => {
//                             console.log('.isTypeSet');
//                             emit('showPic', i.src);
//                         },
//                         false,
//                     );
//                 });
//             TypeSet([document.querySelector(`.ques-item-${props.data.id} .isTypeSet`)]);
//         }
//     },
//     {
//         threshold: 0,
//     },
// );

// 给子题目添加序号
const filterTitle = computed(() => {
  return (title: string | undefined, cidx?: number) => {
    if (title) {
      const t = title.replace(/http:\/\//gi, 'https://')
      if (cidx !== undefined) {
        return `(${cidx + 1})` + t
      } else {
        return t
      }
    }
    return undefined
  }
})

function playVideo(i: anyObj) {
  if (i.videoList?.length) {
    emit('playVideo', i)
  }
}

onMounted(async () => {
  await nextTick()
  document.querySelectorAll(`.ques-item-${props.data.id} .isTypeSet img`).forEach((i: anyObj) => {
    i?.addEventListener(
      'click',
      () => {
        console.log('.isTypeSet')
        emit('showPic', i.src)
      },
      false,
    )
  })
  TypeSet([document.querySelector(`.ques-item-${props.data.id} .isTypeSet`)])
})
</script>
<template>
  <div ref="quesItemRef" :class="['ques-item', `ques-item-${data.id}`]">
    <div class="ques-item-box">
      <div class="isTypeSet">
        <div
          v-if="data?.pidTitle"
          class="ques-item-title"
          v-html="filterTitle(data?.pidTitle)"
        ></div>
        <div v-if="data?.title" class="ques-item-title" v-html="filterTitle(data?.title)"></div>
        <div v-if="data?.voiceUrl" class="ques-item-voice pointer">
          <Horn :active="activeAudio === data.id" @click="emit('play')"></Horn>
        </div>
        <div v-if="data?.children?.length" class="ques-item-children">
          <template v-for="(c, idx) in data.children" :key="c.id">
            <div class="ques-item-title" v-html="filterTitle(c?.title, idx)"></div>
          </template>
        </div>
        <div v-if="data?.optionList?.length" class="ques-item-options">
          <OptionsList :optionList="data.optionList"></OptionsList>
        </div>
        <div v-show="expand" class="ques-item-collapse">
          <div
            v-if="subjectiveFlag === 1 || (subjectiveFlag === 0 && correctFlag === 0)"
            class="answer-box"
          >
            <div class="answer-box-title">我的答案</div>
            <div
              class="answer-box-main"
              v-html="
                correctFlag
                  ? data?.answer
                  : data?.submitAnswer || (data?.submitAnswerPic ? '' : '未作答')
              "
            ></div>
            <div v-if="data?.submitAnswerPic" class="answer-box-main">
              <img :src="data?.submitAnswerPic" />
            </div>
          </div>
          <div v-if="data?.knowledgeVoList?.length" class="answer-box">
            <div class="answer-box-title">知识点</div>
            <div class="answer-box-main knowledge-list">
              <span
                v-for="i in data.knowledgeVoList"
                :key="i.id"
                :class="['knowledge-list-item', i.videoList?.length ? 'active' : null]"
                :style="{
                  'background-image': i.videoList?.length
                    ? `url(${OSS_URL}/urm/doWork_curvedLine.svg)`
                    : '',
                }"
                @click="playVideo(i)"
              >
                {{ i.name }}
              </span>
            </div>
          </div>
          <div v-if="data?.answer" class="answer-box">
            <div class="answer-box-title">参考答案</div>
            <div class="answer-box-main" v-html="data?.answer"></div>
          </div>

          <!-- 暂时隐藏 data.titleAnalyzeJson -->
          <div v-if="data?.titleAnalyze || data?.titleAnalyzePicture" class="answer-box">
            <div class="answer-box-title">解析</div>
            <div v-if="data?.titleAnalyze" class="answer-box-main analyze analyze-title">
              <div
                class="titleAnalyze"
                v-html="data?.titleAnalyze?.replace(/http:\/\//gi, 'https://') || ''"
              ></div>
            </div>
            <div v-if="data?.titleAnalyzePicture" class="answer-box-main analyze analyze-pic">
              <div
                v-for="i in data.titleAnalyzePicture.split(',')"
                :key="i"
                class="titleAnalyzePicture pointer"
                @click="emit('showPic', i)"
              >
                <img :src="i" />
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="ques-item-split_line"></div>
      <div class="ques-item-bottom">
        <div class="ques-item-bottom-time">
          {{ dayjs(time).format('YYYY/MM/DD HH:mm') }}
        </div>
        <slot name="customBtn">
          <mj-button type="warn" width="192" height="64" size="32" @click="emit('startTrain')">
            举一反三
          </mj-button>
        </slot>
        <mj-button
          type="warn"
          width="192"
          height="64"
          size="32"
          :class="{ expand: expand }"
          @click="handleExpand"
        >
          解析
          <Icon name="textbook_arrow_up" width="46"></Icon>
        </mj-button>
      </div>
    </div>
  </div>
</template>
<style scoped lang="scss">
.ques-item {
  background-color: #fff;
  padding: 2.67vh 32px 0;
  border-radius: 1.67vh;
  margin-bottom: 1.33vh;
  line-height: 1.5;
  position: relative;
  &:last-child {
    margin-bottom: 0;
  }
  &-title {
    color: $font-color;
    font-family: OPPOSans;
    font-size: 2.67vh;
    font-style: normal;
    font-weight: 400;
    margin-bottom: 1.33vh;
    overflow: auto;
  }
  &-collapse {
    margin-top: 2.67vh;
  }
  .answer-box {
    margin-top: 2vh;
    &-title {
      padding: 0 20px 0 32px;
      position: relative;
      color: $font-color;
      font-family: $font-special;
      font-size: 2.67vh;
      border-radius: 0.33vh;
      margin-bottom: 2vh;
      &::before {
        content: '';
        width: 12px;
        height: 100%;
        flex-shrink: 0;
        position: absolute;
        top: 0;
        left: 0;
        background-color: #669cfe;
        border-radius: 0 1vh 1vh 0;
      }
    }
    &-main {
      color: $font-color;
      font-family: 'OPPO Sans';
      font-size: 2.67vh;
      font-style: normal;
      font-weight: 400;
      padding-left: 32px;
      box-sizing: border-box;
      width: 100%;
      &.analyze {
        // padding-left: 0;
        width: 100%;
        &.analyze-pic {
          margin-top: 2vh;
          img {
            max-width: 100%;
            max-height: 66.67vh;
            object-fit: contain;
          }
          .titleAnalyzePicture {
            margin-bottom: 1vh;
            &:last-child {
              margin-bottom: 0;
            }
          }
        }
        &.analyze-title {
          margin-top: 2vh;

          .titleAnalyze {
            color: $font-color;
            font-family: 'OPPO Sans';
            font-size: 2.67vh;
          }
        }
      }
      .knowledge-list {
        &-item {
          margin-right: 18px;
          position: relative;
          display: inline-block;
          padding-bottom: 1vh;

          &:last-child {
            margin-right: 0;
          }
          &.active {
            cursor: pointer;
            color: #8409d6;
            background-position: -10px bottom;
            background-size: 19px 12px;
            background-repeat: repeat no-repeat;
          }
        }
      }
    }
  }
  &-split_line {
    width: 100%;
    height: 0.33vh;
    position: relative;
    margin-top: 2vh;
    &::after {
      position: absolute;
      top: 50%;
      left: 0;
      transform: translateY(-50%);
      content: '';
      display: block;
      width: 100%;
      height: 0.33vh;
      background: repeating-linear-gradient(
        to right,
        #deedfc,
        #deedfc 8px,
        transparent 4px,
        transparent 16px /* 线条间隔 */
      );
    }
    &:before {
      content: '';
      width: 18px;
      height: 1.5vh;
      background: #ddedfb;
      position: absolute;
      top: 50%;
      left: 0;
      transform: translateY(-50%) rotate(45deg);
    }
  }
  &-bottom {
    height: 8vh;
    display: flex;
    align-items: center;
    &-time {
      flex: 1;
      color: #999;
      font-family: OPPOSans;
      font-size: 2vh;
    }
    .mj-button {
      min-height: 5.67vh;
      margin-left: 24px;
      border-radius: 4.42vh;
      border: 0.17vh solid #fff;
      box-shadow: 0 0.17vh 0.17vh 0 rgba(0, 0, 0, 0.25);
      color: $font-color;
      .x-icon {
        // margin-left: 0vh;
        transform: rotate(180deg);
      }
      &.expand {
        background: #fff;
        border: 0.17vh solid #fff01f;
        :deep(img) {
          display: none;
        }
        .x-icon {
          transform: rotate(0);
          :deep(img) {
            display: block !important;
          }
        }
      }
    }
  }
}
:global(.ques-item-title .fill) {
  display: inline-block;
  width: 100px;
  border-bottom: 0.25vh solid $font-color;
}
.fade-enter-active,
.fade-leave-active {
  transition: all 0.2s linear;
  // opacity: 1;
}

.fade-enter-from,
.fade-leave-to {
  height: 0;
  // opacity: 0;
}
</style>
