diff --git a/frontend/src/routes/dashboard/general_catalogs/company_information/+page.svelte b/frontend/src/routes/dashboard/general_catalogs/company_information/+page.svelte index 98746992..efa44649 100644 --- a/frontend/src/routes/dashboard/general_catalogs/company_information/+page.svelte +++ b/frontend/src/routes/dashboard/general_catalogs/company_information/+page.svelte @@ -8,14 +8,14 @@ import { Button } from '$lib/components/ui/button'; import { Input } from '$lib/components/ui/input'; - import { Plus, RefreshCw } from 'lucide-svelte'; + import { Pencil, Plus, RefreshCw, Trash2 } from 'lucide-svelte'; import { goto } from '$app/navigation'; import { browser } from '$app/environment'; import * as m from '$lib/paraglide/messages.js'; import { useShortcuts } from '$lib/hooks/use-shortcuts'; import { obtenerAtajosListaEmpresa } from '$lib/config/shortcuts/dashboard/general_catalogs/company_information/list'; - import { getCompanies } from '$lib/api/dashboard/a76/general_catalogs/company'; + import { getCompanies, deleteCompany, type Company } from '$lib/api/dashboard/a76/general_catalogs/company'; import ErrorState from '$lib/components/dashboard/common/error-state.svelte'; import { currentUser } from '$lib/auth'; let { data } = $props(); @@ -46,13 +46,19 @@ let searchRfc = $state($page.url.searchParams.get('rfc') || ''); let timeout: ReturnType; - let allItems = $state(data.companies?.items || []); + let allItems = $state(data.companies?.items || []); let currentPage = $state(data.companies?.page || 1); let pageSize = $state(data.companies?.page_size || 50); let totalItems = $state(data.companies?.total || 0); let loading = $state(false); let hasMore = $derived(allItems.length < totalItems); + // Selección de filas + let selectedIds = $state<(string | number)[]>([]); + const singleSelected = $derived( + selectedIds.length === 1 ? (allItems.find((item) => item.id === selectedIds[0]) ?? null) : null + ); + $effect(() => { if (data.companies) { allItems = data.companies.items || []; @@ -142,7 +148,43 @@ } } + function handleRowClick(row: Company) { + const id = row.id; + selectedIds = selectedIds.includes(id) ? [] : [id]; + } + + function handleRowDoubleClick(row: Company) { + if (canView) { + goto(`/dashboard/general_catalogs/company_information/edit/${row.id}`); + } + } + + function handleEditSelected() { + if (!singleSelected) return; + goto(`/dashboard/general_catalogs/company_information/edit/${singleSelected.id}`); + } + + async function handleDeleteSelected() { + if (!singleSelected) return; + if (!confirm(`¿Estás seguro de eliminar la empresa "${singleSelected.name}"?\n\nNota: No se puede eliminar si tiene registros relacionados.`)) return; + + loading = true; + error = null; + try { + const response = await deleteCompany(singleSelected.id); + if (!response.error) { + selectedIds = []; + await reloadData(); + } + } catch (e) { + error = 'Error al eliminar la empresa'; + } finally { + loading = false; + } + } + function handleSuccess() { + selectedIds = []; reloadData(); } @@ -168,9 +210,9 @@ {#if error || !canView} - {:else} @@ -202,11 +244,10 @@ {loading} {hasMore} {loadMore} - onRowDoubleClick={(row) => { - if (canEdit) { - goto(`/dashboard/general_catalogs/company_information/edit/${row.id}`); - } - }} + {selectedIds} + onSelectedIdsChange={(ids) => (selectedIds = ids)} + onRowClick={handleRowClick} + onRowDoubleClick={handleRowDoubleClick} /> @@ -216,4 +257,40 @@
Mostrando {allItems.length} de {totalItems} registros
+ +
+ + {#if canEdit || canDelete} +
+
+
+ {#if canEdit} + + {/if} + {#if canDelete} + + {/if} +
+
+
+ {/if} diff --git a/frontend/src/routes/dashboard/general_catalogs/company_information/edit/[[id]]/+page.svelte b/frontend/src/routes/dashboard/general_catalogs/company_information/edit/[[id]]/+page.svelte index e45c88af..71af52c8 100644 --- a/frontend/src/routes/dashboard/general_catalogs/company_information/edit/[[id]]/+page.svelte +++ b/frontend/src/routes/dashboard/general_catalogs/company_information/edit/[[id]]/+page.svelte @@ -49,9 +49,10 @@ const id = $derived($page.params.id); const isEdit = $derived(!!id); const title = $derived(isEdit ? 'Editar Empresa' : 'Nueva Empresa'); + const canView = $derived(userHasCompanyCatalogAction($currentUser, 'view')); const canCreate = $derived(userHasCompanyCatalogAction($currentUser, 'create')); const canEdit = $derived(userHasCompanyCatalogAction($currentUser, 'edit')); - const canAccessEditor = $derived((isEdit && canEdit) || (!isEdit && canCreate)); + const canAccessEditor = $derived((isEdit && (canEdit || canView)) || (!isEdit && canCreate)); const canMutate = $derived(isEdit ? canEdit : canCreate); let loading = $state(false); @@ -245,8 +246,8 @@ }); async function fetchData(companyId: string) { - if (!canEdit) { - error = 'Permission denied: cat_company.edit'; + if (!canEdit && !canView) { + error = 'Permission denied: cat_company.view'; return; } loading = true; @@ -779,7 +780,7 @@ {#if !canAccessEditor}
- +
{:else}