@using SteUp.Shared.Components.Layout.Overlay
@using SteUp.Shared.Core.Dto
@using SteUp.Shared.Core.Interface.LocalDb
@inject IIspezioniService IspezioniService

<div class="shop-card ripple-container" @onclick="StartInspection">
    <div class="shop-body-section">
        <div class="title-section">
            <MudText Class="shop-title" Typo="Typo.subtitle1">
                <b>@PuntoVendita.CodMdep</b> - @PuntoVendita.Descrizione
            </MudText>
        </div>

        @if (!PuntoVendita.Indirizzo.IsNullOrEmpty())
        {
            <div class="subtitle-section">
                <MudText Class="shop-title" Typo="Typo.subtitle1">
                    @PuntoVendita.Indirizzo
                </MudText>
            </div>
            <div class="sub-info-section">
                <MudChip T="string" Icon="@Icons.Material.Rounded.LocationCity" Size="Size.Small" Color="Color.Default">
                    @PuntoVendita.Citta
                </MudChip>
                <MudChip T="string" Size="Size.Small" Color="Color.Default">
                    @PuntoVendita.Cap
                </MudChip>
                <MudChip T="string" Size="Size.Small" Color="Color.Default">
                    @PuntoVendita.Provincia
                </MudChip>
            </div>
        }
    </div>
</div>

<SpinnerOverlay VisibleOverlay="VisibleOverlay"/>

@code {
    [Parameter] public PuntoVenditaDto PuntoVendita { get; set; } = null!;

    private bool VisibleOverlay { get; set; }
    
    private async Task StartInspection()
    {
        VisibleOverlay = true;
        StateHasChanged();
        
        SteupDataService.InspectionPageState.Ispezione = await IspezioniService.GetOrCreateIspezioneAsync(
            PuntoVendita.CodMdep!,
            DateTime.Now,
            UserSession.User.Username
        );
        
        VisibleOverlay = false;
        StateHasChanged();

        NavigationManager.NavigateTo("/ispezione");
    }

}