<script setup lang="ts">
import { useDocumentStore } from '@/stores/documents'
import { ref, nextTick } from 'vue'

const documentStore = useDocumentStore()
const searchQuery = ref<string>('')
const showSearchInput = ref<boolean>(false)
const search = ref<HTMLInputElement | null>()

const toggleSearchInput = () => {
  showSearchInput.value = !showSearchInput.value
  if (!showSearchInput.value) {
    searchQuery.value = ''
  }
  nextTick(() => {
    const input = search.value
    if (input) input.focus()
  })
}

const executeSearch = (ev: InputEvent) => {
  // FIXME: Make reactive instead of this update handler
  const query = (ev.target as HTMLInputElement).value
  console.log('Searching', query)
  documentStore.setFilter(query)
  console.log('Filtered')
}
</script>

<template>
  <nav>
    <div class="buttons">
      <UploadButton />
      <SvgButton name="create-folder" @click="() => documentStore.fileExplorer.newFolder()"/>
      <template v-if="true">
        <div class="smallgap"></div>
        <p>N selected files:</p>
        <!-- Needs better icons for copy/move/remove -->
        <SvgButton name="copy" />
        <SvgButton name="paste" />
        <SvgButton name="trash" />
      </template>
      <div class="spacer"></div>
      <SvgButton name="find" @click="toggleSearchInput" />
      <template v-if="showSearchInput">
        <input
          ref="search"
          type="search"
          v-model="searchQuery"
          class="margin-input"
          @keyup.esc="toggleSearchInput"
        />
      </template>
    </div>
  </nav>
</template>

<style scoped>
.buttons {
  padding: 0 0.5em;
  display: flex;
  align-items: center;
}
.spacer {
  flex-grow: 1;
}
.smallgap {
  margin-left: 2em;
}
.search-widget {
  display: flex;
  align-items: center;
}
input[type='search'] {
  background: var(--primary-background);
  color: var(--text-color);
  border: 0;
  border-radius: 0.1rem;
  padding: 0.5rem;
  outline: none;
  font-size: 1.2rem;
}
</style>
