<template>
  <div class="filters-section">
    <!-- Búsqueda por nombre (fuzzy) -->
    <div class="search-input-wrapper">
      <UIcon name="i-heroicons-magnifying-glass" class="search-icon" />
      <input
        v-model="localFilters.search"
        type="text"
        placeholder="Buscar por nombre..."
        class="search-input"
      />
      <button
        v-if="localFilters.search"
        class="search-clear"
        title="Limpiar búsqueda"
        @click="localFilters.search = ''"
      >
        <UIcon name="i-heroicons-x-mark" class="w-4 h-4" />
      </button>
    </div>

    <!-- Filtros adicionales -->
    <div class="filter-row">
      <!-- Filtro por ID -->
      <div class="filter-field filter-id">
        <UInput
          v-model="localFilters.id"
          type="number"
          placeholder="ID"
          size="sm"
        />
      </div>

      <!-- Filtro por teléfono -->
      <div class="filter-field filter-phone">
        <UInput
          v-model="localFilters.telefono"
          placeholder="Teléfono"
          size="sm"
        />
      </div>

      <!-- Checkbox empleados -->
      <label class="filter-checkbox">
        <UCheckbox v-model="localFilters.empleado" />
        <span>Solo empleados</span>
      </label>

      <!-- Botón limpiar -->
      <UButton
        v-if="hasActiveFilters"
        variant="ghost"
        color="neutral"
        size="xs"
        icon="i-heroicons-x-mark"
        @click="clearFilters"
      >
        Limpiar
      </UButton>
    </div>
  </div>
</template>

<script setup lang="ts">
import type { ContactFilters } from '~/composables/useContacts'

const props = defineProps<{
  modelValue: ContactFilters
}>()

const emit = defineEmits<{
  'update:modelValue': [ContactFilters]
  'clear': []
}>()

const localFilters = computed({
  get: () => props.modelValue,
  set: (val) => emit('update:modelValue', val)
})

const hasActiveFilters = computed(() => {
  return (
    localFilters.value.search ||
    localFilters.value.id ||
    localFilters.value.telefono ||
    !localFilters.value.empleado
  )
})

const clearFilters = () => {
  emit('clear')
}
</script>

<style scoped>
.filters-section {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  margin-bottom: 1.5rem;
}

.search-input-wrapper {
  position: relative;
  display: flex;
  align-items: center;
  width: 100%;
  max-width: 400px;
}

.search-icon {
  position: absolute;
  left: 1rem;
  width: 1.25rem;
  height: 1.25rem;
  color: var(--color-gray-400);
  pointer-events: none;
  z-index: 1;
}

.search-input {
  width: 100%;
  padding: 0.75rem 3rem 0.75rem 3rem;
  font-size: 0.9375rem;
  font-weight: 500;
  border-radius: 1rem;
  background: rgba(255, 255, 255, 0.3);
  backdrop-filter: blur(10px) saturate(150%);
  border: 1px solid rgba(0, 0, 0, 0.08);
  color: var(--color-gray-800);
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  box-shadow:
    0 2px 6px 0 rgba(31, 38, 135, 0.08),
    inset 0 1px 1px 0 rgba(255, 255, 255, 0.3);
  outline: none;
}

.search-input::placeholder {
  color: var(--color-gray-400);
}

.search-input:focus {
  background: rgba(255, 255, 255, 0.4);
  border-color: rgba(var(--color-primary-500), 0.3);
  box-shadow:
    0 4px 12px 0 rgba(var(--color-primary-500), 0.15),
    0 0 0 3px rgba(var(--color-primary-500), 0.1),
    inset 0 1px 1px 0 rgba(255, 255, 255, 0.4);
}

.search-clear {
  position: absolute;
  right: 0.75rem;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 1.75rem;
  height: 1.75rem;
  border-radius: 0.5rem;
  background: rgba(0, 0, 0, 0.05);
  border: none;
  color: var(--color-gray-500);
  cursor: pointer;
  transition: all 0.2s ease;
  z-index: 1;
}

.search-clear:hover {
  background: rgba(0, 0, 0, 0.1);
  color: var(--color-gray-700);
  transform: scale(1.1);
}

.filter-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.75rem;
}

.filter-field {
  flex-shrink: 0;
}

.filter-id {
  width: 80px;
}

.filter-phone {
  width: 140px;
}

.filter-checkbox {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 0.75rem;
  font-size: 0.875rem;
  font-weight: 500;
  color: var(--color-gray-700);
  background: rgba(255, 255, 255, 0.25);
  backdrop-filter: blur(10px);
  border-radius: 0.75rem;
  border: 1px solid rgba(0, 0, 0, 0.08);
  cursor: pointer;
  transition: all 0.2s ease;
}

.filter-checkbox:hover {
  background: rgba(255, 255, 255, 0.35);
}

/* Responsive */
@media (max-width: 640px) {
  .filter-row {
    flex-direction: column;
    align-items: stretch;
  }

  .filter-id,
  .filter-phone {
    width: 100%;
  }

  .search-input-wrapper {
    max-width: 100%;
  }
}
</style>

<style>
/* Modo oscuro */
.dark .search-input {
  background: rgba(255, 255, 255, 0.05) !important;
  border-color: rgba(255, 255, 255, 0.1) !important;
  color: var(--color-gray-200) !important;
  box-shadow:
    0 2px 6px 0 rgba(0, 0, 0, 0.3),
    inset 0 1px 1px 0 rgba(255, 255, 255, 0.05) !important;
}

.dark .search-input::placeholder {
  color: var(--color-gray-500) !important;
}

.dark .search-input:focus {
  background: rgba(255, 255, 255, 0.08) !important;
  border-color: rgba(var(--color-primary-500), 0.5) !important;
  box-shadow:
    0 4px 12px 0 rgba(var(--color-primary-500), 0.3),
    0 0 0 3px rgba(var(--color-primary-500), 0.15),
    inset 0 1px 1px 0 rgba(255, 255, 255, 0.08) !important;
}

.dark .search-icon {
  color: var(--color-gray-500) !important;
}

.dark .search-clear {
  background: rgba(255, 255, 255, 0.05) !important;
  color: var(--color-gray-400) !important;
}

.dark .search-clear:hover {
  background: rgba(255, 255, 255, 0.1) !important;
  color: var(--color-gray-200) !important;
}

.dark .filter-checkbox {
  background: rgba(255, 255, 255, 0.05) !important;
  border-color: rgba(255, 255, 255, 0.1) !important;
  color: var(--color-gray-300) !important;
}

.dark .filter-checkbox:hover {
  background: rgba(255, 255, 255, 0.1) !important;
}
</style>
