<template>
  <div class="driver-detail" v-if="driver">
    <div class="header" :style="{ background: getColor(driver.team) }">
      <svg class="vertical-bar1" viewBox="0 0 1000 500" preserveAspectRatio="none">
        <rect x="0" y="0" width="15" height="100" fill="white" opacity="1" />
        <rect x="18" y="0" width="15" height="100" fill="white" opacity="1" />
      </svg>
      <svg class="vertical-bar2" viewBox="0 0 1000 500" preserveAspectRatio="none">
        <rect x="0" y="0" width="15" height="100" fill="white" opacity="1" />
        <rect x="18" y="0" width="15" height="100" fill="white" opacity="1" />
      </svg>
      <img :src="getImage(driver.name)" alt="driver" class="photo">
      <div class="info">
        <h1>{{ driver.name }}</h1>
        <div class="meta">{{ driver.team }} · {{ driver.nation }} · #{{ driver.carNum }}</div>
        <div class="stats">
          <div class="stat"><span>生涯胜场</span><b>{{ dc?.wins }}</b></div>
          <div class="stat"><span>登上领奖台</span><b>{{ dc?.podiums }}</b></div>
          <div class="stat"><span>分站参赛</span><b>{{ dc?.races }}</b></div>
        </div>
      </div>
    </div>
    <div class="bio">
      <div class="intro">
        <h2>简介<br></br>2025赛季</h2>
        <div class="content">
          <div class="content-row line">
            <div class="content-col">
              <span class="label">姓名</span><br>
              <span class="value">{{ driver.name }}</span>
            </div>
            <div class="content-col">
              <span class="label">国籍</span><br>
              <span class="value">{{ driver.country }}</span>
            </div>
          </div>
          <div class="content-row">
            <div class="content-col">
              <span class="label">正赛次数</span><br>
              <span class="value">{{ driver_statistics.formal.totalCnt }}</span>
            </div>
            <div class="content-col">
              <span class="label">正赛积分</span><br>
              <span class="value">{{ driver_statistics.formal.scoreSum }}</span>
            </div>
          </div>
          <div class="content-row">
            <div class="content-col">
              <span class="label">正赛胜场</span><br>
              <span class="value">{{ driver_statistics.formal.gold }}</span>
            </div>
            <div class="content-col">
              <span class="label">正赛领奖台</span><br>
              <span class="value">{{ driver_statistics.formal.medal }}</span>
            </div>
          </div>
          <div class="content-row">
            <div class="content-col">
              <span class="label">正赛杆位</span><br>
              <span class="value">{{ driver_statistics.formal.pole }}</span>
            </div>
            <div class="content-col">
              <span class="label">正赛前十</span><br>
              <span class="value">{{ driver_statistics.formal.topTen }}</span>
            </div>
          </div>
          <div class="content-row line">
            <div class="content-col">
              <span class="label">最快单圈</span><br>
              <span class="value">{{ driver_statistics.formal.fastestLap }}</span>
            </div>
            <div class="content-col">
              <span class="label">未完赛</span><br>
              <span class="value">{{ driver_statistics.formal.unfinished }}</span>
            </div>
          </div>
          <div class="content-row">
            <div class="content-col">
              <span class="label">冲刺赛</span><br>
              <span class="value">{{ driver_statistics.sprint.totalCnt }}</span>
            </div>
            <div class="content-col">
              <span class="label">冲刺赛积分</span><br>
              <span class="value">{{ driver_statistics.sprint.scoreSum }}</span>
            </div>
          </div>
          <div class="content-row">
            <div class="content-col">
              <span class="label">冲刺赛胜场</span><br>
              <span class="value">{{ driver_statistics.sprint.gold }}</span>
            </div>
            <div class="content-col">
              <span class="label">冲刺赛领奖台</span><br>
              <span class="value">{{ driver_statistics.sprint.medal }}</span>
            </div>
          </div>
          <div class="content-row line">
            <div class="content-col">
              <span class="label">冲刺赛杆位</span><br>
              <span class="value">{{ driver_statistics.sprint.pole }}</span>
            </div>
            <div class="content-col">
              <span class="label">冲刺赛前十</span><br>
              <span class="value">{{ driver_statistics.sprint.topTen }}</span>
            </div>
          </div>
        </div>
        <div class="toresult">
          <el-button type="primary" @click="goResult">详细比赛结果</el-button>
        </div>
      </div>
      <div class="statics">
        <h2>生涯数据</h2>
        <div class="content">
          <div class="statics-row line">
            <span class="item">参加比赛场次</span>
            <span class="itemvalue">{{ dc?.races }}</span>
          </div>
          <div class="statics-row line">
            <span class="item">生涯得分</span>
            <span class="itemvalue">{{ dc?.points }}</span>
          </div>
          <div class="statics-row line">
            <span class="item">最佳完赛成绩</span>
            <span class="itemvalue">{{ dc?.hf }}</span>
          </div>
          <div class="statics-row line">
            <span class="item">领奖台数</span>
            <span class="itemvalue">{{ dc?.podiums }}</span>
          </div>
          <div class="statics-row line">
            <span class="item">最佳发车位次</span>
            <span class="itemvalue">{{ dc?.hg }}</span>
          </div>
          <div class="statics-row line">
            <span class="item">杆位数</span>
            <span class="itemvalue">{{ dc?.polepositions }}</span>
          </div>
          <div class="statics-row line">
            <span class="item">世界冠军数</span>
            <span class="itemvalue">{{ dc?.wc }}</span>
          </div>
          <div class="statics-row">
            <span class="item">未完赛数</span>
            <span class="itemvalue">{{ dc?.dnfs }}</span>
          </div>
        </div>
      </div>
    </div>
    <div class="actions">
      <el-button @click="goBack">返回</el-button>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useDriversStore } from '@/store/SeasonDrivers'
