@using SteUp.Shared.Components.SingleElements.MessageBox @using SteUp.Shared.Core.Dto @using SteUp.Shared.Core.Entities @using SteUp.Shared.Core.Enum @using SteUp.Shared.Core.Helpers @using SteUp.Shared.Core.Interface.IntegryApi @using SteUp.Shared.Core.Interface.LocalDb @using SteUp.Shared.Core.Interface.System @using Microsoft.Extensions.Logging @inject IIspezioniService IspezioniService @inject IIntegrySteupService IntegrySteupService @inject IDialogService Dialog @inject IFileManager FileManager @inject ILogger Logger
@Scheda.DescrizioneReparto

@Titolo

@if (!Scheda.Note.IsNullOrEmpty()) {

@Scheda.Note

}
@if (ReadOnly) { Apri @if (Scheda.ImageNames?.Count > 0) { Invia foto } } else { @(IsBozza ? "Completa" : "Modifica") @if (NetworkService.IsNetworkAvailable() && SyncState == Core.Helpers.SyncState.DaInviare) { Invia } }
@if (!ReadOnly) { Elimina scheda }
@code{ [Parameter] public Ispezione Ispezione { get; set; } = new(); [Parameter] public required Scheda Scheda { get; set; } [Parameter] public EventCallback OnSchedaModified { get; set; } [Parameter] public EventCallback OnSchedaDeleted { get; set; } [Parameter] public EventCallback OnBusyChanged { get; set; } private ConfirmMessageBox _messageBox = null!; private bool _busy; private bool ReadOnly => Ispezione.Stato == StatusEnum.Completata; private SyncState SyncState => Scheda.GetSyncState(); private bool IsBozza => SyncState == Core.Helpers.SyncState.Bozza; // Una bozza puo' non avere ancora il motivo: meglio dirlo che lasciare un vuoto. private string Titolo => Scheda.ActivityTypeId.IsNullOrEmpty() ? "Motivo da indicare" : Scheda.ActivityTypeId!; private StatusBadgeHelper.BadgeSpec ScadenzaBadge => (ScadenzaEnum)Scheda.Scadenza switch { ScadenzaEnum.Altissima24Ore => new StatusBadgeHelper.BadgeSpec("danger", "ri-alarm-warning-line", "Entro 24 ore"), ScadenzaEnum.Alta1Settimana => new StatusBadgeHelper.BadgeSpec("warning", "ri-time-line", "Entro 1 settimana"), ScadenzaEnum.Media1Mese => new StatusBadgeHelper.BadgeSpec("info", "ri-calendar-line", "Entro 1 mese"), _ => new StatusBadgeHelper.BadgeSpec("neutral", "ri-calendar-line", "Entro 2 mesi") }; private string ArticoliLabel => Scheda.Articoli.Count == 1 ? "1 articolo" : $"{Scheda.Articoli.Count} articoli"; private string FotoLabel => Scheda.ImageNames?.Count switch { null or 0 => "nessuna foto", 1 => "1 foto", var n => $"{n} foto" }; private string SemanticLabel => $"Scheda {Titolo}, reparto {Scheda.DescrizioneReparto}, {SyncState.ToBadge().Label}, {ArticoliLabel}, {FotoLabel}"; private async Task SetBusy(bool value) { _busy = value; await OnBusyChanged.InvokeAsync(value); StateHasChanged(); } private async Task UpdateScheda() { var modal = await Dialog.OpenFormScheda(Ispezione.CodMdep, Ispezione.Data, false, Scheda); if (modal is { Canceled: false, Data: Scheda scheda }) await OnSchedaModified.InvokeAsync(scheda); } private void ViewScheda() => _ = Dialog.OpenFormScheda(Ispezione.CodMdep, Ispezione.Data, false, Scheda, true); private async Task DeleteScheda() { var result = await _messageBox.ShowAsync(); if (result is not true) return; await SetBusy(true); try { if (Scheda.ActivityId != null) await IntegrySteupService.DeleteScheda(Scheda.ActivityId); if (Scheda.ImageNames != null) { foreach (var fileName in Scheda.ImageNames) { FileManager.RemoveInspectionFile(Ispezione, fileName); } } var deleteScheda = await IspezioniService.DeleteSchedaAsync(Scheda.Id); if (deleteScheda) await OnSchedaDeleted.InvokeAsync(Scheda); } finally { await SetBusy(false); } } private async Task ExportScheda() { // Guardia anti doppio-tap: invio irreversibile al server. if (_busy) return; await SetBusy(true); try { var apiResponse = await IntegrySteupService.SaveScheda( new SaveRequestDto { LocalIdScheda = Scheda.Id, ActivityTypeId = Scheda.ActivityTypeId, CodJfas = Scheda.CodJfas, CodMdep = Ispezione.CodMdep, DataCreazione = Ispezione.Data, Note = Scheda.Note, PersonaRif = Scheda.Responsabile, Barcodes = Scheda.Articoli.ConvertAll(x => x.Barcode), Scandeza = (ScadenzaEnum)Scheda.Scadenza, // Aggancia all'ispezione esistente sul server (evita di crearne una nuova a ogni invio). ParentActivityId = SteupDataService.InspectionPageState.Ispezione.ActivityId } ); if (apiResponse == null) { Snackbar.Add("Invio non riuscito. La scheda resta sul dispositivo, riprova.", Severity.Error); return; } Scheda.ActivityId = apiResponse.ActivityIdScheda; SteupDataService.InspectionPageState.Ispezione.ActivityId = apiResponse.ActivityIdIspezione; await IspezioniService.UpdateActivityIdIspezioneAsync(Ispezione.CodMdep, Ispezione.Data, UserSession.User.Username, apiResponse.ActivityIdIspezione ); await IspezioniService.UpdateActivityIdSchedaAsync(Scheda.Id, Scheda.ActivityId); // Notifica il genitore così il contatore "da inviare" del reparto si aggiorna. await OnSchedaModified.InvokeAsync(Scheda); Snackbar.Add("Scheda inviata", Severity.Success); } catch (Exception e) { Snackbar.Add("Invio non riuscito. La scheda resta sul dispositivo, riprova.", Severity.Error); Logger.LogError(e, e.Message); } finally { await SetBusy(false); } } private async Task ExportImg() { if (_busy || Scheda.ImageNames == null) return; await SetBusy(true); try { var fileList = (await FileManager.GetInspectionFiles(Ispezione, Scheda.ImageNames))? .Where(x => x.ToUpload).ToList(); if (fileList == null || fileList.Count == 0) { Snackbar.Add("Nessuna nuova foto da inviare", Severity.Info); return; } foreach (var file in fileList) { await IntegrySteupService.UploadFile(Scheda.ActivityId!, file.FileBytes!, file.Name!); } Snackbar.Add(fileList.Count == 1 ? "Foto inviata" : $"{fileList.Count} foto inviate", Severity.Success); } catch (Exception e) { Snackbar.Add("Invio foto non riuscito, riprova.", Severity.Error); Logger.LogError(e, e.Message); } finally { await SetBusy(false); } } }