<script setup lang="ts">
import { RouterLink } from 'vue-router';
import type { AdminBreadcrumbItem } from '../utils/admin-breadcrumb';

defineProps<{
  crumbs?: AdminBreadcrumbItem[];
  title: string;
  subtitle?: string;
}>();
</script>

<template>
  <header class="admin-subnav">
    <nav v-if="crumbs?.length" class="admin-subnav__crumbs" aria-label="Breadcrumb">
      <template v-for="(item, i) in crumbs" :key="`${item.label}-${i}`">
        <RouterLink
          v-if="item.to && i < crumbs.length - 1"
          :to="item.to"
          class="admin-subnav__link"
        >
          {{ item.label }}
        </RouterLink>
        <span v-else class="admin-subnav__current">{{ item.label }}</span>
        <span v-if="i < crumbs.length - 1" class="admin-subnav__sep" aria-hidden="true">/</span>
      </template>
    </nav>
    <div class="admin-subnav__main">
      <div class="admin-subnav__titles">
        <h1 class="admin-subnav__title">{{ title }}</h1>
        <span v-if="subtitle" class="admin-subnav__subtitle">{{ subtitle }}</span>
      </div>
      <div v-if="$slots.extra" class="admin-subnav__extra">
        <slot name="extra" />
      </div>
    </div>
  </header>
</template>

<style scoped>
.admin-subnav {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-bottom: 16px;
  flex-shrink: 0;
}
.admin-subnav__crumbs {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 2px 6px;
  font-size: 12px;
  line-height: 1.4;
}
.admin-subnav__link {
  color: var(--text-muted);
  font-weight: 500;
  transition: color 0.15s;
}
.admin-subnav__link:hover {
  color: var(--text);
}
.admin-subnav__current {
  color: var(--text-muted);
}
.admin-subnav__sep {
  color: #b6afa3;
  user-select: none;
}
.admin-subnav__main {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}
.admin-subnav__titles {
  display: flex;
  align-items: baseline;
  flex-wrap: wrap;
  gap: 8px 12px;
  min-width: 0;
}
.admin-subnav__title {
  margin: 0;
  font-size: 20px;
  font-weight: 800;
  color: var(--text);
  letter-spacing: 0;
}
.admin-subnav__subtitle {
  font-size: 13px;
  color: var(--text-muted);
}
.admin-subnav__extra {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

@media (max-width: 640px) {
  .admin-subnav {
    gap: 8px;
    margin-bottom: 12px;
  }

  .admin-subnav__main,
  .admin-subnav__extra {
    width: 100%;
  }

  .admin-subnav__extra {
    justify-content: flex-start;
    flex-wrap: wrap;
  }
}
</style>
