<template>
  <a-layout-header class="modern-header">
    <div class="header-container">

      <div class="university-logo" @click="goTo('inicio')" role="button" tabindex="0">
        <div class="logo-icon">
          <img src="/logotiny.png" alt="Logo UNA" />
        </div>
        <div class="logo-text">
          <h1>Universidad Nacional del Altiplano</h1>
          <span>Dirección de Admisión</span>
        </div>
      </div>

      <nav class="modern-nav desktop-only">
        <a-menu
          v-model:selectedKeys="selectedKeys"
          mode="horizontal"
          class="nav-menu-modern"
          :items="navItems"
          @click="handleMenuClick"
        />
      </nav>

      <!-- <div class="right-actions desktop-only">
        <router-link
          v-if="!authStore.isAuthenticated"
          to="/login-postulante"
          class="auth-link"
        >
          <a-button type="primary">
            <template #icon><UserOutlined /></template>
            Portal del Postulante
          </a-button>
        </router-link>

        <router-link v-else to="/portal-postulante" class="auth-link">
          <a-button type="primary" ghost>
            <template #icon><DashboardOutlined /></template>
            Mi Portal
          </a-button>
        </router-link>
      </div> -->

      <a-button class="mobile-menu-btn mobile-only" type="text" @click="drawerOpen = true">
        ☰
      </a-button>
    </div>

    <a-drawer
      title="Menú"
      placement="right"
      :open="drawerOpen && isMobile"
      @close="handleDrawerClose"
      :width="300"
      :bodyStyle="{ padding: 0 }"
      :headerStyle="{ borderBottom: '1px solid #f0f0f0', fontFamily: 'Times New Roman' }"
      :closable="true"
      :maskClosable="true"
    >
      <div class="drawer-content">
        <div class="drawer-header">
          <div class="drawer-logo">
            <div class="logo-icon">
              <img src="/logotiny.png" alt="Logo UNA" />
            </div>
            <div class="drawer-logo-text">
              <h3>UNA</h3>
              <span>Dirección de Admisión</span>
            </div>
          </div>
        </div>

        <a-menu
          v-model:selectedKeys="selectedKeys"
          mode="inline"
          :items="mobileNavItems"
          class="drawer-menu"
          @click="handleDrawerMenuClick"
          @openChange="handleMobileOpenChange"
          :openKeys="mobileOpenKeys"
        />

        <!-- <div class="drawer-auth">
          <router-link
            v-if="!authStore.isAuthenticated"
            to="/login-postulante"
            class="auth-link"
            @click="handleDrawerClose"
          >
            <a-button type="primary" block>
              <template #icon><UserOutlined /></template>
              Portal del Postulante
            </a-button>
          </router-link>

          <router-link
            v-else
            to="/portal-postulante"
            class="auth-link"
            @click="handleDrawerClose"
          >
            <a-button type="primary" ghost block>
              <template #icon><DashboardOutlined /></template>
              Mi Portal
            </a-button>
          </router-link>
        </div> -->
      </div>
    </a-drawer>
  </a-layout-header>
</template>

<script setup>
import { ref, computed, watch, onMounted, onUnmounted } from "vue"
import { useRouter } from "vue-router"
import { useAuthStore } from "../store/postulanteStore"
import { UserOutlined, DashboardOutlined } from "@ant-design/icons-vue"

const router = useRouter()
const authStore = useAuthStore()

const drawerOpen = ref(false)
const selectedKeys = ref(["inicio"])
const isMobile = ref(false)
const mobileOpenKeys = ref([])

const checkScreenSize = () => {
  isMobile.value = window.innerWidth < 768
  if (!isMobile.value && drawerOpen.value) {
    drawerOpen.value = false
    mobileOpenKeys.value = []
  }
}

