<template>
  <div class="main">
    <button class="menu-toggle" @click="toggleMenu">
      {{ isMenuOpen ? 'Menü schließen' : 'Menü öffnen' }}
    </button>

    <div v-if="isAuthenticated" class="navigation" :class="{ 'menu-open': isMenuOpen }">
      <div class="club-selector">
        <div>
          Verein:
          <select v-model="selectedClub">
            <option value="">---</option>
            <option value="new">Neuer Verein</option>
            <option v-for="club in clubs" :key="club.id" :value="club.id">{{ club.name }}</option>
          </select>
          <button @click="loadClub">-&gt;</button>
        </div>
      </div>

      <div v-if="selectedClub" class="nav-links">
        <a href="/members">Mitglieder</a>
        <a href="/diary">Tagebuch</a>
        <a href="/pending-approvals">Freigaben</a>
        <a href="/schedule">Spielpläne</a>
      </div>

      <div class="logout-btn">
        <button @click="logout()">Ausloggen</button>
      </div>
    </div>

    <div v-else class="navigation">
      <a href="/login">Einloggen</a>
      <a href="/register">Registrieren</a>
    </div>

    <router-view class="content"></router-view>
  </div>
</template>

<script>
import { mapGetters, mapActions } from 'vuex';
import apiClient from './apiClient.js';

export default {
  name: 'App',
  data() {
    return {
      selectedClub: null,
      isMenuOpen: false,
      sessionInterval: null, 
    };
  },
  computed: {
    ...mapGetters(['isAuthenticated', 'currentClub', 'clubs']),
  },
  watch: {
    selectedClub(newVal) {
      this.setCurrentClub(newVal);
    },
    currentClub(newVal) {
      if (newVal === 'new') {
        this.$router.push('/createclub');
      } else if (newVal) {
        this.$router.push(`/showclub/${newVal}`);
      }
    },
  },
  methods: {
    ...mapActions(['setCurrentClub', 'setClubs', 'logout']),
    
    loadClub() {
      this.setCurrentClub(this.currentClub);
      this.$router.push(`/showclub/${this.currentClub}`);
    },

    toggleMenu() {
      this.isMenuOpen = !this.isMenuOpen;
    },

    async checkSession() {
      try {
        const response = await apiClient.get('/session/status');
        if (!response.data.valid) {
          this.handleLogout();
        }
      } catch (error) {
        console.error('Session check failed:', error);
        this.handleLogout();
      }
    },

    handleLogout() {
      alert('Deine Sitzung ist abgelaufen. Du wirst abgemeldet.');
      this.logout();
      clearInterval(this.sessionInterval);
      this.$router.push('/login');
    }
  },
  async mounted() {
    try {
      const response = await apiClient.get('/clubs');
      this.setClubs(response.data);
      if (this.currentClub) {
        this.selectedClub = this.currentClub;
      }
      this.checkSession();
      this.sessionInterval = setInterval(this.checkSession, 5000);
    } catch (error) {
      this.setClubs([]);
      this.selectedClub = null;
    }
  },
  beforeUnmount() {
    clearInterval(this.sessionInterval);
  }
};
</script>

<style scoped>
.main {
  display: flex;
  flex-direction: column;
  height: 100%;
  width: 100%;
  overflow: hidden;
}

.navigation {
  background-color: #e0f0e8;
  display: flex;
  flex-direction: column;
  padding: 0.5rem;
}

.club-selector,
.logout-btn {
  margin-bottom: 1rem;
}

.nav-links {
  display: flex;
  flex-direction: column;
  margin-bottom: 1rem;
}

.navigation>a {
  text-decoration: none;
  margin: 0.3em 0;
  color: #a07040;
}

.content {
  flex: 1;
  padding: 0.5em;
  overflow: auto;
}

button {
  padding: 0.3em 0.6em;
  background-color: #a07040;
  color: white;
  border: none;
  cursor: pointer;
}

button:hover {
  background-color: #804b29;
}

select {
  padding: 0.3em;
  border: 1px solid #ccc;
}

/* Menü-Toggle-Button nur auf kleinen Bildschirmen anzeigen */
.menu-toggle {
  display: none;
}

/* Responsive Design */
@media (min-width: 768px) {
  .main {
    flex-direction: row;
  }

  .navigation {
    width: 13em;
    padding: 1rem;
  }

  .content {
    padding: 1rem;
    height: calc(100% - 2.5rem);
  }

  .nav-links {
    flex-direction: column;
  }

  .nav-links>a {
    margin: 0 1em;
  }

  select {
    width: 100%;
  }
}

@media (max-width: 767px) {
  .main {
    flex-direction: column;
  }

  .navigation {
    display: none;
    flex-direction: column;
    width: 100%;
  }

  /* Das Menü anzeigen, wenn es geöffnet ist */
  .menu-open {
    display: flex;
  }

  .content {
    padding: 0.5rem;
  }

  .nav-links {
    display: block;
  }

  .nav-links>a {
    display: block;
    margin: 0.5em 0;
  }

  /* Menü-Toggle-Button nur auf kleinen Bildschirmen */
  .menu-toggle {
    display: block;
    background-color: #a07040;
    color: white;
    border: none;
    padding: 0.5em;
    margin: 0.5em;
    cursor: pointer;
  }

  .menu-toggle:hover {
    background-color: #804b29;
  }
}

@media (max-width: 480px) {
  .navigation {
    font-size: 1.5em;
  }

  select {
    width: 100%;
  }

  button {
    width: 100%;
    margin-top: 0.5em;
  }

  .nav-links>a {
    font-size: 0.85em;
    padding: 0.5em;
    border-bottom: 1px solid #ddd;
  }
}

</style>
