<template>
  <v-container fluid class="py-6">
    <Teleport to="#fc-nav-actions">
      <v-btn
        prepend-icon="mdi-shuffle-variant" variant="tonal" color="accent"
        size="small" :loading="store.loading" @click="store.shuffle()"
      >Shuffle</v-btn>
    </Teleport>

    <v-alert v-if="store.error" type="error" variant="tonal" closable class="my-4">
      Failed to load: {{ store.error }}
    </v-alert>
    <v-alert v-else-if="store.isEmpty" type="info" variant="tonal" class="my-4">
      No images yet.
    </v-alert>

    <MasonryGrid
      :items="store.images"
      :loading="store.loading"
      :has-more="store.hasMore"
      @load-more="store.fetchPage()"
      @open="openImage"
    />
  </v-container>
</template>

<script setup>
import { onMounted } from 'vue'
import { useShowcaseStore } from '../stores/showcase.js'
import { useModalStore } from '../stores/modal.js'
import MasonryGrid from '../components/discovery/MasonryGrid.vue'

const store = useShowcaseStore()
const modal = useModalStore()

onMounted(() => { if (store.images.length === 0) store.fetchPage() })

function openImage(id) {
  modal.open(id)
}
</script>
