@model FloorPlanListViewModel
@{
    ViewData["Title"] = "Floor Plans";
    ViewData["ProjectSection"] = "Floor Plans";
}

<nav class="breadcrumb-trail" aria-label="Breadcrumb">
    <a asp-controller="Projects" asp-action="Index">Projects</a>
    <a asp-controller="Projects" asp-action="Details" asp-route-id="@Model.Project.ProjectID">@Model.Project.ProjectName</a>
    <span>Floor Plans</span>
</nav>

<partial name="_ProjectSectionNav" model="Model.Project" />

<div class="page-heading compact">
    <div>
        <p class="eyebrow">Location plans</p>
        <h1>@Model.Project.ProjectName <help-icon key="floorPlans.overview" /></h1>
    </div>
</div>

@if (TempData["FloorPlanMessage"] is string message)
{
    <div class="alert alert-success">@message</div>
}

<section class="edit-panel">
    <h2>Create floor plan <help-icon key="floorPlans.creatingFloorsAndRooms" /></h2>
    <form asp-action="Create" method="post" class="row g-3">
        <input asp-for="NewFloorPlan.ProjectID" type="hidden" name="ProjectID" />
        <div class="col-md-4">
            <label asp-for="NewFloorPlan.Name" class="form-label"></label>
            <input asp-for="NewFloorPlan.Name" class="form-control" name="Name" />
        </div>
        <div class="col-md-4">
            <label asp-for="NewFloorPlan.Description" class="form-label"></label>
            <input asp-for="NewFloorPlan.Description" class="form-control" name="Description" />
        </div>
        <div class="col-md-2">
            <label class="form-label">Root Location <help-icon key="floorPlans.fields.rootLocation" mode="inline" /></label>
            <select asp-for="NewFloorPlan.LocationID" asp-items="Model.LocationOptions" class="form-select" name="LocationID"></select>
        </div>
        <div class="col-md-2 d-flex align-items-end">
            <button class="btn btn-primary w-100" type="submit">Create</button>
        </div>
    </form>
</section>

@if (!Model.FloorPlans.Any())
{
    <section class="empty-panel">
        <h2>No floor plans yet</h2>
        <p>Create a plan, then arrange existing locations as rooms, corridors, halls, gardens, or other spaces.</p>
    </section>
}
else
{
    <section class="edit-panel">
        <div class="floor-plan-list">
            @foreach (var floorPlan in Model.FloorPlans)
            {
                <article class="floor-plan-list-item">
                    <div>
                        <h2><a asp-action="Edit" asp-route-id="@floorPlan.FloorPlanID">@floorPlan.Name</a></h2>
                        @if (!string.IsNullOrWhiteSpace(floorPlan.Description))
                        {
                            <p>@floorPlan.Description</p>
                        }
                        <p class="muted">@floorPlan.FloorCount floor(s), @floorPlan.BlockCount placed block(s)</p>
                        <p class="muted">Location: @(string.IsNullOrWhiteSpace(floorPlan.LocationPath) ? "Not linked yet" : floorPlan.LocationPath)</p>
                    </div>
                    <div class="button-row compact-buttons">
                        <a class="btn btn-outline-secondary btn-sm" asp-action="Edit" asp-route-id="@floorPlan.FloorPlanID">Open editor</a>
                        <button class="btn btn-outline-danger btn-sm" type="button" data-bs-toggle="modal" data-bs-target="#delete-floor-plan-@floorPlan.FloorPlanID">Delete</button>
                    </div>
                </article>
            }
        </div>
    </section>

    @foreach (var floorPlan in Model.FloorPlans)
    {
        <div class="modal fade" id="delete-floor-plan-@floorPlan.FloorPlanID" tabindex="-1" aria-labelledby="delete-floor-plan-title-@floorPlan.FloorPlanID" aria-hidden="true">
            <div class="modal-dialog modal-dialog-centered">
                <div class="modal-content plotline-confirm-modal">
                    <div class="modal-header">
                        <h2 class="modal-title fs-5" id="delete-floor-plan-title-@floorPlan.FloorPlanID">Delete floor plan?</h2>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                    </div>
                    <div class="modal-body">
                        <p>Delete <strong>@floorPlan.Name</strong>?</p>
                        <p>This deletes its floors and placed blocks. Linked Locations will not be deleted.</p>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">Cancel</button>
                        <form asp-action="DeletePlan" method="post" data-no-delete-confirm="true">
                            <input type="hidden" name="id" value="@floorPlan.FloorPlanID" />
                            <input type="hidden" name="projectId" value="@Model.Project.ProjectID" />
                            <button class="btn btn-danger" type="submit">Delete floor plan</button>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    }
}
