<template>
  <div
    v-click-outside="hide"
    class="search-form-wrapper"
  >
    <div @click="togglePanel">
      <slot />
    </div>

    <transition name="fade">
      <div
        v-if="open"
        class="search-form-panel"
      >
        <input
          ref="search-value"
          v-model="value"
          type="search"
          :placeholder="__('Search')"
          @input="search"
        >

        <ul class="results" v-if="value">
          <div
            v-if="loading"
            class="loading"
          >
            <sync-loader
              :size="10"
              class="spinner"
            />
          </div>
          <li
            v-for="item in results"
            v-else-if="results.length > 0"
            :key="item.id"
          >
            <a
              :href="item.url"
              :title="item.title"
            >
              {{ item.title }}
              <span class="type">
                {{ item.type }}
              </span>
            </a>
          </li>
          <li
            v-else-if="results.length === 0"
            class="no-results"
          >
            {{ __('No results') }}
          </li>
        </ul>
      </div>
    </transition>
  </div>
</template>

<script>
import { get } from 'axios'
import ClickOutside from 'vue-click-outside'
import { SyncLoader } from '@saeris/vue-spinners'

export default {
  name: 'SearchForm',
  
  components: {
    SyncLoader,
  },

  directives: {
    ClickOutside,
  },
  
  props: {
  },

  data() {
    return {
      open: false,
      loading: false,
      value: '',
      results: [],
    };
  },

  created() {
    this.state = this.$props.initialState;
  },

  methods: {
    togglePanel: function () {
      this.open = !this.open

      setTimeout(() => {
        if (this.open) this.$refs['search-value'].focus()
      }, 1)
    },

    hide: function () {
      this.open = false
    },

    search: function () {
      this.loading = true

      get(`${wpData.baseApi}/search`, {
        params: {
          search: this.value,
        },
      })
        .then((response) => {
          console.log(response)

          this.results = response.data
          this.loading = false
        })
    },
  },
};
</script>

<style lang="scss" scoped>
.search-form-wrapper {
  @apply
    relative;

  .search-form-panel {
    @apply
      absolute;

    width: 250px;
    top: -8px;
    right: 100%;

    input {
      @apply
        py-2
        px-4
        w-full
        rounded-sm;
    }

    .results {
      @apply
        mt-1
        p-0
        rounded-sm
        bg-white
        shadow
        w-full
        flex
        flex-col
        overflow-x-hidden
        overflow-y-auto
        text-black;

      max-height: 300px;

      .loading {
        @apply
          flex
          items-center
          justify-center
          h-12
          py-2
          px-4;

        .spinner > div {
          @apply
            opacity-50
            bg-primary-500;
        }
      }

      .no-results {
        @apply
          text-primary-500
          h-12
          flex
          items-center
          px-4;
      }

      li {
        @apply
          m-0
          p-0
          block;

        line-height: 1;

        a {
          @apply
            block
            px-4
            py-2
            border-0
            whitespace-nowrap
            text-primary-500;

          &:hover {
            @apply
              opacity-100
              text-white
              bg-primary-500;
          }

          .type {
            @apply
              block
              text-xs
              text-primary-400;
          }
        }
      }
    }
  }

  .fade-enter-active,
  .fade-leave-active {
    transition: all 0.25s ease;
    transform: translateX(0);
  }

  .fade-enter,
  .fade-leave-to {
    opacity: 0;
    transform: translateX(-20px);
  }
}

</style>