@using System.Globalization @using System.Text.RegularExpressions @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.Messages.Activity.Copy @inject IManageDataService ManageData @inject INetworkService NetworkService @inject IIntegryApiService IntegryApiService @inject IMessenger Messenger @inject IDialogService Dialog @inject IAttachedService AttachedService
Commessa @if (Commesse.IsNullOrEmpty()) { Nessuna commessa presente } else { @foreach (var com in Commesse) { @($"{com.CodJcom} - {com.Descrizione}") } }
Inizio
Fine
Avviso
Assegnata a @foreach (var user in Users) { @user.FullName }
Tipo @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 (ActivityFileList != null) { foreach (var file in ActivityFileList) { @file.FileName } }
Aggiungi allegati @if (!IsNew) {
Duplica
Elimina }
Confermi la cancellazione dell'attività corrente? Cancella Salva Vuoi creare un promemoria? Crea
@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 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; } private MudMessageBox ConfirmMemo { get; set; } private MudMessageBox AddNamePosition { get; set; } //Attached private List? AttachedList { get; set; } private string? NamePosition { get; set; } private bool CanAddPosition { get; set; } = true; 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; _ = LoadData(); await LoadCommesse(); if (IsNew) { ActivityModel.EstimatedTime = DateTime.Today.Add(TimeSpan.FromHours(DateTime.Now.Hour)); ActivityModel.EstimatedEndtime = DateTime.Today.Add(TimeSpan.FromHours(DateTime.Now.Hour) + TimeSpan.FromHours(1)); ActivityModel.UserName = UserSession.User.Username; } 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 != null) { foreach (var attached in AttachedList) { if (attached.Type != AttachedDTO.TypeAttached.Position) continue; var position = new PositionDTO { Description = attached.Description, Lat = attached.Lat, Lng = attached.Lng }; ActivityModel.Position = await IntegryApiService.SavePosition(position); } } } private async Task SaveAttached(string activityId) { if (AttachedList != null) { foreach (var attached in AttachedList) { if (attached.FileContent is not null && attached.Type != AttachedDTO.TypeAttached.Position) { await IntegryApiService.UploadFile(activityId, attached.FileBytes, attached.Name); } } } } private bool CheckPreSave() { Snackbar.Clear(); if (!ActivityModel.ActivityTypeId.IsNullOrEmpty()) return true; Snackbar.Add("Tipo attività obbligatorio!", Severity.Error); return false; } private async Task LoadData() { if (!IsNew && Id != null) { ActivityFileList = await IntegryApiService.GetActivityFile(Id); } Users = await ManageData.GetTable(); ActivityResult = await ManageData.GetTable(); Clienti = await ManageData.GetClienti(new WhereCondContact {FlagStato = "A"}); Pros = await ManageData.GetProspect(); } private async Task LoadActivityType() { if (ActivityModel.UserName is null) ActivityType = []; ActivityType = await ManageData.GetTable(x => x.UserName != null && x.UserName.Equals(ActivityModel.UserName) ); } private async Task LoadCommesse() => Commesse = await ManageData.GetTable(x => x.CodAnag != null && x.CodAnag.Equals(ActivityModel.CodAnag)); private async Task?> SearchCliente(string value, CancellationToken token) { if (string.IsNullOrEmpty(value)) return null; var listToReturn = new List(); listToReturn.AddRange( Clienti.Where(x => x.RagSoc.Contains(value, StringComparison.OrdinalIgnoreCase)).Select(x => $"{x.CodAnag} - {x.RagSoc}") ); listToReturn.AddRange( Pros.Where(x => x.RagSoc.Contains(value, StringComparison.OrdinalIgnoreCase)).Select(x => $"{x.CodPpro} - {x.RagSoc}") ); return listToReturn; } private async Task OnClienteChanged() { ActivityModel.CodJcom = null; if (ActivityModel.Cliente.IsNullOrEmpty()) return; var match = Regex.Match(ActivityModel.Cliente!, @"^\s*(\S+)\s*-\s*(.*)$"); if (!match.Success) return; ActivityModel.CodAnag = match.Groups[1].Value; ActivityModel.Cliente = match.Groups[2].Value; await LoadCommesse(); OnAfterChangeValue(); } private async Task OnCommessaChanged() { ActivityModel.Commessa = (await ManageData.GetTable(x => x.CodJcom.Equals(ActivityModel.CodJcom))).Last().Descrizione; OnAfterChangeValue(); } private async Task OnUserChanged() { await LoadActivityType(); OnAfterChangeValue(); } private void OnAfterChangeValue() { if (!IsNew) { LabelSave = !OriginalModel.Equals(ActivityModel) ? "Aggiorna" : null; } if (ActivityModel.EstimatedEndtime <= ActivityModel.EstimatedTime) { ActivityModel.EstimatedEndtime = ActivityModel.EstimatedTime.Value.AddHours(1); } } private async Task OnAfterChangeEsito() { OnAfterChangeValue(); // var result = await ConfirmMemo.ShowAsync(); // if (result is true) // OpenAddMemo = !OpenAddMemo; } private void OpenSelectEsito() { 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; var resultNamePosition = await AddNamePosition.ShowAsync(); if (resultNamePosition is true) attached.Description = NamePosition; attached.Name = NamePosition!; } 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 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(",", "."); }