<script setup>
  defineShortcuts({
    /*'/': () => {
      //console.log(searchinput)
      //searchinput.value.focus()
      document.getElementById("searchinput").focus()
    },*/
    'Enter': {
      usingInput: true,
      handler: () => {
        router.push(`/standardEntity/${props.type}/show/${props.rows.value[selectedItem.value].id}`)
      }
    },
    'arrowdown': () => {
      if(selectedItem.value < props.rows.length - 1) {
        selectedItem.value += 1
      } else {
        selectedItem.value = 0
      }
    },
    'arrowup': () => {
      if(selectedItem.value === 0) {
        selectedItem.value = props.rows.length - 1
      } else {
        selectedItem.value -= 1
      }
    }
  })
  const props = defineProps({
    rows: {
      type: Array,
      required: true,
      default: []
    },
    columns: {
      type: Array,
      required: true,
    },
    type: {
      type: String,
      required: true,
    },
    loading: {
      type: Boolean,
      required: true,
      default: false
    }
  })

  const emit = defineEmits(["sort"]);

  const dataStore = useDataStore()

  const router = useRouter()

  const dataType = dataStore.dataTypes[props.type]

  const selectedItem = ref(0)
  const sort = ref({
    column: dataType.supabaseSortColumn || "date",
    direction: 'desc'
  })

</script>

<template>
  <UTable
      :loading="props.loading"
      :loading-state="{ icon: 'i-heroicons-arrow-path-20-solid', label: 'Loading...' }"
      sort-mode="manual"
      v-model:sort="sort"
      @update:sort="emit('sort',{sort_column: sort.column, sort_direction: sort.direction})"
      v-if="dataType && columns"
      :rows="props.rows"
      :columns="props.columns"
      class="w-full"
      :ui="{ divide: 'divide-gray-200 dark:divide-gray-800' }"
      @select="(i) => router.push(`/standardEntity/${type}/show/${i.id}`) "
      :empty-state="{ icon: 'i-heroicons-circle-stack-20-solid', label: `Keine ${dataType.label} anzuzeigen` }"
  >
<!--    <template
        v-for="column in dataType.templateColumns.filter(i => !i.disabledInTable)"
        v-slot:[`${column.key}-header`]="{row}">
      <span class="text-nowrap">{{column.label}}</span>
    </template>-->
    <template #name-data="{row}">
      <span
          v-if="row.id === props.rows[selectedItem].id"
          class="text-primary-500 font-bold">
        <UTooltip
            :text="row.name"
        >
          {{dataType.templateColumns.find(i => i.key === "name").maxLength ? (row.name.length > dataType.templateColumns.find(i => i.key === "name").maxLength ? `${row.name.substring(0,dataType.templateColumns.find(i => i.key === "name").maxLength)}...` : row.name ) : row.name}}
        </UTooltip>      </span>
      <span v-else>
        <UTooltip
          :text="row.name"
        >
          {{dataType.templateColumns.find(i => i.key === "name").maxLength ? (row.name.length > dataType.templateColumns.find(i => i.key === "name").maxLength ? `${row.name.substring(0,dataType.templateColumns.find(i => i.key === "name").maxLength)}...` : row.name ) : row.name}}
        </UTooltip>
      </span>
    </template>
    <template #fullName-data="{row}">
      <span
          v-if="row.id === props.rows[selectedItem].id"
          class="text-primary-500 font-bold">{{row.fullName}}
      </span>
      <span v-else>
        {{row.fullName}}
      </span>
    </template>
    <template #licensePlate-data="{row}">
      <span
          v-if="row.id === props.rows[selectedItem].id"
          class="text-primary-500 font-bold">{{row.licensePlate}}
      </span>
      <span v-else>
        {{row.licensePlate}}
      </span>
    </template>
    <template
        v-for="column in dataType.templateColumns.filter(i => i.key !== 'name' && i.key !== 'fullName' && i.key !== 'licensePlate' && !i.disabledInTable)"
        v-slot:[`${column.key}-data`]="{row}">
      <component v-if="column.component" :is="column.component" :row="row"></component>
      <span v-else-if="row[column.key]">
        <UTooltip :text="row[column.key]">
        {{row[column.key] ? `${column.maxLength ? (row[column.key].length > column.maxLength ? `${row[column.key].substring(0,column.maxLength)}...` : row[column.key]) : row[column.key]} ${column.unit ? column.unit : ''}`: ''}}
      </UTooltip>
      </span>

    </template>
  </UTable>
</template>

<style scoped>

</style>