<template>
  <header class="header" :class="{ mobile: isMobile }">
    <!-- Десктоп версия -->
    <template v-if="!isMobile">
      <div class="header-left">
        <div class="title-row">
          <h1 class="page-title">{{ title }}</h1>
          <slot name="filters"></slot>
        </div>
        <p v-if="subtitle" class="page-subtitle">{{ subtitle }}</p>
      </div>
      <div class="header-right">
        <slot name="stats"></slot>
        <button class="logout-btn" @click="logout" title="Выйти">
          <i data-lucide="log-out"></i>
        </button>
      </div>
    </template>

    <!-- Мобильная версия -->
    <template v-else>
      <!-- Компактная строка: заголовок слева + иконки-кнопки -->
      <div class="mobile-header-row">
        <h1 v-if="!$slots['mobile-filters']" class="mobile-title">{{ title }}</h1>
        <slot name="mobile-filters"></slot>
        <button class="mobile-logout-btn" @click="logout" title="Выйти">
          <i data-lucide="log-out"></i>
        </button>
      </div>
    </template>
  </header>
</template>

<script setup>
import { onMounted, onUpdated } from 'vue'
import { useRouter } from 'vue-router'
import { authApi } from '../api'
import { useMobile } from '../composables/useMobile'
import { clearAuthCache } from '../router'

const { isMobile } = useMobile()

defineProps({
  title: {
    type: String,
    required: true
  },
  subtitle: {
    type: String,
    default: ''
  }
})

const router = useRouter()

const logout = async () => {
  clearAuthCache()
  await authApi.logout()
  router.push('/login')
}

const refreshIcons = () => {
  if (window.lucide) {
    window.lucide.createIcons()
  }
}

onMounted(refreshIcons)
onUpdated(refreshIcons)
</script>

<style scoped>
.header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  padding: 24px 32px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  gap: 24px;
}

.header-left {
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-width: 0;
  flex: 1;
}

.title-row {
  display: flex;
  align-items: center;
  gap: 24px;
  min-width: 0;
}

.page-title {
  font-size: 24px;
  font-weight: 600;
  color: var(--text);
  margin: 0;
  white-space: nowrap;
}

.page-subtitle {
  color: var(--text-secondary);
  font-size: 14px;
  margin: 0;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 20px;
  flex-shrink: 0;
}

.logout-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--text-secondary);
  cursor: pointer;
  transition: all 0.15s;
}

.logout-btn:hover {
  background: rgba(239, 68, 68, 0.15);
  border-color: rgba(239, 68, 68, 0.3);
  color: #ef4444;
}

.logout-btn i {
  width: 18px;
  height: 18px;
}

/* ========== MOBILE ========== */
.header.mobile {
  padding: 10px 16px;
  flex-direction: row;
  border-bottom: none;
}

.mobile-header-row {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
}

.mobile-title {
  font-size: 18px;
  font-weight: 600;
  color: var(--text);
  margin: 0;
  white-space: nowrap;
}

.mobile-logout-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 10px;
  color: var(--text-muted);
  cursor: pointer;
  transition: all 0.15s;
  margin-left: auto;
}

.mobile-logout-btn:hover,
.mobile-logout-btn:active {
  background: rgba(239, 68, 68, 0.15);
  border-color: rgba(239, 68, 68, 0.3);
  color: #ef4444;
}

.mobile-logout-btn i {
  width: 18px;
  height: 18px;
}
</style>
