<template>
  <header class="header" :class="{ mobile: isMobile }">
    <!-- Десктоп версия -->
    <template v-if="!isMobile">
      <div v-if="title || $slots.filters" class="header-left">
        <div class="title-row">
          <h1 v-if="title" 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="actions"></slot>
        <slot name="stats"></slot>
        <LogoutButton />
      </div>
    </template>

    <!-- Мобильная версия -->
    <template v-else>
      <!-- Компактная строка: заголовок слева + иконки-кнопки -->
      <div class="mobile-header-row">
        <h1 v-if="title && !$slots['mobile-filters']" class="mobile-title">{{ title }}</h1>
        <slot name="mobile-filters"></slot>
        <slot name="actions"></slot>
        <LogoutButton :mobile="true" />
      </div>
    </template>
  </header>
</template>

<script setup>
import { onMounted, onUpdated } from 'vue'
import { useMobile } from '../composables/useMobile'
import LogoutButton from './ui/LogoutButton.vue'

const { isMobile } = useMobile()

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

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;
}

/* ========== 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;
}
</style>