const navItems = computed(() => [
  { key: "inicio", label: "Inicio" },
  {
    key: "programas",
    label: "Programas",
    children: [
      { key: "ingenierias", label: "Ingenierías" },
      { key: "biomedicas", label: "Biomédicas" },
      { key: "sociales", label: "Sociales" },
    ],
  },

  {
    key: "modalidades",
    label: "Modalidades",
    children: [
      { key: "cepreuna", label: "Cepreuna" },
      { key: "extraordinario", label: "Extraordinario" },
      { key: "general", label: "General" },
    ],
  },
  { key: "resultados", label: "Resultados" },
])

const mobileNavItems = computed(() => navItems.value)

watch(drawerOpen, (open) => {
  if (!open) mobileOpenKeys.value = []
})

const routesByKey = {
  inicio: "/",
  programas: "",
  ingenierias: "",
  biomedicas: "",
  sociales: "",
  modalidades: "/modalidades",
  cepreuna: "/modalidades/cepreuna",
  extraordinario: "/modalidades/extraordinario",
  general: "/modalidades/general",
  resultados: "/resultados",
}

const goTo = (key) => {
  selectedKeys.value = [key]
  const path = routesByKey[key]
  if (path) router.push(path)
}

const handleMenuClick = ({ key }) => {
  goTo(key)
}

const handleDrawerMenuClick = ({ key }) => {
  const isParent = navItems.value.some((i) => i.key === key && i.children)
  if (isParent) return

  goTo(key)
  if (isMobile.value) handleDrawerClose()
}

const handleMobileOpenChange = (keys) => {
  // Solo 1 submenu abierto en móvil
  const latest = keys.find((k) => !mobileOpenKeys.value.includes(k))
  mobileOpenKeys.value = latest ? [latest] : []
}

const handleDrawerClose = () => {
  mobileOpenKeys.value = []
  drawerOpen.value = false
}

onMounted(() => {
  checkScreenSize()
  window.addEventListener("resize", checkScreenSize)
})

onUnmounted(() => {
  window.removeEventListener("resize", checkScreenSize)
})
</script>

<style scoped>

:host {
  --primary-color: #1e3a8a;
  --secondary-color: #374151;
  --border-color: #d1d5db;
  --bg-color: #ffffff;
}

.modern-header,
.nav-menu-modern,
.logo-text h1,
.logo-text span,
.drawer-logo-text h3,
.drawer-logo-text span {
  font-family: "Lora", serif;
}

.modern-header {
  background: rgba(255, 255, 255, 0.92) !important;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border-color);
  padding: 0 24px !important;
  height: 82px !important;
  line-height: 82px !important;
  position: sticky;
  top: 0;
  z-index: 1000;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.06);
}

.header-container {
  max-width: 1320px;
  margin: 0 auto;
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 16px;
  height: 100%;
  width: 100%;
}

.university-logo {
  display: flex;
  align-items: center;
  gap: 14px;
  cursor: pointer;
  user-select: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
}
.university-logo:hover {
  opacity: 0.95;
  transform: translateY(-1px);
}
.logo-icon {
  width: 46px;
  height: 46px;
  border: 1px solid var(--border-color);
  border-radius: 8px;
  background: var(--bg-color);
  display: grid;
  place-items: center;
  flex-shrink: 0;
}
.logo-icon img {
  width: 90%;
  height: 90%;
  object-fit: contain;
}
.logo-text {
  display: flex;
  flex-direction: column;
  line-height: 1.2;
}
.logo-text h1 {
  margin: 0;
  font-size: 1.02rem;
  font-weight: 700;
  color: var(--primary-color);
}
.logo-text span {
  font-size: 0.82rem;
  color: var(--secondary-color);
}

