@using System.Globalization @using CommunityToolkit.Mvvm.Messaging @using salesbook.Shared.Components.Layout @using salesbook.Shared.Components.Layout.Overlay @using salesbook.Shared.Components.SingleElements.BottomSheet @using salesbook.Shared.Core.Dto @using salesbook.Shared.Core.Dto.Activity @using salesbook.Shared.Core.Dto.Contact @using salesbook.Shared.Core.Entity @using salesbook.Shared.Core.Interface @using salesbook.Shared.Core.Interface.IntegryApi @using salesbook.Shared.Core.Interface.System.Network @using salesbook.Shared.Core.Messages.Activity.Copy @inject IManageDataService ManageData @inject INetworkService NetworkService @inject IIntegryApiService IntegryApiService @inject IMessenger Messenger @inject IDialogService Dialog @inject IAttachedService AttachedService
Suggerisci descrizione
Commessa @if (ActivityModel.CodJcom != null && SelectedComessa == null) { } else { }
Inizio
Fine
Avviso Nessuno All'ora pianificata 30 minuti prima 1 ora prima 2 ore prima 1 giorno prima 1 settimana prima
Assegnata a @if (ActivityModel.UserName != null && SelectedUser == null) { } else { }
Tipo @if (ActivityType.IsNullOrEmpty()) { } else { @foreach (var type in ActivityType) { @type.ActivityTypeId } }
Esito @foreach (var result in ActivityResult) { @result.ActivityResultId }
@if (!AttachedList.IsNullOrEmpty()) { foreach (var item in AttachedList!.Select((p, index) => new { p, index })) { @if (item.p.Type == AttachedDTO.TypeAttached.Position) { @item.p.Description } else { @item.p.Name } } } @if (!IsLoading) { if (ActivityFileList != null) { foreach (var file in ActivityFileList) { @file.FileName } } } else { }
@if (!IsView) {
Aggiungi allegati @if (!IsNew) {
Duplica
Elimina }
}
Confermi la cancellazione dell'attività corrente? Cancella
@code { [CascadingParameter] private IMudDialogInstance MudDialog { get; set; } [Parameter] public string? Id { get; set; } [Parameter] public ActivityDTO? ActivityCopied { get; set; } private ActivityDTO OriginalModel { get; set; } = new(); private ActivityDTO ActivityModel { get; set; } = new(); private List ActivityResult { get; set; } = []; private List ActivityType { get; set; } = []; private List Users { get; set; } = []; private List Commesse { get; set; } = []; private List Clienti { get; set; } = []; private List Pros { get; set; } = []; private List? ActivityFileList { get; set; } private bool IsNew { get; set; } private bool IsLoading { get; set; } private bool IsView => !NetworkService.ConnectionAvailable; private string? LabelSave { get; set; } //Overlay for save private bool VisibleOverlay { get; set; } private bool SuccessAnimation { get; set; } private bool OpenEsito { get; set; } private bool OpenAddMemo { get; set; } //MessageBox private MudMessageBox ConfirmDelete { get; set; } //Attached private List? AttachedList { get; set; } private bool CanAddPosition { get; set; } = true; // cache per commesse private string? _lastLoadedCodAnag; //Commessa private JtbComt? SelectedComessa { get; set; } //User private StbUser? SelectedUser { get; set; } protected override async Task OnInitializedAsync() { Snackbar.Configuration.PositionClass = Defaults.Classes.Position.TopCenter; if (ActivityCopied != null) { ActivityModel = ActivityCopied.Clone(); } else if (!Id.IsNullOrEmpty()) { ActivityModel = (await ManageData.GetActivity(new WhereCondActivity { ActivityId = Id }, true)).Last(); } if (Id.IsNullOrEmpty()) Id = ActivityModel.ActivityId; IsNew = Id.IsNullOrEmpty(); LabelSave = IsNew ? "Aggiungi" : null; if (IsNew) { ActivityModel.EstimatedTime = DateTime.Today.AddHours(DateTime.Now.Hour); ActivityModel.EstimatedEndtime = ActivityModel.EstimatedTime?.AddHours(1); ActivityModel.UserName = UserSession.User.Username; } _ = LoadData(); await LoadActivityType(); OriginalModel = ActivityModel.Clone(); } private async Task Save() { if (!CheckPreSave()) return; VisibleOverlay = true; StateHasChanged(); await SavePosition(); var response = await IntegryApiService.SaveActivity(ActivityModel); if (response == null) return; var newActivity = response.Last(); await ManageData.InsertOrUpdate(newActivity); await SaveAttached(newActivity.ActivityId!); SuccessAnimation = true; StateHasChanged(); await Task.Delay(1250); MudDialog.Close(newActivity); } private async Task SavePosition() { if (AttachedList is null) return; var positionTasks = AttachedList .Where(a => a.Type == AttachedDTO.TypeAttached.Position) .Select(async attached => { var position = new PositionDTO { Description = attached.Description, Lat = attached.Lat, Lng = attached.Lng }; ActivityModel.Position = await IntegryApiService.SavePosition(position); }); await Task.WhenAll(positionTasks); } private async Task SaveAttached(string activityId) { if (AttachedList is null) return; var uploadTasks = AttachedList .Where(a => a.FileContent is not null && a.Type != AttachedDTO.TypeAttached.Position) .Select(a => IntegryApiService.UploadFile(activityId, a.FileBytes, a.Name)); await Task.WhenAll(uploadTasks); } private bool CheckPreSave() { Snackbar.Clear(); if (!ActivityModel.ActivityTypeId.IsNullOrEmpty()) return true; Snackbar.Add("Tipo attività obbligatorio!", Severity.Error); return false; } private Task LoadData() { return Task.Run(async () => { IsLoading = true; await InvokeAsync(StateHasChanged); SelectedComessa = ActivityModel.Commessa; Users = await ManageData.GetTable(); if (!ActivityModel.UserName.IsNullOrEmpty()) SelectedUser = Users.FindLast(x => x.UserName.Equals(ActivityModel.UserName)); if (!IsNew && Id != null) ActivityFileList = await IntegryApiService.GetActivityFile(Id); ActivityResult = await ManageData.GetTable(); Clienti = await ManageData.GetClienti(new WhereCondContact { FlagStato = "A" }); Pros = await ManageData.GetProspect(); IsLoading = false; await InvokeAsync(StateHasChanged); }); } private async Task LoadActivityType() { if (ActivityModel.UserName is null) { ActivityType = []; return; } ActivityType = await ManageData.GetTable(x => x.UserName != null && x.UserName.Equals(ActivityModel.UserName) ); } private async Task LoadCommesse(string searchValue) { if (_lastLoadedCodAnag == ActivityModel.CodAnag && searchValue.IsNullOrEmpty()) return; if (ActivityModel.CodAnag == null) { Commesse = await ManageData.GetTable(x => x.CodJcom.ToUpper().Contains(searchValue.ToUpper()) || x.Descrizione.ToUpper().Contains(searchValue.ToUpper()) ); } else { Commesse = await ManageData.GetTable(x => x.CodAnag == ActivityModel.CodAnag); } Commesse = Commesse.OrderByDescending(x => x.CodJcom).ToList(); _lastLoadedCodAnag = ActivityModel.CodAnag; } private async Task> SearchCommesseAsync(string value, CancellationToken token) { await LoadCommesse(value); if (Commesse.IsNullOrEmpty()) return []; IEnumerable list; if (string.IsNullOrWhiteSpace(value)) { list = Commesse.OrderByDescending(x => x.CodJcom).Take(10); } else { list = Commesse .Where(x => (x.CodJcom.Contains(value, StringComparison.OrdinalIgnoreCase) || x.Descrizione.Contains(value, StringComparison.OrdinalIgnoreCase)) && (x.CodAnag == ActivityModel.CodAnag || ActivityModel.CodAnag == null)) .OrderByDescending(x => x.CodJcom) .Take(50); } return token.IsCancellationRequested ? [] : list; } private Task> SearchUtentiAsync(string value, CancellationToken token) { IEnumerable list; if (string.IsNullOrWhiteSpace(value)) { list = Users.OrderBy(u => u.FullName).Take(50); } else { list = Users .Where(x => x.UserName.Contains(value, StringComparison.OrdinalIgnoreCase) || x.FullName.Contains(value, StringComparison.OrdinalIgnoreCase)) .OrderBy(u => u.FullName) .Take(50); } return Task.FromResult(token.IsCancellationRequested ? [] : list); } private Task?> SearchCliente(string value, CancellationToken token) { if (string.IsNullOrWhiteSpace(value)) return Task.FromResult?>(null); var results = new List(); results.AddRange(Clienti .Where(x => (x.CodAnag != null && x.CodAnag.Contains(value, StringComparison.OrdinalIgnoreCase)) || x.RagSoc.Contains(value, StringComparison.OrdinalIgnoreCase)) .Select(x => $"{x.CodAnag} - {x.RagSoc}")); results.AddRange(Pros .Where(x => (x.CodPpro != null && x.CodPpro.Contains(value, StringComparison.OrdinalIgnoreCase)) || x.RagSoc.Contains(value, StringComparison.OrdinalIgnoreCase)) .Select(x => $"{x.CodPpro} - {x.RagSoc}")); return Task.FromResult?>(results); } private Task OnClienteChanged() { ActivityModel.CodJcom = null; if (string.IsNullOrWhiteSpace(ActivityModel.Cliente)) return Task.CompletedTask; var parts = ActivityModel.Cliente.Split('-', 2, StringSplitOptions.TrimEntries); if (parts.Length == 2) { ActivityModel.CodAnag = parts[0]; ActivityModel.Cliente = parts[1]; var isCliente = Clienti.FirstOrDefault(x => x.CodAnag != null && x.CodAnag.Equals(ActivityModel.CodAnag)); ActivityModel.TipoAnag = isCliente == null ? "P" : "C"; } OnAfterChangeValue(); return Task.CompletedTask; } private void OnCommessaSelectedAfter() { var com = SelectedComessa; if (com != null) { ActivityModel.CodJcom = com.CodJcom; ActivityModel.Commessa = com; if (com.CodAnag != null) { ActivityModel.CodAnag = com.CodAnag; ActivityModel.TipoAnag = com.TipoAnag; ActivityModel.Cliente = Clienti .Where(x => x.CodAnag != null && x.CodAnag.Equals(com.CodAnag)) .Select(x => x.RagSoc) .FirstOrDefault() ?? Pros .Where(x => x.CodPpro != null && x.CodPpro.Equals(com.CodAnag)) .Select(x => x.RagSoc) .FirstOrDefault(); } else { ActivityModel.CodAnag = null; ActivityModel.Cliente = null; ActivityModel.TipoAnag = null; } } else { ActivityModel.CodJcom = null; ActivityModel.Commessa = null; ActivityModel.CodAnag = null; ActivityModel.Cliente = null; ActivityModel.TipoAnag = null; } OnAfterChangeValue(); } private async Task OnUserSelectedAfter() { ActivityModel.UserName = SelectedUser?.UserName; await LoadActivityType(); OnAfterChangeValue(); } private async Task OnCommessaClear() { ActivityModel.Commessa = null; ActivityModel.CodJcom = null; StateHasChanged(); } private async Task OnUserClear() { ActivityModel.UserName = null; ActivityType = []; StateHasChanged(); } private void OnAfterChangeTimeBefore() { if (ActivityModel.EstimatedTime is not null) { ActivityModel.NotificationDate = ActivityModel.MinuteBefore switch { -1 => null, 0 => ActivityModel.EstimatedTime, _ => ActivityModel.EstimatedTime.Value.AddMinutes(ActivityModel.MinuteBefore * -1) }; } OnAfterChangeValue(); } private void OnAfterChangeValue() { if (!IsNew) LabelSave = !OriginalModel.Equals(ActivityModel) ? "Aggiorna" : null; if (ActivityModel.EstimatedTime is not null && (ActivityModel.EstimatedEndtime is null || ActivityModel.EstimatedEndtime <= ActivityModel.EstimatedTime)) { ActivityModel.EstimatedEndtime = ActivityModel.EstimatedTime.Value.AddHours(1); } StateHasChanged(); } private Task OnAfterChangeEsito() { OnAfterChangeValue(); return Task.CompletedTask; } private void OpenSelectEsito() { if (IsView) return; if (!IsNew && (ActivityModel.UserName is null || !ActivityModel.UserName.Equals(UserSession.User.Username))) { Snackbar.Add("Non puoi inserire un esito per un'attività che non ti è stata assegnata.", Severity.Info); return; } OpenEsito = !OpenEsito; StateHasChanged(); } private async Task DeleteActivity() { var result = await ConfirmDelete.ShowAsync(); if (result is true) { VisibleOverlay = true; StateHasChanged(); try { await IntegryApiService.DeleteActivity(ActivityModel.ActivityId); ActivityModel.Deleted = true; SuccessAnimation = true; StateHasChanged(); await Task.Delay(1250); MudDialog.Close(ActivityModel); } catch (Exception e) { VisibleOverlay = false; StateHasChanged(); Snackbar.Add("Impossibile cancellare l'attività", Severity.Error); } } } private void Duplica() { var activityCopy = ActivityModel.Clone(); activityCopy.ActivityId = null; MudDialog.Cancel(); Messenger.Send(new CopyActivityMessage(activityCopy)); } private async Task OpenAddAttached() { var result = await ModalHelpers.OpenAddAttached(Dialog, CanAddPosition); if (result is { Canceled: false, Data: not null } && result.Data.GetType() == typeof(AttachedDTO)) { var attached = (AttachedDTO)result.Data; if (attached.Type == AttachedDTO.TypeAttached.Position) CanAddPosition = false; AttachedList ??= []; AttachedList.Add(attached); LabelSave = "Aggiorna"; } } private void OnRemoveAttached(int index) { if (AttachedList is null || index < 0 || index >= AttachedList.Count) return; var attached = AttachedList[index]; if (attached.Type == AttachedDTO.TypeAttached.Position) CanAddPosition = true; AttachedList.RemoveAt(index); StateHasChanged(); } private async Task OpenAttached(string idAttached, string fileName) { try { var bytes = await IntegryApiService.DownloadFile(ActivityModel.ActivityId!, fileName); await AttachedService.OpenFile(bytes, fileName); } catch (Exception ex) { Snackbar.Clear(); Snackbar.Add("Impossibile aprire il file", Severity.Error); Console.WriteLine($"Errore durante l'apertura del file: {ex.Message}"); } } private async Task OpenAttached(AttachedDTO attached) { if (attached is { FileContent: not null, MimeType: not null }) { await AttachedService.OpenFile(attached.FileContent!, attached.Name); } else { Snackbar.Clear(); Snackbar.Add("Impossibile aprire il file", Severity.Error); } } private async Task DeleteAttach(ActivityFileDto file) { Snackbar.Clear(); if (ActivityFileList == null) return; try { ActivityFileList.Remove(file); StateHasChanged(); await IntegryApiService.DeleteFile(ActivityModel.ActivityId!, file.FileName); } catch (Exception ex) { ActivityFileList.Add(file); StateHasChanged(); Snackbar.Add("Impossibile eliminare il file", Severity.Error); Console.WriteLine($"Impossibile eliminare il file: {ex.Message}"); } finally { Snackbar.Add($"{file.FileName} eliminato con successo", Severity.Info); } } private void OpenPosition(AttachedDTO attached) { if (attached is { Lat: not null, Lng: not null }) { const string baseUrl = "https://www.google.it/maps/place/"; NavigationManager.NavigateTo( $"{baseUrl}{AdjustCoordinate(attached.Lat.Value)},{AdjustCoordinate(attached.Lng.Value)}" ); } else { Snackbar.Clear(); Snackbar.Add("Impossibile aprire la posizione", Severity.Error); } } private static string AdjustCoordinate(double coordinate) => coordinate.ToString(CultureInfo.InvariantCulture).Replace(",", "."); private async Task SuggestActivityDescription() { if (ActivityModel.ActivityTypeId == null) { Snackbar.Add("Indicare prima il tipo attività", Severity.Error); return; } VisibleOverlay = true; StateHasChanged(); _ = Task.Run(async () => { var activityDescriptions = await IntegryApiService.SuggestActivityDescription(ActivityModel.ActivityTypeId); var modal = ModalHelpers.OpenSuggestActivityDescription(Dialog, activityDescriptions); if (modal is { IsCanceled: false, Result: not null }) ActivityModel.ActivityDescription = modal.Result.Data!.ToString(); VisibleOverlay = false; await InvokeAsync(StateHasChanged); }); } }