<script lang="ts">
	import * as Dialog from "$lib/components/ui/dialog";
	import * as Table from "$lib/components/ui/table";
	import { Button } from "$lib/components/ui/button";
	import { Input } from "$lib/components/ui/input";
	import { Search, Loader2 } from "lucide-svelte";
	import { valuationMethodsApi, type ValuationMethod } from "$lib/api/dashboard/reference_data/valuation_methods";
	import { onMount } from "svelte";

	let {
		open = $bindable(false),
		onSelect
	}: {
		open: boolean;
		onSelect: (method: ValuationMethod) => void;
	} = $props();

	let methods = $state<ValuationMethod[]>([]);
	let loading = $state(false);
	let searchTerm = $state("");

	async function loadMethods() {
		loading = true;
		try {
			const res = await valuationMethodsApi.list(1, 100);
			if (res.data) {
				methods = res.data.items || [];
			}
		} catch (error) {
			console.error("Error loading valuation methods:", error);
		} finally {
			loading = false;
		}
	}

	onMount(() => {
		loadMethods();
	});

	const filteredMethods = $derived(
		methods.filter(m => 
			m.key.toLowerCase().includes(searchTerm.toLowerCase()) || 
			m.description.toLowerCase().includes(searchTerm.toLowerCase())
		)
	);

	function handleSelect(method: ValuationMethod) {
		onSelect(method);
		open = false;
	}
</script>

<Dialog.Root bind:open>
	<Dialog.Content class="sm:max-w-[500px] h-[600px] flex flex-col p-0">
		<Dialog.Header class="px-6 pt-6 pb-4 shrink-0">
			<Dialog.Title>Seleccionar Método de Valoración</Dialog.Title>
			<Dialog.Description>
				Busca y selecciona un método de valoración de la lista.
			</Dialog.Description>
		</Dialog.Header>

		<div class="px-6 pb-4 shrink-0">
			<div class="relative">
				<Search class="absolute left-2.5 top-2.5 h-4 w-4 text-muted-foreground" />
				<Input
					type="search"
					placeholder="Buscar por clave o descripción..."
					class="pl-9 h-9"
					bind:value={searchTerm}
				/>
			</div>
		</div>

		<div class="flex-1 overflow-y-auto px-6 pb-6">
			{#if loading}
				<div class="flex h-full items-center justify-center">
					<Loader2 class="h-6 w-6 animate-spin text-muted-foreground" />
				</div>
			{:else}
				<Table.Root>
					<Table.Header class="sticky top-0 bg-background z-10">
						<Table.Row>
							<Table.Head class="w-20">Clave</Table.Head>
							<Table.Head>Descripción</Table.Head>
						</Table.Row>
					</Table.Header>
					<Table.Body>
						{#each filteredMethods as method}
							<Table.Row 
								class="cursor-pointer hover:bg-muted" 
								onclick={() => handleSelect(method)}
							>
								<Table.Cell class="font-medium">{method.key}</Table.Cell>
								<Table.Cell>{method.description}</Table.Cell>
							</Table.Row>
						{:else}
							<Table.Row>
								<Table.Cell colspan={2} class="h-24 text-center text-muted-foreground">
									No se encontraron métodos de valoración.
								</Table.Cell>
							</Table.Row>
						{/each}
					</Table.Body>
				</Table.Root>
			{/if}
		</div>
	</Dialog.Content>
</Dialog.Root>
