<script setup lang="ts">
import type { Contest } from '../types'
import dayjs from 'dayjs'
import { updateTrainCount } from '../api'
import { useMapStore } from '@/stores/map'

interface Props {
  data: Contest
}
const props = defineProps<Props>()
const router = useRouter()
const isMy = $computed(() => {
  return !!props.data.login_id
})

const types = useMapStore().getMapValuesByKey('competition_type')
// 赛项类型
const typeText = $computed(() => {
  return types.find(item => item.value == props.data.type)?.label
})
// 是否可以训练
const canTrain = $computed(() => {
  return dayjs().isBefore(dayjs(parseInt(props.data.end_at) * 1000))
})
function handleTrain(id: string, type: string) {
  // 训练计数
  updateTrainCount({ competition_id: id })
  window.open(`/student/contest/lab/${id}?type=${type}`)
  // router.push({ path: `/student/contest/lab/${id}`, query: { type: type } })
}
// 是否可以报名
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 } })
}
function formatDate(value: string) {
  return dayjs(parseInt(value) * 1000).format('YYYY-MM-DD')
}
</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>赛项类型：{{ typeText }}</li>
              <li>赛项周期：{{ formatDate(data.start_range) }} 至 {{ formatDate(data.end_range) }}</li>
              <li>正式比赛日期：{{ formatDate(data.start_at) }}</li>
            </ul>
            <!-- {{ data.train_platform_configs }} -->
            <!-- <el-button v-if="data.train_platform_configs" round type="primary" @click="handleTrain" v-if="canTrain">我要训练</el-button> -->
            <div style="display: flex; flex-wrap: wrap; justify-content: space-evenly">
              <template v-for="item in data.train_platform_configs">
                <el-button
                  v-if="item.is_show === '1' && canTrain"
                  style="margin-bottom: 10px; margin-left: 0"
                  :key="item"
                  round
                  type="primary"
                  @click="handleTrain(item.competition_id, item.platform_key)"
                  >{{ item.name }}</el-button
                >
              </template>
            </div>
          </template>
          <template v-else>
            <ul class="info-list">
              <li>主办单位：{{ data.host_unit.label }}</li>
              <li>赛项类型：{{ typeText }}</li>
              <li>赛项周期：{{ formatDate(data.start_range) }} 至 {{ formatDate(data.end_range) }}</li>
              <li>正式比赛日期：{{ formatDate(data.start_at) }}</li>
            </ul>
            <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: 240px;
  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>
