import { useEffect, useMemo, useState } from "react"
import { Popover, PopoverTrigger, PopoverContent } from "@/components/ui/popover"
import { Command, CommandInput, CommandList, CommandGroup, CommandItem, CommandEmpty } from "@/components/ui/command"
import { Button } from "@/components/ui/button"
import { Check, ChevronsUpDown, Building2, GraduationCap } from "lucide-react"
import { supabase } from "@/auth/supabase"
const cls = (...a: (string | false | undefined)[]) => a.filter(Boolean).join(" ")
/* ---------- Base reutilizable ---------- */
function ComboBase({
placeholder,
value,
onChange,
options,
icon: Icon,
disabled = false,
}: {
placeholder: string
value?: string | null
onChange: (id: string) => void
options: { id: string; label: string }[]
icon?: any
disabled?: boolean
}) {
const [open, setOpen] = useState(false)
const current = useMemo(() => options.find(o => o.id === value), [options, value])
return (
Sin resultados
{options.map(o => (
{ onChange(o.id); setOpen(false) }}
className="whitespace-normal"
>
{o.label}
))}
)
}
/* ---------- Facultades ---------- */
export function FacultadCombobox({
value,
onChange,
disabled = false,
placeholder = "Selecciona facultad…",
}: {
value?: string | null
onChange: (id: string) => void
disabled?: boolean
placeholder?: string
}) {
const [items, setItems] = useState<{ id: string; label: string }[]>([])
useEffect(() => {
supabase
.from("facultades")
.select("id, nombre")
.order("nombre", { ascending: true })
.then(({ data }) => setItems((data ?? []).map(f => ({ id: f.id, label: f.nombre }))))
}, [])
return (
)
}
/* ---------- Carreras (filtra por facultad) ---------- */
export function CarreraCombobox({
facultadId,
value,
onChange,
disabled = false,
placeholder,
}: {
facultadId?: string | null
value?: string | null
onChange: (id: string) => void
disabled?: boolean
placeholder?: string
}) {
const [items, setItems] = useState<{ id: string; label: string }[]>([])
useEffect(() => {
if (!facultadId) { setItems([]); return }
supabase
.from("carreras")
.select("id, nombre")
.eq("facultad_id", facultadId)
.order("nombre", { ascending: true })
.then(({ data }) => setItems((data ?? []).map(c => ({ id: c.id, label: c.nombre }))))
}, [facultadId])
const ph = placeholder ?? (facultadId ? "Selecciona carrera…" : "Selecciona una facultad primero")
return (
)
}