@model SceneOccupancyEditorViewModel
@{
    var selectedPlanId = Model.FloorPlanID;
    var locationOptions = Model.Locations
        .Where(x => !selectedPlanId.HasValue || x.FloorPlanID == selectedPlanId.Value)
        .OrderBy(x => x.FloorSortOrder)
        .ThenBy(x => x.FloorName)
        .ThenBy(x => x.LocationPath)
        .ToList();
}

<form asp-controller="Scenes" asp-action="SaveOccupancy" method="post" class="scene-occupancy-form" data-scene-occupancy-form data-editor-url="@Url.Action("OccupancyEditor", "Scenes")">
    <input type="hidden" name="SceneID" value="@Model.SceneID" />
    <div class="scene-occupancy-status" data-scene-occupancy-status>Saved</div>
    <div class="row g-2">
        <div class="col-sm-6">
            <label class="form-label" for="OccupancyFloorPlanID">Floor Plan</label>
            <select class="form-select form-select-sm" id="OccupancyFloorPlanID" name="FloorPlanID" data-occupancy-plan-select>
                <option value="">No floor plan</option>
                @foreach (var option in Model.FloorPlanOptions)
                {
                    <option value="@option.Value" selected="@(option.Value == Model.FloorPlanID?.ToString())">@option.Text</option>
                }
            </select>
        </div>
        <div class="col-sm-6">
            <label class="form-label" for="OccupancyInitialFloorID">Initial Floor</label>
            <select class="form-select form-select-sm" id="OccupancyInitialFloorID" name="InitialFloorPlanFloorID" data-occupancy-floor-select>
                <option value="">Use first floor</option>
                @foreach (var floor in Model.Floors.Where(x => !selectedPlanId.HasValue || x.FloorPlanID == selectedPlanId.Value))
                {
                    <option value="@floor.FloorPlanFloorID" selected="@(floor.FloorPlanFloorID == Model.InitialFloorPlanFloorID)">@floor.Name</option>
                }
            </select>
        </div>
    </div>

    @if (!Model.FloorPlanOptions.Any())
    {
        <p class="muted mt-2 mb-0">No floor plans exist for this project yet.</p>
    }
    else
    {
        <div class="scene-occupancy-grid mt-3">
            <section>
                <h4>Characters</h4>
                @if (!Model.Characters.Any())
                {
                    <p class="muted">No characters appear in this scene yet.</p>
                }
                else
                {
                    @for (var i = 0; i < Model.Characters.Count; i++)
                    {
                        <div class="scene-occupancy-row">
                            <input type="hidden" name="Characters[@i].CharacterID" value="@Model.Characters.ElementAt(i).CharacterID" />
                            <span>@Model.Characters.ElementAt(i).CharacterName</span>
                            <select class="form-select form-select-sm" name="Characters[@i].LocationID">
                                <option value="">Not placed</option>
                                @foreach (var location in locationOptions)
                                {
                                    <option value="@location.LocationID" selected="@(location.LocationID == Model.Characters.ElementAt(i).LocationID)">@location.FloorName &gt; @location.LocationPath</option>
                                }
                            </select>
                        </div>
                    }
                }
            </section>

            <section>
                <h4>Assets</h4>
                @if (!Model.Assets.Any())
                {
                    <p class="muted">No story assets appear in this scene yet.</p>
                }
                else
                {
                    @for (var i = 0; i < Model.Assets.Count; i++)
                    {
                        <div class="scene-occupancy-row">
                            <input type="hidden" name="Assets[@i].StoryAssetID" value="@Model.Assets.ElementAt(i).StoryAssetID" />
                            <span>@Model.Assets.ElementAt(i).AssetName</span>
                            <select class="form-select form-select-sm" name="Assets[@i].LocationID">
                                <option value="">Not placed</option>
                                @foreach (var location in locationOptions)
                                {
                                    <option value="@location.LocationID" selected="@(location.LocationID == Model.Assets.ElementAt(i).LocationID)">@location.FloorName &gt; @location.LocationPath</option>
                                }
                            </select>
                        </div>
                    }
                }
            </section>
        </div>

        <button class="btn btn-primary btn-sm mt-3" type="submit">Save occupancy</button>
    }
</form>
