<template>
  <Toast />
  <div id="app-layout">
    <header class="app-header">
      <div class="header-content">
        <div class="logo">
          <i class="pi pi-database"></i>
          <span>myCRM</span>
        </div>
        
        <button class="hamburger" @click="toggleMobileMenu" :class="{ active: mobileMenuOpen }">
          <span></span>
          <span></span>
          <span></span>
        </button>
        
        <nav :class="{ open: mobileMenuOpen }">
          <RouterLink to="/" @click="closeMobileMenu">
            <i class="pi pi-home"></i> Dashboard
          </RouterLink>
          <RouterLink to="/contacts" @click="closeMobileMenu">
            <i class="pi pi-users"></i> Kontakte
          </RouterLink>
          <RouterLink to="/companies" @click="closeMobileMenu">
            <i class="pi pi-building"></i> Unternehmen
          </RouterLink>
          <RouterLink to="/deals" @click="closeMobileMenu">
            <i class="pi pi-chart-line"></i> Deals
          </RouterLink>
          <RouterLink to="/users" v-if="authStore.isAdmin" @click="closeMobileMenu">
            <i class="pi pi-user-edit"></i> Benutzerverwaltung
          </RouterLink>
        </nav>
        
        <div class="user-info" v-if="authStore.isAuthenticated">
          <span>{{ authStore.fullName }}</span>
          <a href="/logout" class="logout-link">
            <i class="pi pi-sign-out"></i> Logout
          </a>
        </div>
      </div>
    </header>
    
    <main class="app-main">
      <RouterView />
    </main>
    
    <footer class="app-footer">
      <p>&copy; {{ new Date().getFullYear() }} myCRM</p>
    </footer>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { RouterLink, RouterView } from 'vue-router';
import Toast from 'primevue/toast';
import { useAuthStore } from './stores/auth';

const authStore = useAuthStore();
authStore.initializeFromElement(document.getElementById('app'));

const mobileMenuOpen = ref(false);

const toggleMobileMenu = () => {
  mobileMenuOpen.value = !mobileMenuOpen.value;
};

const closeMobileMenu = () => {
  mobileMenuOpen.value = false;
};
</script>

<style scoped lang="scss">
#app-layout {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.app-header {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  
  .header-content {
    max-width: 1400px;
    margin: 0 auto;
    padding: 1rem;
    display: flex;
    align-items: center;
    gap: 1rem;
    flex-wrap: wrap;
    position: relative;
    
    @media (min-width: 768px) {
      padding: 1rem 2rem;
      gap: 2rem;
      flex-wrap: nowrap;
    }
  }
  
  .logo {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 1.25rem;
    font-weight: 700;
    z-index: 1001;
    
    i {
      font-size: 1.5rem;
    }
    
    @media (min-width: 768px) {
      font-size: 1.5rem;
      
      i {
        font-size: 1.8rem;
      }
    }
  }
  
  .hamburger {
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    width: 2rem;
    height: 2rem;
    background: transparent;
    border: none;
    cursor: pointer;
    padding: 0;
    z-index: 1001;
    order: 2;
    margin-left: auto;
    
    @media (min-width: 768px) {
      display: none;
    }
    
    span {
      width: 2rem;
      height: 0.2rem;
      background: white;
      border-radius: 10px;
      transition: all 0.3s linear;
      position: relative;
      transform-origin: 1px;
      
      &:first-child {
        transform: rotate(0);
      }
      
      &:nth-child(2) {
        opacity: 1;
        transform: translateX(0);
      }
      
      &:nth-child(3) {
        transform: rotate(0);
      }
    }
    
    &.active {
      span:first-child {
        transform: rotate(45deg);
      }
      
      span:nth-child(2) {
        opacity: 0;
        transform: translateX(20px);
      }
      
      span:nth-child(3) {
        transform: rotate(-45deg);
      }
    }
  }
  
  nav {
    display: none;
    position: fixed;
    top: 0;
    right: 0;
    height: 100vh;
    width: 70%;
    max-width: 300px;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    flex-direction: column;
    padding: 5rem 1.5rem 2rem;
    gap: 0.5rem;
    transform: translateX(100%);
    transition: transform 0.3s ease-in-out;
    box-shadow: -2px 0 10px rgba(0, 0, 0, 0.2);
    z-index: 1000;
    
    &.open {
      display: flex;
      transform: translateX(0);
    }
    
    @media (min-width: 768px) {
      display: flex;
      position: static;
      height: auto;
      width: auto;
      max-width: none;
      flex-direction: row;
      padding: 0;
      gap: 0.5rem;
      flex: 1;
      transform: none;
      box-shadow: none;
      background: transparent;
      order: 1;
    }
    
    a {
      color: white;
      text-decoration: none;
      padding: 0.75rem 1rem;
      border-radius: 8px;
      transition: all 0.2s;
      display: flex;
      align-items: center;
      gap: 0.5rem;
      font-size: 1rem;
      white-space: nowrap;
      
      @media (min-width: 768px) {
        padding: 0.6rem 1.2rem;
        font-size: 1rem;
      }
      
      i {
        font-size: 1.1rem;
      }
      
      &:hover {
        background: rgba(255, 255, 255, 0.15);
      }
      
      &.router-link-active {
        background: rgba(255, 255, 255, 0.25);
        font-weight: 600;
      }
    }
  }
  
  .user-info {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin-left: auto;
    order: 3;
    
    @media (min-width: 768px) {
      gap: 1rem;
      order: 2;
    }
    
    span {
      font-weight: 500;
      font-size: 0.9rem;
      display: none;
      
      @media (min-width: 768px) {
        display: inline;
        font-size: 1rem;
      }
    }
    
    .logout-link {
      color: white;
      text-decoration: none;
      padding: 0.5rem 0.8rem;
      border-radius: 6px;
      background: rgba(255, 255, 255, 0.1);
      transition: all 0.2s;
      display: flex;
      align-items: center;
      gap: 0.4rem;
      font-size: 0.9rem;
      
      @media (min-width: 768px) {
        padding: 0.5rem 1rem;
        gap: 0.5rem;
        font-size: 1rem;
      }
      
      &:hover {
        background: rgba(255, 255, 255, 0.2);
      }
    }
  }
}

.app-main {
  flex: 1;
  padding: 1rem;
  max-width: 1400px;
  width: 100%;
  margin: 0 auto;
  box-sizing: border-box;
  
  @media (min-width: 768px) {
    padding: 2rem;
  }
}

.app-footer {
  background: #f8f9fa;
  padding: 0.75rem 1rem;
  text-align: center;
  color: #6c757d;
  border-top: 1px solid #dee2e6;
  font-size: 0.875rem;
  
  @media (min-width: 768px) {
    padding: 1rem 2rem;
    font-size: 1rem;
  }
  
  p {
    margin: 0;
  }
}
</style>