.modern-nav {
  display: flex;
  justify-content: center;
  min-width: 0;
}
.nav-menu-modern {
  width: 100%;
  background: transparent !important;
  border-bottom: none !important;
  justify-content: center;
}
.nav-menu-modern :deep(.ant-menu-item),
.nav-menu-modern :deep(.ant-menu-submenu) {
  height: 82px;
  line-height: 82px;
  padding: 0 14px;
  font-size: 0.95rem;
  color: var(--secondary-color);
  transition: color 0.2s ease;
}
.nav-menu-modern :deep(.ant-menu-item:hover),
.nav-menu-modern :deep(.ant-menu-submenu:hover) {
  color: var(--primary-color);
}
.nav-menu-modern :deep(.ant-menu-item-selected),
.nav-menu-modern :deep(.ant-menu-submenu-selected) {
  color: var(--primary-color);
  font-weight: 600;
}
.nav-menu-modern :deep(.ant-menu-item-selected::after),
.nav-menu-modern :deep(.ant-menu-submenu-selected::after) {
  border-bottom: 3px solid var(--primary-color) !important;
}

.right-actions {
  display: flex;
  justify-content: flex-end;
}
.auth-link {
  text-decoration: none;
}

.mobile-menu-btn {
  font-size: 24px;
  color: #111827;
  padding: 8px;
  border-radius: 8px;
  transition: background-color 0.2s ease;
}
.mobile-menu-btn:hover {
  background: rgba(0, 0, 0, 0.04);
}

.drawer-content {
  display: flex;
  flex-direction: column;
  height: 100%;
}
.drawer-header {
  padding: 18px 20px;
  border-bottom: 1px solid var(--border-color);
  background: #fafafa;
}
.drawer-logo {
  display: flex;
  align-items: center;
  gap: 12px;
}
.drawer-logo-text {
  display: flex;
  flex-direction: column;
  line-height: 1.2;
}
.drawer-logo-text h3 {
  margin: 0;
  color: var(--primary-color);
  font-size: 1rem;
}
.drawer-logo-text span {
  font-size: 12px;
  color: var(--secondary-color);
}

.drawer-menu {
  border-right: none !important;
}
.drawer-menu :deep(.ant-menu-item),
.drawer-menu :deep(.ant-menu-submenu-title) {
  font-size: 15px;
  padding-left: 20px !important;
  height: 48px;
  line-height: 48px;
  margin: 2px 0;
}
.drawer-menu :deep(.ant-menu-item:hover),
.drawer-menu :deep(.ant-menu-submenu-title:hover) {
  background-color: #f0f7ff;
}
.drawer-menu :deep(.ant-menu-item-selected) {
  background-color: #f0f7ff;
  color: var(--primary-color);
  font-weight: 600;
}
.drawer-menu :deep(.ant-menu-item-selected::after) {
  border-right: 3px solid var(--primary-color);
}
.drawer-menu :deep(.ant-menu-submenu .ant-menu-item) {
  padding-left: 42px !important;
  font-size: 14px;
}

.drawer-auth {
  margin-top: auto;
  padding: 16px 20px;
  border-top: 1px solid var(--border-color);
  background: #fff;
}

.desktop-only {
  display: flex;
}
.mobile-only {
  display: none;
}

@media (max-width: 992px) {
  .modern-header {
    padding: 0 16px !important;
  }
  .logo-text h1 {
    font-size: 14px !important;
  }
  .logo-text span {
    font-size: 12px !important;
  }
  .nav-menu-modern :deep(.ant-menu-item),
  .nav-menu-modern :deep(.ant-menu-submenu) {
    padding: 0 10px;
    font-size: 0.92rem;
  }
}

@media (max-width: 768px) {
  .header-container {
    grid-template-columns: auto 1fr auto;
    gap: 10px;
  }
  .desktop-only {
    display: none;
  }
  .mobile-only {
    display: inline-flex;
  }
  .modern-header {
    height: 72px !important;
    line-height: 72px !important;
  }
  .logo-icon {
    width: 40px;
    height: 40px;
  }
  .logo-text h1 {
    font-size: 13px !important;
    line-height: 1.1;
  }
  .logo-text span {
    font-size: 11px !important;
  }
}

@media (max-width: 480px) {
  .modern-header {
    padding: 0 12px !important;
  }
  .logo-text span {
    display: none;
  }
  .mobile-menu-btn {
    font-size: 20px;
    padding: 6px;
  }
}
</style>