<template>
  <div class="p-6">
    <h2 class="kestrel-page-heading mb-2">
      POI placement
    </h2>
    <p
      v-if="!canEditPois"
      class="mb-4 text-sm text-kestrel-muted"
    >
      View-only. Sign in as admin or leader to add or edit POIs.
    </p>
    <template v-else>
      <form
        class="mb-6 flex flex-wrap items-end gap-3 rounded border border-kestrel-border bg-kestrel-surface p-4"
        @submit.prevent="onAdd"
      >
        <div>
          <label
            for="poi-lat"
            class="kestrel-label"
          >Lat</label>
          <input
            id="poi-lat"
            v-model.number="form.lat"
            type="number"
            step="any"
            required
            class="kestrel-input w-28"
          >
        </div>
        <div>
          <label
            for="poi-lng"
            class="kestrel-label"
          >Lng</label>
          <input
            id="poi-lng"
            v-model.number="form.lng"
            type="number"
            step="any"
            required
            class="kestrel-input w-28"
          >
        </div>
        <div>
          <label
            for="poi-label"
            class="kestrel-label"
          >Label</label>
          <input
            id="poi-label"
            v-model="form.label"
            type="text"
            class="kestrel-input w-40"
          >
        </div>
        <div>
          <label
            for="poi-icon"
            class="kestrel-label"
          >Icon</label>
          <select
            id="poi-icon"
            v-model="form.iconType"
            class="kestrel-input w-28"
          >
            <option
              v-for="opt in POI_ICON_TYPES"
              :key="opt"
              :value="opt"
            >
              {{ opt }}
            </option>
          </select>
        </div>
        <button
          type="submit"
          class="rounded bg-kestrel-accent px-3 py-1.5 text-sm font-medium text-kestrel-bg hover:opacity-90"
        >
          Add POI
        </button>
      </form>
    </template>
    <div class="overflow-x-auto rounded border border-kestrel-border">
      <table class="w-full text-left text-sm">
        <thead>
          <tr class="border-b border-kestrel-border bg-kestrel-surface-hover">
            <th class="px-4 py-2 font-medium text-kestrel-text">
              Label
            </th>
            <th class="px-4 py-2 font-medium text-kestrel-text">
              Lat
            </th>
            <th class="px-4 py-2 font-medium text-kestrel-text">
              Lng
            </th>
            <th class="px-4 py-2 font-medium text-kestrel-text">
              Icon
            </th>
            <th
              v-if="canEditPois"
              class="px-4 py-2 font-medium text-kestrel-text"
            >
              Actions
            </th>
          </tr>
        </thead>
        <tbody>
          <tr
            v-for="p in poisList"
            :key="p.id"
            class="border-b border-kestrel-border"
          >
            <td class="px-4 py-2 text-kestrel-text">
              {{ p.label || '-' }}
            </td>
            <td class="px-4 py-2 text-kestrel-muted">
              {{ p.lat }}
            </td>
            <td class="px-4 py-2 text-kestrel-muted">
              {{ p.lng }}
            </td>
            <td class="px-4 py-2 text-kestrel-muted">
              {{ p.icon_type }}
            </td>
            <td
              v-if="canEditPois"
              class="px-4 py-2"
            >
              <button
                type="button"
                class="text-xs text-red-400 hover:underline"
                @click="remove(p.id)"
              >
                Delete
              </button>
            </td>
          </tr>
        </tbody>
      </table>
    </div>
  </div>
</template>

<script setup>
const POI_ICON_TYPES = Object.freeze(['pin', 'flag', 'waypoint'])

const { data: poisData, refresh } = usePois()
const { canEditPois } = useUser()
const poisList = computed(() => poisData.value ?? [])

const form = ref({ lat: 37.77, lng: -122.42, label: '', iconType: 'pin' })

async function onAdd() {
  const { lat, lng, label, iconType } = form.value
  try {
    await $fetch('/api/pois', { method: 'POST', body: { lat, lng, label, iconType } })
    await refresh()
  }
  catch { /* ignore */ }
}

async function remove(id) {
  try {
    await $fetch(`/api/pois/${id}`, { method: 'DELETE' })
    await refresh()
  }
  catch { /* ignore */ }
}
</script>
