<script lang="ts">
	import { page } from '$app/stores';
	import DataTable from '$lib/components/dashboard/general_catalogs/classification_concepts/data-table.svelte';
	import { createColumns } from '$lib/components/dashboard/general_catalogs/classification_concepts/columns';
	import CreateDialog from '$lib/components/dashboard/general_catalogs/classification/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 { obtenerAtajosListaClasificacion } from '$lib/config/shortcuts/dashboard/general_catalogs/classification/list';

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

	// Atajos
	useShortcuts(
		'Lista Clasificaciones',
		obtenerAtajosListaClasificacion({
			manejarNuevo: () => (dialogOpen = true),
			manejarActualizar: handleSuccess
		})
	);

	// Filtros
	let searchClassification = $state($page.url.searchParams.get('classification') || '');
	let searchDesc = $state($page.url.searchParams.get('description') || '');
	let timeout: ReturnType<typeof setTimeout>;

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

	const columns = $derived(createColumns(handleSuccess));

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

			if (searchDesc) url.searchParams.set('description', searchDesc);
			else url.searchParams.delete('description');

			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">Clasificaciones de Conceptos</h1>
			<p class="text-muted-foreground">Gestión del catálogo de clasificaciones de conceptos</p>
		</div>
		<Button onclick={() => (dialogOpen = true)}>
			<Plus class="mr-2 h-4 w-4" />
			Nueva Clasificación
		</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 clasificación..."
				bind:value={searchClassification}
				oninput={handleSearch}
			/>
		</div>
		<div class="grid w-full max-w-sm items-center gap-1.5">
			<Input
				placeholder="Buscar por descripción..."
				bind:value={searchDesc}
				oninput={handleSearch}
			/>
		</div>
	</div>

	<DataTable
		data={data.classifications?.items || []}
		{columns}
		pageCount={data.classifications?.pages || 0}
		totalItems={data.classifications?.total || 0}
	/>
	<CreateDialog
		bind:open={dialogOpen}
		onSuccess={handleSuccess}
	/>
</div>
