@page "/User/{CodAnag}"
@attribute [Authorize]
@using salesbook.Shared.Components.Layout
@using salesbook.Shared.Core.Entity
@using salesbook.Shared.Core.Interface
@using salesbook.Shared.Components.Layout.Spinner
@inject IManageDataService ManageData

<HeaderLayout BackTo="Indietro" Back="true" BackOnTop="true" Title="" ShowProfile="false"/>

@if (IsLoading)
{
    <SpinnerLayout FullScreen="true"/>
}
else
{
    <div class="container content">
        <div class="container-primary-info">
            <div class="section-primary-info">
                <MudAvatar Style="height: 70px; width: 70px; font-size: 2rem; font-weight: bold" Color="Color.Secondary">
                    @UtilityString.ExtractInitials(Anag.RagSoc)
                </MudAvatar>

                <div class="personal-info">
                    <span class="info-nome">@Anag.RagSoc</span>
                    @if (UserSession.User.KeyGroup is not null)
                    {
                        <span class="info-section">@Anag.Indirizzo</span>
                        <span class="info-section">@($"{Anag.Cap} - {Anag.Citta} ({Anag.Prov})")</span>
                    }
                </div>
            </div>

            <div class="divider"></div>

            <div class="section-info">
                <div class="section-personal-info">
                    <div>
                        <span class="info-title">Telefono</span>
                        <span class="info-text">
                            @if (string.IsNullOrEmpty(Anag.Telefono))
                            {
                                @("Nessuna mail configurata")
                            }
                            else
                            {
                                @Anag.Telefono
                            }
                        </span>
                    </div>
                </div>

                <div class="section-personal-info">
                    <div>
                        <span class="info-title">E-mail</span>
                        <span class="info-text">
                            @if (string.IsNullOrEmpty(Anag.EMail))
                            {
                                @("Nessuna mail configurata")
                            }
                            else
                            {
                                @Anag.EMail
                            }
                        </span>
                    </div>
                </div>
            </div>
        </div>

        @if (PersRif is { Count: > 0 })
        {
            <div class="container-pers-rif">
                <Virtualize Items="PersRif" Context="person">
                    @{
                        var index = PersRif.IndexOf(person);
                        var isLast = index == PersRif.Count - 1;
                    }
                    <ContactCard Contact="person" />
                    @if (!isLast)
                    {
                        <div class="divider"></div>
                    }
                </Virtualize>
            </div>
        }

        <div class="container-button">
            <MudButton Class="button-settings infoText"
                       FullWidth="true"
                       Size="Size.Medium"
                       Variant="Variant.Outlined">
                Aggiungi contatto
            </MudButton>
        </div>
    </div>
}

@code {
    [Parameter] public string CodAnag { get; set; }

    private AnagClie Anag { get; set; } = new();
    private List<VtbCliePersRif>? PersRif { get; set; }

    private bool IsLoading { get; set; } = true;

    protected override async Task OnInitializedAsync()
    {
        await LoadData();
    }

    private async Task LoadData()
    {
        Anag = (await ManageData.GetTable<AnagClie>(x => x.CodAnag.Equals(CodAnag))).Last();
        PersRif = await ManageData.GetTable<VtbCliePersRif>(x => x.CodAnag.Equals(Anag.CodAnag));

        IsLoading = false;
        StateHasChanged();
    }

}