<script setup lang="ts">
import DragPanel from '@/components/DragPanel.vue'
import { useCompetition } from '../composables/useCompetition'
import { useAppConfig } from '@/composables/useAppConfig'
import { useNow } from '@vueuse/core'
import dayjs from 'dayjs'
import { formatDuration } from '@/utils/utils'
import { ElMessage, ElMessageBox } from 'element-plus'

const appConfig = useAppConfig()

const Case = defineAsyncComponent(() => import('../components/Case.vue'))

const { courseId, experimentId, competition, submit } = useCompetition()

const LAB_URL = computed<string>(() => {
  return `${appConfig.dmlURL || import.meta.env.VITE_DML_URL}/live/test?experiment_id=${experimentId.value}`
})

const isEntry = ref(false)
const now = useNow({ interval: 1000 })

const isSubmitted = computed(() => {
  return competition.value?.student_status?.has_submitted
})

const canEnter = computed(() => {
  if (isSubmitted.value) return false
  if (!competition.value?.start_time || !competition.value?.end_time) return false
  const current = dayjs(now.value)
  const start = dayjs(competition.value.start_time)
  const end = dayjs(competition.value.end_time)
  return current.isAfter(start) && current.isBefore(end)
})

const countdown = computed(() => {
  if (!competition.value?.start_time || !competition.value?.end_time) {
    return { label: '倒计时', value: '--:--:--' }
  }

  const current = dayjs(now.value)
  const end = dayjs(competition.value.end_time)

  if (current.isBefore(end)) {
    return { label: '距离考试结束', value: formatDuration(end.diff(current)) }
  }

  return { label: '考试已结束', value: '00:00:00' }
})

const handleEntry = () => {
  isEntry.value = true
}
const handleSubmit = async () => {
  ElMessageBox.confirm('此操作将会提交该实验，状态会变为已提交，您将不能再操作该实验，确定提交考试吗？', '提示').then(
    async () => {
      await submit()
      ElMessage.success('提交成功')
      isEntry.value = false
    }
  )
}
</script>
<template>
  <DragPanel v-if="isEntry">
    <template #left>
      <div class="left-box">
        <h2 class="left-box-title">实操试题</h2>
        <div class="left-box-content">
          <Case :course_id="courseId" :experiment_id="experimentId" />
        </div>
      </div>
    </template>
    <template #right>
      <AppCard>
        <div class="competition-header">
          <div class="time">
            <div class="label">倒计时：</div>
            <div class="value">{{ countdown.value }}</div>
          </div>
          <el-button type="primary" @click="handleSubmit">提交考试</el-button>
        </div>
      </AppCard>
      <div class="iframe-box">
        <iframe
          allow="camera; microphone"
          allowfullscreen
          :src="LAB_URL"
          frameborder="0"
          class="iframe"
          ref="iframeRef"></iframe>
      </div>
    </template>
  </DragPanel>
  <template v-else>
    <div class="welcome-box" v-if="competition?.name">
      <div class="welcome-box-header">
        <div class="welcome-box-header-content">
          <h1>{{ competition?.name }}</h1>
          <h2>考试时间</h2>
          <div class="line"></div>
          <p>{{ competition?.start_time }} ～ {{ competition?.end_time }}</p>
        </div>
      </div>
      <el-button v-if="canEnter" type="primary" size="large" @click="handleEntry" class="entry-btn"
        >进入实操考试</el-button
      >
      <el-button plain v-if="isSubmitted" type="primary" size="large" class="entry-btn">考试已提交</el-button>
    </div>
    <el-empty description="暂无比赛" v-else />
  </template>
</template>

<style lang="scss" scoped>
.left-box {
  height: 100%;
  display: flex;
  flex-direction: column;
}
.left-box-title {
  font-size: 22px;
  font-weight: 600;
  color: var(--main-color);
  line-height: 32px;
  text-align: center;
  margin-bottom: 20px;
}
.left-box-content {
  flex: 1;
}
.competition-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  .time {
    display: flex;
    align-items: center;
    gap: 4px;
    .label {
      font-size: 14px;
      color: var(--main-color);
      font-weight: 500;
    }
    .value {
      font-size: 24px;
      font-weight: 700;
      letter-spacing: 2px;
      color: var(--main-color);
      line-height: 1;
    }
  }
}
.iframe-box {
  position: relative;
  flex: 1;
  width: 100%;
  margin-top: 20px;
  display: flex;
}
.iframe {
  width: 100%;
  height: 100%;
}
.welcome-box {
  margin: -20px;
  height: calc(100vh - 66px);
  .entry-btn {
    display: block;
    width: 300px;
    margin: 100px auto;
  }
}
.welcome-box-header {
  height: 50%;
  background: url('/public/images/competition_bg.jpg') no-repeat top center;
  background-size: cover;
  display: flex;
  align-items: flex-end;
}
.welcome-box-header-content {
  width: 100%;
  max-width: 1000px;
  margin: 0 auto;
  color: #fff;
  padding: 40px 0;

  h1 {
    font-size: 36px;
    font-weight: 600;
    color: #fff;
    line-height: 50px;
    letter-spacing: 7px;
    text-shadow: 0px 2px 4px rgba(0, 0, 0, 0.5);
  }
  h2 {
    margin-top: 20px;
    font-size: 18px;
    font-weight: 700;
    color: #fff;
    line-height: 25px;
    text-shadow: 0px 2px 4px rgba(0, 0, 0, 0.5);
  }
  .line {
    width: 24px;
    height: 2px;
    background: #ffffff;
    box-shadow: 0 2px 4px #00000080;
    margin: 10px 0;
  }
  p {
    font-size: 18px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: #fff;
    line-height: 25px;
    text-shadow: 0px 2px 4px rgba(0, 0, 0, 0.5);
  }
}
</style>