import { getColor, getImage , driver_career } from '@/assets/source'

const route = useRoute()
const router = useRouter()
const driversStore = useDriversStore()

const driver = ref<any>()
const dc = computed(() => driver_career.find((d: any) => d.name === driver.value.name))
const driver_statistics = ref<any>()

onMounted(async () => {
  const id = Number(route.params.id)
  try {
    driversStore.ensureDriversLoaded()
    driver.value = driversStore.driversList.find((d: any) => (d.id) === id)
    const sres = await fetch(`/api/drivers/${id}/statistics?season=2025`)
    driver_statistics.value = await sres.json()
  } catch (e) {
    driver.value = undefined
  }
})

const goResult = () => {
  const d = driver.value
  if (!d) return
  router.push(`/drivers/${d.id}/results`)
}

const goBack = () => {
  router.push('/drivers')
}
</script>

<style scoped lang="scss">
@font-face {
  font-family: 'F1';
  src: url('../assets/Formula1-Black.woff2') format('woff2');
  font-weight: normal;
  font-style: normal;
  font-display: swap;
}

.driver-detail {
  padding: 20px;
}

.header {
  display: flex;
  flex-direction: row-reverse;
  justify-content: space-around;
  align-items: center;
  gap: 20px;
  max-height: 500px;
  overflow: hidden;
  border-radius: 20px 20px 0 0;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.7);
  position: relative;
}

.photo {
  height: 1250px;
  margin-top: 750px;
}

.info {
  font-family: 'F1';
}

.info h1 {
  font-size: 36px;
  font-family: 'F1';
}

.vertical-bar1 {
  position: absolute;
  top: 0;
  left: 250px;
}

.vertical-bar2 {
  position: absolute;
  top: 400px;
  left: 250px;
}

.meta {
  color: #666;
  margin-top: 4px;
}

.stats {
  display: flex;
  gap: 20px;
  margin-top: 12px;
}

.stat span {
  font-size: 12px;
  color: #888;
}

.stat b {
  display: block;
  font-size: 22px;
}

.bio {
  background-color: #1c1c25;
  color: #fff;
  display: flex;
  flex-direction: row;
  justify-content: space-evenly;
  padding-bottom: 30px;
  border-radius: 0 0 20px 20px;
}

.intro {
  width: 45%;
}

.statics {
  width: 45%;
  border-radius: 20px;
  background-color: #303037;
  margin-top: 150px;
}

.intro h2,
.statics h2 {
  font-size: 40px;
  margin-top: 0;
  padding-top: 40px;
}

.intro h2 {
  margin-left: 20px;
}

.statics h2 {
  text-align: center;
}

.content-row {
  display: flex;
  margin-bottom: 20px;
}

.content-col {
  width: 50%;
  padding: 0;
}

.label {
  font-size: 14px;
  color: #aaaaaa;
}

.value {
  font-weight: bold;
  font-size: 25px;
  display: block;
  margin-top: 10px;
}

.line {
  border-bottom: 2px solid gray;
}

.statics .content {
  width: 90%;
  margin: 0 auto;
}

.statics-row {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  padding: 20px;
  align-items: center;
}

.item {
  font-size: 14px;
  color: #aaaaaa;
}

.itemvalue {
  font-weight: bold;
  font-size: 25px;
}

.actions {
  margin-top: 20px;
}
</style>
