<script setup>

  const events = await queryContent('event')
    .sort({ date: -1 })
    .only(['title', 'slug', 'location', 'date', 'time', 'meetup'])
    .limit(3)
    .find()

  import { format, isBefore, parseISO } from "date-fns";

  for (let i = 0; i < events.length; i++) {
    let date = parseISO(events[i].date)
    events[i].past = isBefore(date, new Date())
    events[i].date = format(date, 'EEE, MMM d')
  } 
  
</script>

<template>
  <div class="events" id="events">
    <h4>Upcoming Events</h4>
    <div v-bind:class="(event.past)?'event past':'event'" v-for="event in events" :key="event.slug">
      <div class="info">
        <NuxtLink :to="'/event/' + event.slug"><h5>{{ event.title }}</h5></NuxtLink>
        <p class="location">{{ event.location }}</p>
        <p>{{ event.date }} · {{ event.time }}</p>
      </div>
      <NuxtLink v-bind:class="(event.meetup)?'attend both':'attend bottom'" class="attend" v-if="!event.past" :href="'/login?action=attend&?ref=' + event.slug" >Attend event</NuxtLink>
      <NuxtLink class="meetup bottom" v-if="event.meetup" target="_BLANK"
        :to="'https://www.meetup.com/capitalregionbitcoinnetwork/events/' + event.meetup + '/'">Meetup.com</NuxtLink>
    </div>
  </div>
</template>