<script setup lang="ts">
definePageMeta({
  pageId: 'lists'
})
import type {List} from '~/types/lists'
const listStore = useListStore()

// activeTabId réactif
const activeTabId = ref<number | undefined>(undefined)

onMounted(async () => {
  if (!listStore.lists.length) {
    await listStore.fetchLists()
  }

  const defaultOpen = listStore.lists.find((l) => l.is_open === true)
  activeTabId.value = defaultOpen?.id ?? listStore.lists[0]?.id
})

// Fonction utilitaire pour parser le contenu chiffré
const parseContent = (list: List) => {
  try {
    return JSON.parse(list.encrypted_content) as any[]
  } catch {
    return []
  }
}
</script>

<template>
  <div v-if="!listStore.loading">
    <div v-for="list in listStore.lists" :key="list.id">
      <h3>{{ list.list_title }}</h3>
      <ul>
        <li v-for="item in parseContent(list)" :key="item.id ?? item.name ?? item">
          {{ item.name ?? JSON.stringify(item) }}
        </li>
      </ul>
    </div>
  </div>
  <div v-else>
    <UiLoading/>
  </div>
</template>

<style>
h3{
  font-size: 1.2em;
  color:blueviolet
}
</style>