@page "/guests"
@rendermode InteractiveServer
@using Microsoft.AspNetCore.Authorization
@attribute [Authorize(Roles = "Admin,Maintainer")]

<div class="d-flex flex-column flex-md-row align-items-md-center justify-content-between gap-3 mb-4">
    <div>
        <h1 class="h2 mb-1">Guest management</h1>
        <p class="text-secondary mb-0">Add, edit, delete and filter guest entries.</p>
    </div>

    <form method="get" action="/guests" class="w-100 w-md-auto">
        <label class="form-label" for="guest-search">Search</label>
        <div class="d-flex gap-2">
            <input id="guest-search" class="form-control" name="search" value="@SearchTerm" placeholder="Name, note or user" />
            <button type="submit" class="btn btn-outline-primary">
                Search
            </button>
            @if (!string.IsNullOrWhiteSpace(SearchTerm))
            {
                <a class="btn btn-outline-secondary" href="/guests">Clear</a>
            }
        </div>
    </form>
</div>

@if (!string.IsNullOrWhiteSpace(ErrorMessage))
{
    <div class="alert alert-warning" role="alert">@ErrorMessage</div>
}

<div class="guest-card-grid">
    <article class="card shadow-sm">
        <div class="card-body">
            <h2 class="h5 card-title">Add guest</h2>
            <form method="post" action="/guests/add">
                <div class="mb-3">
                    <label class="form-label" for="new-guest-name">Name</label>
                    <input id="new-guest-name" class="form-control" name="name" />
                </div>

                <div class="mb-3">
                    <label class="form-label" for="new-guest-notes">Notes</label>
                    <textarea id="new-guest-notes" class="form-control" rows="3" name="notes"></textarea>
                </div>

                <button type="submit" class="btn btn-primary w-100">
                    Add guest
                </button>
            </form>
        </div>
    </article>

    @if (GuestEntries is null)
    {
        <article class="card shadow-sm">
            <div class="card-body">
                <p class="text-secondary mb-0">Loading guest list...</p>
            </div>
        </article>
    }
    else if (GuestEntries.Count == 0)
    {
        <article class="card shadow-sm">
            <div class="card-body">
                <p class="text-secondary mb-0">No guests have been added yet.</p>
            </div>
        </article>
    }
    else if (!FilteredGuestEntries.Any())
    {
        <article class="card shadow-sm">
            <div class="card-body">
                <p class="text-secondary mb-0">No guests match the current filter.</p>
            </div>
        </article>
    }
    else
    {
        @foreach (var guest in FilteredGuestEntries)
        {
            <article class="card shadow-sm">
                <div class="card-body d-flex flex-column">
                    @if (EditingGuestId == guest.Id)
                    {
                        <h2 class="h5 card-title">Edit guest</h2>

                        <form class="d-flex flex-column flex-grow-1" method="post" action="/guests/update">
                            <input type="hidden" name="id" value="@guest.Id" />

                            <div class="mb-3">
                                <label class="form-label">Name</label>
                                <input class="form-control" name="name" value="@guest.Name" />
                            </div>

                            <div class="mb-3">
                                <label class="form-label">Notes</label>
                                <textarea class="form-control" rows="3" name="notes">@guest.Notes</textarea>
                            </div>

                            <div class="d-flex gap-2 mt-auto">
                                <button type="submit" class="btn btn-primary">
                                    Save
                                </button>

                                <a class="btn btn-outline-secondary" href="/guests">
                                    Cancel
                                </a>
                            </div>
                        </form>
                    }
                    else
                    {
                        <h2 class="h5 card-title">@guest.Name</h2>

                        @if (!string.IsNullOrWhiteSpace(guest.Notes))
                        {
                            <p class="card-text">@guest.Notes</p>
                        }
                        else
                        {
                            <p class="card-text text-secondary">No notes.</p>
                        }

                        <p class="small text-secondary mt-auto mb-3">
                            Added by @guest.AddedByUsername<br />
                            @guest.CreatedAt.ToLocalTime().ToString("g")
                        </p>

                        <div class="d-flex gap-2">
                            <a class="btn btn-outline-primary btn-sm" href="/guests?edit=@guest.Id">
                                Edit
                            </a>

                            <form method="post" action="/guests/delete">
                                <input type="hidden" name="id" value="@guest.Id" />
                                <button type="submit" class="btn btn-outline-danger btn-sm">
                                    Delete
                                </button>
                            </form>
                        </div>
                    }
                </div>
            </article>
        }
    }
</div>
