<script setup>
definePageMeta({
  middleware: "auth"
})

defineShortcuts({
  '/': () => {
    //console.log(searchinput)
    //searchinput.value.focus()
    document.getElementById("searchinput").focus()
  },
  '+': () => {
    router.push("/chats/create")
  },
  'Enter': {
    usingInput: true,
    handler: () => {
      router.push(`/chats/show/${filteredRows.value[selectedItem.value].id}`)
    }
  },
  'arrowdown': () => {
    if(selectedItem.value < filteredRows.value.length - 1) {
      selectedItem.value += 1
    } else {
      selectedItem.value = 0
    }
  },
  'arrowup': () => {
    if(selectedItem.value === 0) {
      selectedItem.value = filteredRows.value.length - 1
    } else {
      selectedItem.value -= 1
    }
  }
})

const router = useRouter()
const items = ref([])
const selectedItem = ref(0)

const setup = async () => {
  items.value = await useSupabaseSelect("chats","*, profiles(*)")
  //items.value = await useSupabaseSelect("chats","*, profiles(*), chatmessages(*)")
}

const templateColumns = [
  {
    key: "name",
    label: "Name"
  },{
    key: "profiles",
    label: "Mitglieder"
  },

]
const selectedColumns = ref(templateColumns)
const columns = computed(() => templateColumns.filter((column) => selectedColumns.value.includes(column)))

const searchString = ref('')

const filteredRows = computed(() => {

  return useListFilter(searchString.value, items.value)
})

setup()
</script>

<template>
  <UDashboardNavbar title="Chats" :badge="filteredRows.length">
    <template #right>
      <UInput
          id="searchinput"
          v-model="searchString"
          icon="i-heroicons-funnel"
          autocomplete="off"
          placeholder="Suche..."
          class="hidden lg:block"
          @keydown.esc="$event.target.blur()"
      >
        <template #trailing>
          <UKbd value="/" />
        </template>
      </UInput>

      <UButton @click="router.push(`/chats/create`)">+ Chat</UButton>
    </template>
  </UDashboardNavbar>
  <UDashboardToolbar>
    <template #right>
      <USelectMenu
          v-model="selectedColumns"
          icon="i-heroicons-adjustments-horizontal-solid"
          :options="templateColumns"
          multiple
          class="hidden lg:block"
          by="key"
      >
        <template #label>
          Spalten
        </template>
      </USelectMenu>
    </template>
  </UDashboardToolbar>
  <UTable
      :rows="filteredRows"
      :columns="columns"
      class="w-full"
      :ui="{ divide: 'divide-gray-200 dark:divide-gray-800' }"
      @select="(i) => router.push(`/chats/show/${i.id}`) "
      :empty-state="{ icon: 'i-heroicons-circle-stack-20-solid', label: 'Keine Chats anzuzeigen' }"
  >
    <template #name-data="{row}">
      <span class="text-primary-500 font-bold" v-if="row === filteredRows[selectedItem]">{{row.name}}</span>
      <span v-else>{{row.name}}</span>
    </template>
    <template #profiles-data="{row}">
      {{row.profiles.map(i => i.fullName).join(", ")}}
    </template>
  </UTable>
</template>

<style scoped>

</style>