@page "/stock/productimport"
@using Domain.Dtos.Stock
@using phronCare.UIBlazor.Services.Stock
@inject IJSRuntime JS
@inject IToastService toastService
@inject NavigationManager Navigation
@inject LSProductService productService
Importación masiva de productos
@if (UploadedFile != null)
{
}
@if (PreviewItems != null)
{
Vista previa
| Cód. Fábrica |
Nombre |
Descripción |
Tipo |
Trazabilidad |
División |
Unidad |
Plus |
Cód. Ext. |
Errores |
@foreach (var item in PreviewItems)
{
| @item.FactoryCode |
@item.Name |
@item.Description |
@item.ProductType |
@item.TraceabilityType |
@item.DivisionCode |
@item.UnitCode |
@(item.PlusProcess ? "Sí" : "No") |
@item.ExternalCode |
@item.ErrorMessage |
}
}
@code {
private List? PreviewItems;
private IBrowserFile? UploadedFile;
private bool _isUploading;
private async Task DownloadTemplate()
{
try
{
await productService.DownloadTemplateAsync();
}
catch (Exception ex)
{
toastService.ShowError($"Error al descargar la plantilla: {ex.Message}");
}
}
private void HandleFileSelected(InputFileChangeEventArgs e)
{
UploadedFile = e.File;
PreviewItems = null; // limpiar vista previa anterior
}
private async Task ProcessFile()
{
if (UploadedFile == null) return;
try
{
_isUploading = true;
PreviewItems = await productService.PreviewImportAsync(UploadedFile);
toastService.ShowSuccess("Vista previa generada correctamente.");
}
catch (Exception ex)
{
toastService.ShowError($"Error al procesar el archivo: {ex.Message}");
}
finally
{
_isUploading = false;
}
}
private async Task ConfirmImport()
{
if (PreviewItems is null || PreviewItems.Any(x => x.HasError))
{
toastService.ShowWarning("Existen errores; corríjalos antes de importar.");
return;
}
try
{
var result = await productService.ConfirmImportAsync(PreviewItems);
toastService.ShowSuccess($"Se importaron {result?.Inserted ?? 0} productos.");
UploadedFile = null;
PreviewItems = null;
}
catch (Exception ex)
{
toastService.ShowError($"Error al importar: {ex.Message}");
}
}
}