<template>
  <div class="nav-tabs-container">
    <div class="nav-tabs">
      <button 
        :class="['nav-tab', { active: activeSection === 'items' }]" 
        @click="$emit('section-change', 'items')"
      >
        Tuotteet
      </button>
      <button 
        :class="['nav-tab', { active: activeSection === 'clients' }]" 
        @click="$emit('section-change', 'clients')"
      >
        Asiakkaat
      </button>
      <button 
        :class="['nav-tab', { active: activeSection === 'projects' }]" 
        @click="$emit('section-change', 'projects')"
      >
        Projektit
      </button>
    
    <!-- Tasks Tab -->
    <button 
      :class="['nav-tab', { active: activeSection === 'tasks' }]" 
      @click="$emit('section-change', 'tasks')"
    >
      Tehtävät
    </button>
    </div>
    
    <!-- Talous Dropdown Menu -->
    <div class="nav-dropdown" @click="toggleDropdown">
      <button 
        :class="['nav-tab', { active: isAccountingSection }]"
      >
        Talous
        <span class="dropdown-arrow">▼</span>
      </button>
      <div v-if="dropdownOpen" class="dropdown-menu">
        <button 
          :class="['dropdown-item', { active: activeSection === 'accounting' }]"
          @click.stop="$emit('section-change', 'accounting')"
        >
          Yksinkertainen Kirjanpito
        </button>
        <button 
          :class="['dropdown-item', { active: activeSection === 'tuloslaskelma' }]"
          @click.stop="$emit('section-change', 'tuloslaskelma')"
        >
          Tuloslaskelma
        </button>
        <button 
          :class="['dropdown-item', { active: activeSection === 'alv-laskenta' }]"
          @click.stop="$emit('section-change', 'alv-laskenta')"
        >
          ALV-laskenta
        </button>
      </div>
    </div>
    
    <!-- User Management (Admin Only) -->
    <button 
      v-if="currentUser && currentUser.role === 'admin'"
      :class="['nav-tab', { active: activeSection === 'user-management' }]" 
      @click="$emit('section-change', 'user-management')"
    >
      Käyttäjät
    </button>
    
    <!-- Profile Management (All Users) -->
    <button 
      v-if="currentUser"
      :class="['nav-tab', { active: activeSection === 'profile' }]" 
      @click="$emit('section-change', 'profile')"
    >
      Profiili
    </button>
    
    <!-- Timer Component -->
    <div class="timer-section">
      <HeaderTimer @navigate-to-tasks="$emit('section-change', 'tasks')" />
    </div>
    
    <!-- Logout Button -->
    <div class="logout-section" v-if="currentUser">
      <button class="logout-btn" @click="$emit('logout')">
        <i class="fas fa-sign-out-alt"></i>
        Kirjaudu ulos
      </button>
    </div>
  </div>
</template>

<script>
import HeaderTimer from '../HeaderTimer.vue'

export default {
  name: 'NavigationTabs',
  components: {
    HeaderTimer
  },
  props: {
    activeSection: {
      type: String,
      required: true,
      validator: (value) => ['items', 'clients', 'projects', 'accounting', 'tuloslaskelma', 'alv-laskenta', 'tasks', 'user-management', 'profile'].includes(value)
    },
    currentUser: {
      type: Object,
      default: null
    }
  },
  emits: ['section-change', 'logout'],
  data() {
    return {
      dropdownOpen: false
    }
  },
  computed: {
    isAccountingSection() {
      return ['accounting', 'tuloslaskelma', 'alv-laskenta'].includes(this.activeSection)
    }
  },
  methods: {
    toggleDropdown(event) {
      event.preventDefault()
      event.stopPropagation()
      this.dropdownOpen = !this.dropdownOpen
      console.log('Dropdown toggled:', this.dropdownOpen)
    },
    closeDropdown() {
      this.dropdownOpen = false
    }
  },
  mounted() {
    // Close dropdown when clicking outside
    document.addEventListener('click', this.closeDropdown)
  },
  beforeUnmount() {
    document.removeEventListener('click', this.closeDropdown)
  }
}
</script>

<style scoped>
.nav-tabs-container {
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 2px solid #e9ecef;
  margin-bottom: 20px;
}

.nav-tabs {
  display: flex;
  align-items: center;
}

.timer-section {
  display: flex;
  align-items: center;
  margin-left: 20px;
}

.nav-tab {
  background: none;
  border: none;
  padding: 12px 24px;
  cursor: pointer;
  font-size: 16px;
  color: #6c757d;
  border-bottom: 2px solid transparent;
  transition: all 0.3s ease;
}

.nav-tab:hover {
  color: #495057;
}

.nav-tab.active {
  color: #007bff;
  border-bottom-color: #007bff;
}

.nav-dropdown {
  position: relative;
  display: inline-block;
  z-index: 100;
}

.nav-dropdown .nav-tab {
  display: flex;
  align-items: center;
  gap: 5px;
}

.dropdown-arrow {
  font-size: 12px;
  transition: transform 0.3s ease;
  margin-left: 5px;
  color: #007bff;
}

.nav-dropdown:hover .dropdown-arrow {
  transform: rotate(180deg);
}

.dropdown-menu {
  position: absolute;
  top: 100%;
  left: 0;
  background: white;
  border: 2px solid #007bff;
  border-radius: 4px;
  box-shadow: 0 4px 8px rgba(0,0,0,0.2);
  z-index: 9999;
  min-width: 200px;
  margin-top: 2px;
}

.dropdown-item {
  display: block;
  width: 100%;
  padding: 10px 20px;
  background: none;
  border: none;
  text-align: left;
  cursor: pointer;
  font-size: 14px;
  color: #333;
  transition: background-color 0.2s ease;
}

.dropdown-item:hover {
  background-color: #f8f9fa;
}

.dropdown-item.active {
  background-color: #007bff;
  color: white;
}

.dropdown-item:first-child {
  border-radius: 4px 4px 0 0;
}

.dropdown-item:last-child {
  border-radius: 0 0 4px 4px;
}

.logout-section {
  display: flex;
  align-items: center;
  margin-left: auto;
  padding-left: 20px;
}

.logout-btn {
  background: #dc3545;
  color: white;
  border: none;
  padding: 8px 16px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  display: flex;
  align-items: center;
  gap: 8px;
  transition: all 0.3s ease;
}

.logout-btn:hover {
  background: #c82333;
  transform: translateY(-1px);
}

.logout-btn i {
  font-size: 16px;
}
</style>
