@page "/credentials/{id:guid}/delete"
@inherits MainBase
@inject CredentialService CredentialService

<LayoutPageTitle>Delete credentials entry</LayoutPageTitle>

<PageHeader
    BreadcrumbItems="@BreadcrumbItems"
    Title="Delete credentials"
    Description="You can delete a credentials entry below.">
</PageHeader>

@if (IsLoading)
{
    <LoadingIndicator />
}
else
{
    <div class="p-4 mb-4 bg-white border border-gray-200 rounded-lg shadow-sm 2xl:col-span-2 dark:border-gray-700 sm:p-6 dark:bg-gray-800">
        <AlertMessageError Message="Note: removing this login entry is permanent and cannot be undone." />
        <h3 class="mb-4 text-xl font-semibold dark:text-white">Credential entry</h3>
        <div class="mb-4">
            <label class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Id</label>
            <div class="text-gray-900 dark:text-white">@Id</div>
        </div>
        <div class="mb-4">
            <label class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Service name</label>
            <div class="text-gray-900 dark:text-white">@Obj?.Service.Name</div>
        </div>
        <button @onclick="DeleteConfirm" class="text-white bg-red-600 hover:bg-red-800 focus:ring-4 focus:ring-red-300 font-medium rounded-lg text-sm inline-flex items-center px-3 py-2.5 text-center mr-2 dark:focus:ring-red-900">
            Yes, I'm sure
        </button>

        <button @onclick="Cancel" class="text-gray-900 bg-white hover:bg-gray-100 focus:ring-4 focus:ring-primary-300 border border-gray-200 font-medium inline-flex items-center rounded-lg text-sm px-3 py-2.5 text-center dark:bg-gray-800 dark:text-gray-400 dark:border-gray-600 dark:hover:text-white dark:hover:bg-gray-700 dark:focus:ring-gray-700" data-drawer-hide="drawer-delete-product-default">
            No, cancel
        </button>
    </div>
}

@code {
    /// <summary>
    /// Gets or sets the login ID.
    /// </summary>
    [Parameter]
    public Guid Id { get; set; }

    private bool IsLoading { get; set; } = true;
    private Credential? Obj { get; set; }

    /// <inheritdoc />
    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();
        BreadcrumbItems.Add(new BreadcrumbItem { Url = "credentials/" + Id, DisplayName = "View credentials entry" });
        BreadcrumbItems.Add(new BreadcrumbItem { DisplayName = "Delete credential" });
    }

    /// <inheritdoc />
    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        await base.OnAfterRenderAsync(firstRender);

        if (firstRender)
        {
            // Load existing Obj, retrieve from service
            Obj = await CredentialService.LoadEntryAsync(Id);

            // Hide loading spinner
            IsLoading = false;

            // Force re-render invoke so the charts can be rendered
            StateHasChanged();
        }
    }

    private async void DeleteConfirm()
    {
        if (Obj is null)
        {
            GlobalNotificationService.AddErrorMessage("Error deleting. Credentials entry not found.", true);
            return;
        }

        GlobalLoadingSpinner.Show();
        await CredentialService.SoftDeleteEntryAsync(Id);
        GlobalNotificationService.AddSuccessMessage("Credentials entry successfully deleted.");
        GlobalLoadingSpinner.Hide();

        NavigationManager.NavigateTo("/");
    }

    private void Cancel()
    {
        NavigationManager.NavigateTo("/credentials/" + Id);
    }
}
