@using salesbook.Shared.Core.Dto
@using salesbook.Shared.Core.Entity
@using salesbook.Shared.Core.Interface
@inject IManageDataService ManageData

<div class="user-card-card">
    <div class="user-card-left-section">
        <div class="user-card-body-section">
            <div class="title-section">
                <MudIcon @onclick="OpenUser" Color="Color.Primary" Icon="@(User.IsContact? Icons.Material.Filled.Person : Icons.Material.Filled.PersonOutline)" Size="Size.Large" />

                <div class="user-card-right-section">
                    <div class="user-card-title">
                        <MudText Typo="Typo.body1" @onclick="OpenUser" HtmlTag="h3">@User.RagSoc</MudText>
                        <MudIcon @onclick="ShowCommesse" Color="Color.Info" Icon="@Icons.Material.Outlined.Info" Size="Size.Medium"/>
                    </div>

                    <div class="user-card-subtitle @(ShowSectionCommesse ? "show" : "")">
                        @if (ShowSectionCommesse && IsLoading)
                        {
                            <MudProgressLinear Color="Color.Primary" Indeterminate="true" Class="my-7" />
                        }
                        else
                        {
                            @if (!Commesse.IsNullOrEmpty())
                            {
                                <div @onclick="OpenUser" class="container-commesse">
                                    @foreach (var commessa in Commesse!)
                                    {
                                        <div class="commessa">
                                            <span>@($"{commessa.CodJcom} - {commessa.Descrizione}")</span>
                                        </div>
                                    }
                                </div>
                            }
                            else
                            {
                                <div class="commessa">
                                    <span>Nessuna commessa presente</span>
                                </div>
                            }
                        }
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

@code {
    [Parameter] public ContactDTO User { get; set; } = new();

    private List<JtbComt>? Commesse { get; set; }
    private bool IsLoading { get; set; } = true;
    private bool ShowSectionCommesse { get; set; }

    private void OpenUser() =>
        NavigationManager.NavigateTo($"/User/{User.CodContact}/{User.IsContact}");

    private async Task ShowCommesse()
    {
        ShowSectionCommesse = !ShowSectionCommesse;

        if (ShowSectionCommesse)
        {
            Commesse = await ManageData.GetTable<JtbComt>(x => x.CodAnag.Equals(User.CodContact));
            IsLoading = false;
            StateHasChanged();
            return;
        }

        IsLoading = true;
    }

}