<template>
  <div ref="topbar" class="col-span-4 h-12 hidden md:flex">
    <form class="main-search w-full flex md:ml-0" action="#"
          method="GET"
    >
      <label for="search_field" class="sr-only">Поиск контента</label>
      <div
        class="relative w-full text-gray-400 focus-within:text-gray-600"
      >
        <input
          id="search_field"
          v-model="search"
          name="search_field"
          class="
            h-full
            w-full
            rounded-md
            border-transparent
            py-2
            pr-11
            pl-4
            text-base
            bg-orange-dark
            text-white
            placeholder-gray
            focus:border-transparent
            focus:outline-none
            focus:ring-2 focus:ring-orange-dark
        "
          placeholder="Глобальный поиск..."
          type="search"
        />

        <div
          class="pointer-events-none absolute inset-y-0 right-4 flex items-center text-gray"
        >
          <svg v-show="!loadead" class="flex-shrink-0 h-6 w-6"
               xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"
               fill="currentColor"
          > <path fill-rule="evenodd" clip-rule="evenodd"
                  d="M11 4a7 7 0 100 14 7 7 0 000-14zm-9 7a9 9 0 1118 0 9 9 0 01-18 0z"
          /> <path fill-rule="evenodd" clip-rule="evenodd"
                   d="M15.943 15.943a1 1 0 011.414 0l4.35 4.35a1 1 0 01-1.414 1.414l-4.35-4.35a1 1 0 010-1.414z"
          /> </svg>
          <svg v-show="loadead" class="flex-shrink-0 animate-spin h-6 w-6 text-white"
               xmlns="http://www.w3.org/2000/svg" fill="none"
               viewBox="0 0 24 24"
          >
            <circle class="opacity-25" cx="12"
                    cy="12" r="10"
                    stroke="currentColor" stroke-width="4"
            ></circle>
            <path class="opacity-75" fill="currentColor"
                  d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"
            ></path>
          </svg>
        </div>
      </div>
    </form>
  </div>

  <teleport to="body">
    <div v-if="open_drop">
      <div
        style="position: fixed;top: 0;right: 0;left: 0;bottom: 0;z-index: 9998;background: black; opacity: 0.2;"
        @click="open_drop = false"
      />
      <div ref="dropdown" class="z-[9999] main-search-list shadow-classic rounded-md bg-indigo-200 ">
        <div class="w-[580px] overflow-hidden pb-5">
          <button class="text-white absolute top-2 right-2" @click.stop="jump">
            <svg xmlns="http://www.w3.org/2000/svg" width="18"
                 height="18" viewBox="0 0 24 24"
                 fill="none" stroke="currentColor"
                 stroke-width="2" stroke-linecap="round"
                 stroke-linejoin="round" class="feather feather-x"
            ><line x1="18" y1="6"
                   x2="6" y2="18"
            ></line><line x1="6" y1="6"
                          x2="18" y2="18"
            ></line></svg>
          </button>

          <div data-simplebar class="mt-5 overflow-auto max-h-72">
            <div class="block md:hidden mx-5 p-3">
              <input v-model="search" type="text"
                     class="w-full
                      rounded-md
            border-transparent
            py-2
            pr-11
            pl-4
            text-base
            bg-indigo-400
            text-white
            placeholder-gray
            focus:border-transparent
            focus:outline-none
            focus:ring-2 focus:ring-orange-dark" placeholder="Глобальный поиск..."
              >
            </div>
            <div v-if="userSearch" class="text-gray-light divide-y divide-indigo-300"
                 @click.stop="jump"
            >
              <inertia-link v-for="user in userSearch" :key="user.id"
                            :href="route('profile.user', user.username)" class="flex cursor-pointer items-center p-3 hover:bg-indigo-300"
              >
                <div class="flex-shrink-0 mr-5">
                  <user-avatar :user="user" size="small"
                               class="w-8 h-8 md:h-10 md:w-10 text-md"
                  />
                </div>
                <div class="flex flex-col">
                  <span class="hover:underline text-sm md:text-base block font-medium text-white">{{ user.name }}</span>
                  <span class="hover:underline text-xs text-gray-light">{{ user.username }}</span>
                </div>
              </inertia-link>
            </div>

            <div v-if="tagSearch" class="text-gray-light divide-y divide-indigo-300">
              <inertia-link v-for="tag in tagSearch" :key="tag.id"
                            :href="route('feed.tags', tag.slug)" class="flex cursor-pointer items-center p-3 hover:bg-indigo-300"
              >
                <div class="flex items-center">
                  <span class="text-base md:text-lg block font-medium text-white">#{{ tag.name }}</span>
                  <span class="ml-5 text-xs text-gray-light">{{ tag.feeds_count }}</span>
                </div>
              </inertia-link>
            </div>
          </div>
        </div>
      </div>
    </div>
  </teleport>
</template>

<script>
import Popper from 'popper.js'
import throttle from 'lodash/throttle'
import axios from 'axios'

import UserAvatar from '@/Shared/Misc/UserAvatar.vue'

export default {
    components: {
        UserAvatar,
    },

    data() {
        return {
            search: '',
            open_drop: false,
            loadead: false,
            popper: null,
            data: [],
        }
    },

    computed: {
        userSearch() {
            return this.data.users
        },

        tagSearch() {
            return this.data.tags
        },
    },

    watch: {
        search: {
            handler: throttle(function () {
                this.loadead = true
                axios
                    .post(route('user.tab.search'), {
                        search: this.search,
                    })
                    .then(({ data }) => {
                        this.data = data
                        this.loadead = false
                        if (data.tags.length || data.users.length) {
                            this.open_drop = true
                        }
                    })
            }, 1000),
        },

        open_drop(open_drop) {
            if (open_drop) {
                this.$nextTick(() => {
                    if (window.matchMedia('(min-width: 1024px)').matches) {
                        this.popper = new Popper(this.$refs.topbar, this.$refs.dropdown, {
                            placement: 'bottom-start',
                            modifiers: {
                                preventOverflow: { boundariesElement: 'scrollParent' },
                                offset: {
                                    enabled: true,
                                    offset: '0, 10',
                                },
                            },
                        })
                    } else {
                        const mainHeader = document.querySelector('.main-header-wrp')
                        this.popper = new Popper(mainHeader, this.$refs.dropdown, {
                            placement: 'bottom-center',
                            positionFixed: true,
                            modifiers: {
                                preventOverflow: {
                                    boundariesElement: 'scrollParent',
                                    padding: 10,
                                },

                                offset: {
                                    enabled: true,
                                    offset: '0, 10',
                                },
                            },
                        })
                    }
                })
            } else if (this.popper) {
                setTimeout(() => this.popper.destroy(), 100)
            }
        },
    },

    methods: {
        jump() {
            this.data = []
            this.search = ''
            this.loadead = false
            this.open_drop = false
        },
        openModal(){
            this.open_drop = true
        }
    },

}
</script>
