<script setup lang="ts">
import type { Contest } from '../types'
import dayjs from 'dayjs'
import { contestMode } from '@/utils/dictionary'
import { updateTrainCount } from '../api'
interface Props {
  data: Contest
}
const props = defineProps<Props>()
const router = useRouter()
const isMy = $computed(() => {
  return !!props.data.login_id
})
const modeText = $computed(() => {
  return contestMode[props.data.mode]
})

// 是否可以训练
const canTrain = $computed(() => {
  return dayjs().isBefore(dayjs(parseInt(props.data.end_at) * 1000))
})
function handleTrain() {
  // 训练计数
  updateTrainCount({ competition_id: props.data.id })
  router.push({ path: `/student/contest/lab/${props.data.id}` })
}
// 是否可以报名
const canJoin = $computed(() => {
  return dayjs().isBefore(dayjs(parseInt(props.data.apply_expiration_date) * 1000))
})

// 我要报名
function handleJoin() {
  router.push({ path: '/student/contest/join', query: { id: props.data.id, name: props.data.name } })
}
</script>

<template>
  <div class="contest-item">
    <div class="contest-item-main">
      <div class="contest-item-main__bg"><img :src="data.cover" /></div>
      <div class="contest-item-main__inner">
        <!-- <img :src="data.logo" class="logo" /> -->
        <div class="cover">
          <template v-if="isMy">
            <ul class="info-list">
              <li>姓名：{{ data.student_name }}</li>
              <li>参赛ID：{{ data.login_id }}</li>
              <li>参赛形式：{{ modeText }}</li>
              <li>所属学校：{{ data.org_name }}</li>
            </ul>
            <el-button round type="primary" @click="handleTrain" v-if="canTrain">我要训练</el-button>
          </template>
          <template v-else>
            <el-button round :disabled="!canJoin" type="primary" @click="handleJoin">我要报名</el-button>
          </template>
        </div>
      </div>
    </div>
    <div class="contest-item-name">{{ data.name }}</div>
  </div>
</template>

<style lang="scss">
.contest-item-main {
  position: relative;
  height: 200px;
  border-radius: 10px;
  overflow: hidden;
}
.contest-item-main__bg {
  width: 100%;
  height: 100%;
  img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    // opacity: 0.5;
  }
}
.contest-item-main__inner {
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  z-index: 1;
  .logo {
    width: 100%;
    height: 100%;
    object-fit: contain;
  }
  &:hover {
    .cover {
      display: flex;
    }
  }
  .cover {
    display: none;
    position: absolute;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    background-color: rgba(0, 0, 0, 0.7);
    .info-list {
      padding: 10px;
      font-size: 14px;
      color: #fff;
      line-height: 24px;
    }
  }
}
.contest-item-name {
  margin-top: 10px;
  padding: 10px;
  font-size: 16px;
  color: #fff;
  text-align: center;
  background-color: var(--main-color);
  border-radius: 10px;
}
</style>
