<template>
  <v-container fluid>
    <v-row>
      <!-- Bloc événements -->
      <v-col cols="12" md="8">
        <v-card>
          <v-tabs v-model="tab">
            <v-tab value="future">Mes événements à venir</v-tab>
            <v-tab value="past">Mes événements passés</v-tab>
          </v-tabs>

          <v-tabs-window v-model="tab">
            <v-tabs-window-item value="future">
              <UiEventList
                :events="upcomingEvents"
                @load-next="loadNextUpcoming"
                @load-prev="loadPrevUpcoming"
                @edit="editEvent"
                @delete="deleteEvent"
              />
            </v-tabs-window-item>
            <v-tabs-window-item value="past">
              <UiEventList
                :events="pastEvents"
                @load-next="loadNextPast"
                @load-prev="loadPrevPast"
                @edit="editEvent"
                @delete="deleteEvent"
              />
            </v-tabs-window-item>
          </v-tabs-window>

          <v-card-actions>
            <v-btn color="primary" to="events/new">Ajouter un événement</v-btn>
          </v-card-actions>
        </v-card>
      </v-col>

      <!-- Bloc structure -->
      <v-col cols="12" md="4">
        <v-card>
          <v-card-title>Structure</v-card-title>
          <v-card-text>
            <div><strong>Nom:</strong> {{ organization?.name }}</div>
            <div><strong>Email:</strong> {{ organization?.email }}</div>
          </v-card-text>
          <v-card-actions>
            <v-btn color="secondary" to="organization">Modifier la structure</v-btn>
          </v-card-actions>
        </v-card>
      </v-col>
    </v-row>
  </v-container>
</template>

<script setup lang="ts">

import {useAp2iRequestService} from "~/composables/data/useAp2iRequestService";

definePageMeta({
  name: 'freemium_dashboard_page',
})

import { ref } from 'vue'
import {useEntityFetch} from "~/composables/data/useEntityFetch";
import Organization from "~/models/Freemium/Organization";
import type {AsyncData} from "#app";

const tab = ref(null)


const {apiRequestService, pending} = useAp2iRequestService()

const { fetch } = useEntityFetch()

const { data: organization } = fetch(
  Organization
) as AsyncData<Organization | null, Error | null>


// Pagination state
const upcomingPage = ref(1)
const pastPage = ref(1)

const upcomingEvents = ref<Event[]>([{
  'id': 1,
  'name': "Mon événement",
  'date': "2025/10/25",
  'location': "Cluses",
  'image': "https://www.ville-lecastellet.fr/wp-content/uploads/sites/2/2022/04/declaration-devenements.jpg"
},
  {
    'id': 2,
    'name': "Noel",
    'date': "2025/12/25",
    'location': "Thyez",
    'image': "https://www.maisonvictor.fr/img/cms/sapin%20de%20noel.jpg"
  }
  ])
const pastEvents = ref<Event[]>([{
  'id': 3,
  'name': "Musique en stock",
  'date': "2025/07/03",
  'location': "Scionzier",
  'image': "https://musiquesenstock.fr/wp-content/uploads/MES2025_AFF_H-1024x695.jpg"
}])

function loadNextUpcoming() {
  upcomingPage.value++
  fetchUpcoming()
}

function loadPrevUpcoming() {
  if (upcomingPage.value > 1) upcomingPage.value--
  fetchUpcoming()
}

function loadNextPast() {
  pastPage.value++
  fetchPast()
}

function loadPrevPast() {
  if (pastPage.value > 1) pastPage.value--
  fetchPast()
}

function fetchUpcoming() {
  // TODO: call API with page=upcomingPage
  console.log('fetch upcoming events page', upcomingPage.value)
}

function fetchPast() {
  // TODO: call API with page=pastPage
  console.log('fetch past events page', pastPage.value)
}

function editEvent(eventId: number) {
  // TODO: navigate to event edit page
  console.log('edit event', eventId)
}

function deleteEvent(eventId: number) {
  // TODO: call delete API then refresh
  console.log('delete event', eventId)
}

function addEvent() {
  // TODO: navigate to add event page
  console.log('add new event')
}

</script>

<style scoped>
.v-card {
  margin-bottom: 16px;
}
</style>
