diff --git a/nuxt4-app/app/components/ClienteMultiSelector.vue b/nuxt4-app/app/components/ClienteMultiSelector.vue index 6db7f3e..0f593d9 100644 --- a/nuxt4-app/app/components/ClienteMultiSelector.vue +++ b/nuxt4-app/app/components/ClienteMultiSelector.vue @@ -1,30 +1,33 @@ @@ -94,10 +45,8 @@ interface Cliente { id: number name: string - cedula?: number + cedula?: string ubicacion?: string - telefono?: string - email?: string } const props = defineProps<{ @@ -112,61 +61,55 @@ const emit = defineEmits<{ const clientes = ref([]) const loading = ref(false) const searchQuery = ref('') +const isMenuOpen = ref(false) -// Computed -const filteredClientes = computed(() => { - if (!searchQuery.value) return clientes.value +// Computed - Sync with props +const selectedClientes = computed({ + get: () => { + return clientes.value.filter(c => props.selectedIds.includes(c.id)) + }, + set: (value: Cliente[]) => { + emit('update:selectedIds', value.map(c => c.id)) + } +}) + +// Computed - Filter items based on search (min 4 characters) +const filteredItems = computed(() => { + const query = searchQuery.value.trim() + + // Only show items if search has at least 4 characters + if (query.length < 4) { + return [] + } + + const lowerQuery = query.toLowerCase() - const query = searchQuery.value.toLowerCase() return clientes.value.filter(c => - c.name.toLowerCase().includes(query) || - c.cedula?.toString().includes(query) || - c.ubicacion?.toLowerCase().includes(query) + c.name.toLowerCase().includes(lowerQuery) || + c.cedula?.includes(query) ) }) -const ubicaciones = computed(() => { - const locs = new Set() - clientes.value.forEach(c => { - if (c.ubicacion) locs.add(c.ubicacion) - }) - return Array.from(locs).sort() +// Watch searchQuery to control menu opening +watch(searchQuery, (newValue) => { + if (newValue.trim().length >= 4 && filteredItems.value.length > 0) { + isMenuOpen.value = true + } else { + isMenuOpen.value = false + } }) // Methods -function isSelected(id: number): boolean { - return props.selectedIds.includes(id) -} - -function toggleCliente(id: number) { - const newIds = isSelected(id) - ? props.selectedIds.filter(i => i !== id) - : [...props.selectedIds, id] - - emit('update:selectedIds', newIds) -} - function clearAll() { emit('update:selectedIds', []) } -function selectByUbicacion(ubicacion: string) { - const clientesInUbicacion = clientes.value - .filter(c => c.ubicacion === ubicacion) - .map(c => c.id) - - // Add to selection (not replace) - const newIds = [...new Set([...props.selectedIds, ...clientesInUbicacion])] - emit('update:selectedIds', newIds) -} - -function formatCedula(cedula: number): string { - const str = cedula.toString() +function formatCedula(cedula: string): string { // Format as XXXX-XXXX-XXXXX - if (str.length === 13) { - return `${str.slice(0, 4)}-${str.slice(4, 8)}-${str.slice(8)}` + if (cedula.length === 13) { + return `${cedula.slice(0, 4)}-${cedula.slice(4, 8)}-${cedula.slice(8)}` } - return str + return cedula } async function loadClientes() {