<template>
  <div v-show="props.modelValue" class="mask">
    <div class="record_detail_popup">
      <div v-if="!islastPop" class="btn_close" @click="close">
        <img class="full-w-h" :src="btn_close" />
      </div>
      <div
        v-if="islastPop"
        class="btn btn__add animate__animated animate__slideInLeft pointer"
        @click="nextAround"
      >
        <div class="btn__icon animate__animated animate__infinite animate__breath">
          <img :src="img_arrow" />
        </div>
        <div class="btn__text">下一轮</div>
      </div>
      <div
        v-if="islastPop"
        class="btn btn__next animate__animated animate__slideInLeft pointer"
        @click="back"
      >
        <div class="btn__icon animate__animated animate__infinite animate__breath">
          <img :src="img_close" />
        </div>
        <div class="btn__text">退出</div>
      </div>
      <div class="record_data">
        <div class="record_data_item">
          <img class="icon" :src="accuracy" />
          <div class="label">成绩</div>
          <div class="value">{{ props.rightNum }}/{{ props.totalNum }}</div>
        </div>
        <div class="record_data_item">
          <img class="icon" :src="stopwatch" />
          <div class="label">耗时</div>
          <div class="value">{{ dealTime((props.duration || 0) / 1000) }}</div>
        </div>
        <div class="record_data_item">
          <img class="icon" :src="clock" />
          <div class="label">时间</div>
          <div class="value">{{ props.createTime?.replace(/-/g, '/') }}</div>
        </div>
        <div v-for="item in rewardList" :key="item.iconname" class="record_data_item reward">
          <span class="label">{{ item.name }}</span>
          <img class="icon" :src="item.iconname" />
          <span class="value">+{{ item.number || 0 }}</span>
        </div>
      </div>
      <div class="answer_record_list hidden_scroll">
        <card
          v-for="item in props.wordVoList"
          :key="item.id"
          v-bind="item as any"
          :result="item.testResult === 1 ? question_right : question_wrong"
          @click="playAccent(item)"
        ></card>
      </div>
    </div>
  </div>
</template>
<script setup lang="ts">
import card from './Card.vue'
import type { wordRecord } from '../type'
import useAudio from '@/hooks/useCommonAudio'
import { question_right, question_wrong } from '../../..'
import { OSS_URL } from '@/utils'

const props = defineProps<{
  modelValue: boolean
  rightNum?: number
  totalNum?: number
  duration?: number
  createTime?: string
  exp?: number
  diamond?: number
  wordVoList?: wordRecord[]
  islastPop?: boolean
}>()
const emits = defineEmits(['update:modelValue', 'back', 'next'])

const img_arrow = `${OSS_URL}/xuexiaole_clinet/normalMode/arrow.png`
const img_close = `${OSS_URL}/xuexiaole_clinet/normalMode/close.png`
const img_again = `${OSS_URL}/xuexiaole_clinet/normalMode/again.png`

const btn_close = `${OSS_URL}/xuexiaole_clinet/normalMode/btn_close.png`
const accuracy = `${OSS_URL}/xuexiaole_clinet/normalMode/accuracy.png`
const stopwatch = `${OSS_URL}/xuexiaole_clinet/normalMode/stopwatch.png`
const clock = `${OSS_URL}/xuexiaole_clinet/normalMode/clock.png`

function dealTime(time: number) {
  const h = parseInt(String((time / 60 / 60) % 24), 10)
  const m = parseInt(String((time / 60) % 60), 10)
  const s = parseInt(String(time % 60), 10)

  if (time === undefined) return

  if (h > 0) {
    return m === 0 ? `${h}h` : `${h}h ${m}m`
  } else if (m > 0) {
    return s === 0 ? `${m}m` : `${m}m ${s}s`
  } else if (s > 0) {
    return `${s}s`
  } else {
    return '0'
  }
}
const rewardList = computed(() => {
  return [
    {
      iconname: `${OSS_URL}/xuexiaole_clinet/normalMode/icon_exp.png`,
      name: '奖励',
      number: props.exp,
    },
    {
      iconname: `${OSS_URL}/icon/diamond_new.svg`,
      number: props.diamond,
    },
  ]
})
const audio = useAudio()
function playAccent(item: wordRecord) {
  if (item.usAccents) {
    audio.play(item.usAccents)
  }
}

function back() {
  // 返回
  emits('back')
  emits('update:modelValue', false)
}
function nextAround() {
  // 下一轮
  emits('next')
  emits('update:modelValue', false)
}

function close() {
  emits('update:modelValue', false)
}
</script>
<style lang="scss" scoped>
* {
  box-sizing: border-box;
}
.mask {
  background: rgba(0, 0, 0, 0.45);
  backdrop-filter: blur(4px);
  position: fixed;
  z-index: 999;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}
.record_detail_popup {
  position: relative;
  width: 1530px;
  height: 65.83vh;
  border-radius: 3.33vh;
  background-color: #fff;
  .btn_close {
    position: absolute;
    right: -12px;
    top: -1.5vh;
    width: 4vh;
    height: 4vh;
    cursor: pointer;
  }
  .btn {
    position: absolute;
    cursor: pointer;
    right: -140px;
    z-index: -1;

    width: 145px;
    padding: 2.67vh 20px;
    border-radius: 0 1.67vh 1.67vh 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;

    .btn__icon img {
      width: 8vh;
      height: 8vh;
    }

    .btn__text {
      color: #d9d163;
      text-align: center;
      font-size: 2vh;
      font-weight: 500;
    }
    &.btn__add {
      top: 12.5vh;
      animation-delay: 0.5s;
      animation-duration: 0.5s;
      animation-fill-mode: both;
      height: 21.67vh;
      border: 0.33vh solid #d9d163;
      border-left: none;
      background-color: #fffbc2;
      animation-delay: 0.5s;
      animation-duration: 0.5s;
      animation-fill-mode: both;

      .btn__text {
        color: #d9d163;
      }
    }
    &.btn__next {
      top: 37.5vh;
      animation-delay: 1s;
      animation-duration: 0.5s;
      animation-fill-mode: both;
      height: 16.33vh;
      border: 0.33vh solid #bf4d45;
      border-left: none;
      background-color: #fff2f2;
      animation-delay: 0s;
      animation-duration: 0.5s;
      animation-fill-mode: both;

      .btn__text {
        color: #bf4d45;
      }
    }
  }
  .record_data {
    display: flex;
    margin: 2vh 24px 0;
    padding-bottom: 1.67vh;
    border-bottom: 0.17vh dashed #d9d163;
    .record_data_item {
      display: flex;
      align-items: center;
      & + .record_data_item {
        margin-left: 48px;
      }
      .icon {
        width: 4vh;
        height: 4vh;
      }
      .label {
        color: rgba(0, 0, 0, 0.45);
        font-family: 'PingFang SC';
        font-size: 2.33vh;
        margin: 0 16px 0 8px;
      }
      .value {
        color: rgba(0, 0, 0, 0.75);
        font-family: 'PingFang SC';
        font-size: 2.67vh;
      }
      &.reward:last-child {
        margin-left: 10px;
      }
    }
  }
  .answer_record_list {
    max-height: 56.17vh;
    padding: 2vh 14px 2vh 24px;
    display: flex;
    align-items: flex-start;
    flex-wrap: wrap;
    overflow: auto;
    :deep(.card) {
      &:not(:nth-child(5n)) {
        margin-right: 20px;
      }
      &:nth-child(n + 6) {
        margin-top: 1.67vh;
      }
    }
  }
}
</style>
