<script lang="ts">
	import { page } from '$app/stores';
	import SimpleDataTable from '$lib/components/dashboard/general_catalogs/simple-data-table.svelte';
	import CreateDialog from '$lib/components/dashboard/general_catalogs/multi_currency_types/create-edit-dialog.svelte';
	import { Button } from '$lib/components/ui/button';
	import { Input } from '$lib/components/ui/input';
	import { Plus } from 'lucide-svelte';
	import { goto } from '$app/navigation';
	import { browser } from '$app/environment';
	import { useShortcuts } from '$lib/hooks/use-shortcuts';
	import { obtenerAtajosListaMonedas } from '$lib/config/shortcuts/dashboard/general_catalogs/multi_currency_types/list';

	let { data } = $props();
	let dialogOpen = $state(false);

	// Atajos
	useShortcuts(
		'Lista Monedas',
		obtenerAtajosListaMonedas({
			manejarNuevo: () => (dialogOpen = true),
			manejarActualizar: () => goto($page.url, { invalidateAll: true }),
		})
	);

	// Filtros
	let searchCode = $state($page.url.searchParams.get('currency_type_code') || '');
	let searchCountry = $state($page.url.searchParams.get('country_key') || '');
	let timeout: ReturnType<typeof setTimeout>;

	const columns = [
		{ key: 'currency_type_code', label: 'Código Moneda' },
		{ key: 'country_key', label: 'País' },
		{ key: 'conversion_factor', label: 'Factor Conversión' },
		{ key: 'publication_date', label: 'Fecha Publicación' }
	];

	function handleSuccess() {
		const url = new URL($page.url);
		goto(url, { invalidateAll: true });
	}

	function handleSearch() {
		if (!browser) return;
		clearTimeout(timeout);
		timeout = setTimeout(() => {
			const url = new URL($page.url);
			if (searchCode) url.searchParams.set('currency_type_code', searchCode);
			else url.searchParams.delete('currency_type_code');

			if (searchCountry) url.searchParams.set('country_key', searchCountry);
			else url.searchParams.delete('country_key');

			url.searchParams.set('page', '1');
			goto(url, { keepFocus: true, noScroll: true });
		}, 500);
	}
</script>

<div class="flex flex-col gap-4 p-4">
	<div class="flex items-center justify-between">
		<div>
			<h1 class="text-2xl font-bold tracking-tight">Tipos de Moneda Múltiple</h1>
			<p class="text-muted-foreground">Gestión del catálogo de tipos de moneda múltiple</p>
		</div>
		<Button onclick={() => (dialogOpen = true)}>
			<Plus class="mr-2 h-4 w-4" />
			Nuevo Tipo
		</Button>
	</div>

	<div class="flex gap-4 items-end">
		<div class="grid w-full max-w-sm items-center gap-1.5">
			<Input placeholder="Buscar por código..." bind:value={searchCode} oninput={handleSearch} />
		</div>
		<div class="grid w-full max-w-sm items-center gap-1.5">
			<Input placeholder="Buscar por país..." bind:value={searchCountry} oninput={handleSearch} />
		</div>
	</div>

	<div class="rounded-md border">
		<SimpleDataTable
			data={data.types?.items || []}
			{columns}
			pageCount={data.types?.pages || 0}
			totalItems={data.types?.total || 0}
		/>
	</div>

	<CreateDialog bind:open={dialogOpen} onSuccess={handleSuccess} />
</div>
