<script setup>

import dayjs from "dayjs";

definePageMeta({
  middleware: "auth"
})

defineShortcuts({
  '/': () => {
    //console.log(searchinput)
    //searchinput.value.focus()
    document.getElementById("searchinput").focus()
  }
})

const profileStore = useProfileStore()
const tempStore = useTempStore()
const router = useRouter()
const supabase = useSupabaseClient()

const items = ref([])

const setupPage = async () => {
  items.value = (await supabase.from("accounts").select("*").order("number", {ascending:true})).data
}

const templateColumns = [
  {
    key: "number",
    label: "Nummer"
  },{
    key: "label",
    label: "Name"
  },
  {
    key: "description",
    label: "Beschreibung"
  },
]
const selectedColumns = ref(templateColumns)
const columns = computed(() => templateColumns.filter((column) => selectedColumns.value.includes(column)))


const searchString = ref(tempStore.searchStrings["bankstatements"] ||'')

const clearSearchString = () => {
  tempStore.clearSearchString("bankstatements")
  searchString.value = ''
}


const displayCurrency = (value, currency = "€") => {
  return `${Number(value).toFixed(2).replace(".",",")} ${currency}`
}





const selectedFilters = ref(['Nur offene anzeigen'])

const filteredRows = computed(() => {
  let temp = items.value


  return useSearch(searchString.value, temp)
})

setupPage()
</script>

<template>
  <UDashboardNavbar title="Buchungskonten" :badge="filteredRows.length">
    <template #right>
      <UInput
          id="searchinput"
          name="searchinput"
          v-model="searchString"
          icon="i-heroicons-funnel"
          autocomplete="off"
          placeholder="Suche..."
          class="hidden lg:block"
          @keydown.esc="$event.target.blur()"
          @change="tempStore.modifySearchString('bankstatements',searchString)"
      >
        <template #trailing>
          <UKbd value="/" />
        </template>
      </UInput>
      <UButton
          icon="i-heroicons-x-mark"
          variant="outline"
          color="rose"
          @click="clearSearchString()"
          v-if="searchString.length > 0"
      />
    </template>
  </UDashboardNavbar>
  <UDashboardToolbar>
    <template #right>
      <USelectMenu
          v-model="selectedColumns"
          icon="i-heroicons-adjustments-horizontal-solid"
          :options="templateColumns"
          multiple
          class="hidden lg:block"
          by="key"
          :ui-menu="{ width: 'min-w-max' }"
      >
        <template #label>
          Spalten
        </template>
      </USelectMenu>
      <USelectMenu
          icon="i-heroicons-adjustments-horizontal-solid"
          multiple
          v-model="selectedFilters"
          :options="['Nur offene anzeigen']"
          :color="selectedFilters.length > 0 ? 'primary' : 'white'"
          :ui-menu="{ width: 'min-w-max' }"
      >
        <template #label>
          Filter
        </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(`/accounts/show/${i.id}`)"
      :empty-state="{ icon: 'i-heroicons-circle-stack-20-solid', label: 'Keine Buchungen anzuzeigen' }"
  >

  </UTable>


</template>



<style scoped>

</style>