<template>
  <div class="search">
    <div
        @click="toggleSearch(true)"
        class="search__wrapper"
        :class="[{ active: isSearchActive }]"
    >
      <form class="search__form" @submit.prevent="submitSearch">
        <input
            type="text"
            v-model="query"
            :placeholder="t('fields.search')"
        />
        <div class="search__tools">
          <button
              type="button"
              @click="clearSearch"
              v-if="query"
          >
            <i class="pi pi-times"></i>
          </button>
          <div class="search__tools-line" v-if="query"></div>
          <button type="submit">
            <i class="pi pi-search"></i>
          </button>
        </div>
      </form>
      <div class="search__results" :class="[{ active: (searchResults && isSearchActive) || loading }]">
        <header-search-skeleton v-if="loading" />
        <div 
            class="search__results-inner"
            v-for="(blocks, item) in filteredSearchResults"
            :key="item"
        >
          <div class="search__results-title">
            <p>{{ getBlockTitle(item) }}:</p>
          </div>
          <div
              class="search__item"
              v-for="item in blocks"
              :key="item.uuid"
          >
            <div class="search__item-left">
              <i class="pi pi-search"></i>
              <p>{{ item.name }}</p>
            </div>
            <i class="pi pi-external-link"></i>
          </div>
        </div>
        <div class="search__results-empty" v-if="!hasResults && query && !loading">
          <p>{{ t('header.search.empty') }}</p>
        </div>
      </div>
    </div>
    <div
        class="search__bg"
        @click="toggleSearch(false)"
        v-if="isSearchActive"
    />
  </div>
</template>

<script setup>
import {useI18n} from "vue-i18n";
import HeaderSearchSkeleton from "@/components/skeletons/header/header-search-skeleton.vue";
import { useSearchUI } from "@/composables/search";
import {useRouter} from "vue-router";

const {t} = useI18n();
const router = useRouter();

const {
  query,
  isSearchActive,
  loading,
  searchResults,
  filteredSearchResults,
  hasResults,
  getBlockTitle,
  clearSearch,
  toggleSearch
} = useSearchUI();

function submitSearch() {
  if (query.value) {
    router.push({
      name: 'search',
      query: { q: query.value }
    });

    toggleSearch(false);
  }
}
</script>

<style lang="scss" scoped>
.search {
  width: 100%;
  position: relative;
  height: 45px;

  &__bg {
    background-color: #0000001a;
    height: 100vh;
    left: 0;
    position: fixed;
    top: 0;
    width: 100vw;
    z-index: 1;
  }

  &__wrapper {
    width: 100%;
    background-color: #f7f7f7;
    border-radius: $default_border_radius;
    transition: 0.2s;
    position: relative;
    z-index: 2;

    &.active {
      background-color: $white;
      box-shadow: 0 0 0 1px #0000000a,0 4px 4px #0000000a,0 20px 40px #00000014;
    }

    @include hover {
      background-color: $white;
      box-shadow: 0 0 0 1px #0000000a,0 4px 4px #0000000a,0 20px 40px #00000014;
    }
  }

  &__form {
    width: 100%;
    height: 45px;
    position: relative;

    & input {
      background-color: transparent;
      width: 100%;
      height: 100%;
      padding-inline: 20px 150px;
      border: 1px solid #dedede;
      border-radius: $default_border_radius;

      font-size: 16px;
      font-weight: 500;
    }
  }

  &__tools {
    position: absolute;
    right: 20px;
    top: 50%;
    transform: translateY(-50%);
    display: flex;
    align-items: center;
    gap: 10px;

    & button {
      cursor: pointer;
      border-radius: $default_border_radius;
      padding: 5px 12px;
      border: 1px solid $accent;
      background-color: rgba($accent, 0.2);
      transition: 0.2s;

      font-size: 12px;
      color: $accent;

      @include hover {
        background-color: rgba($accent, 1);
        color: $white;
      }
    }

    &-line {
      background-color: $accent;
      height: 15px;
      width: 1px;
    }
  }

  &__results {
    max-height: 0;
    overflow: auto;
    transition: 0.2s;

    &.active {
      max-height: 40vh;
    }

    &-title {
      background-color: rgba($accent, 0.2);
      padding: 7px 20px;

      font-weight: 600;
    }

    &-empty {
      padding: 10px 20px;
      font-size: 14px;
    }
  }

  &__item {
    cursor: pointer;
    padding: 7px 20px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 30px;
    transition: 0.2s;
    font-size: 14px;

    @include hover {
      background-color: #efefef;
    }

    &-left {
      display: flex;
      align-items: center;
      gap: 15px;
    }

    & p {
      word-break: break-all;
      display: -webkit-box;
      -webkit-box-orient: vertical;
      -webkit-line-clamp: 1;
      overflow: hidden;
    }
    
    & i {
      color: #7c7c7c;
    }
  }
}
</style>