@model TightWiki.Models.ViewModels.Admin.PagesViewModel
@using TightWiki.Library
@using TightWiki.Models
@{
    Layout = "/Views/Shared/_Layout.cshtml";
    var sessionState = ViewData["SessionState"] as TightWiki.SessionState ?? throw new Exception("Wiki State Context cannot be null.");
}

<h3>
    Pages
</h3>

<p>
    All pages contained in the wiki.<br /><br />
</p>

@if (!string.IsNullOrEmpty(Model.ErrorMessage))
{
    <div class="alert alert-danger">@Html.Raw(Model.ErrorMessage)</div>
}
@if (!string.IsNullOrEmpty(Model.SuccessMessage))
{
    <div class="alert alert-success">@Html.Raw(Model.SuccessMessage)</div>
}

@Html.Raw(ConfirmActionHelper.GenerateWarnLink(GlobalConfiguration.BasePath,
    "This will rebuild all pages in the wiki. This could take some time! Continue?",
    "Rebuild All Pages", "/Admin/RebuildAllPages", Context.Request.Path.Value))

@Html.Raw(ConfirmActionHelper.GenerateWarnLink(GlobalConfiguration.BasePath,
    "This will compile all pages in the wiki and store them in cache. This could take some time! Continue?",
    "Pre-Cache All Pages", "/Admin/PreCacheAllPages", Context.Request.Path.Value))

@Html.Raw(ConfirmActionHelper.GenerateDangerLink(GlobalConfiguration.BasePath, 
    "Truncating the page revisions will delete all the revision history for all pages and page attachments. This will leave only the most current revision of all objects. This is generally considered a big deal! Continue?",
    "Truncate Page Revisions", "/Admin/TruncatePageRevisions", Context.Request.Path.Value))
<br />
<br />

@using (Html.BeginForm(null, null, FormMethod.Get, new { action = $"{GlobalConfiguration.BasePath}{Context.Request.Path}" }))
{
    <div class="container">
        <div class="d-flex justify-content-end mb-4">
            <div class="flex-grow-1 me-2">
                @Html.TextBoxFor(x => x.SearchString, new { @class = "form-control" })
            </div>
            <button type="submit" class="btn btn-primary">Search</button>
        </div>
    </div>
    <br />

    @if (Model.Pages.Count > 0)
    {
        <table class="table fixedTable100 table-striped" border="0" cellspacing="0" cellpadding="0">
            <thead>
                <tr>
                    <td><strong><a href="?@QueryStringConverter.OrderHelper(sessionState, "Name")">Name</a></strong></td>
                    <td><strong><a href="?@QueryStringConverter.OrderHelper(sessionState, "Revision")">Revision</a></strong></td>
                    <td><strong>Deleted Revisions</strong></td>
                    <td><strong><a href="?@QueryStringConverter.OrderHelper(sessionState, "ModifiedBy")">Modified By</a></strong></td>
                    <td><strong><a href="?@QueryStringConverter.OrderHelper(sessionState, "ModifiedDate")">Modified Date</a></strong></td>
                    <td><strong>Action</strong></td>
                </tr>
            </thead>

            @foreach (var p in Model.Pages)
            {
                <tr>
                    <td>
                        @if (string.IsNullOrEmpty(@p.Namespace) == false)
                        {
                            <text><a href="@GlobalConfiguration.BasePath/Admin/Namespace/@p.Namespace">@p.Namespace</a> :: </text>
                        } <a href="@GlobalConfiguration.BasePath/@p.Navigation">@p.Title</a>
                    </td>
                    <td><a href="@GlobalConfiguration.BasePath/Admin/PageRevisions/@p.Navigation">@p.Revision</a></td>
                    <td><a href="@GlobalConfiguration.BasePath/Admin/DeletedPageRevisions/@p.Id">@p.DeletedRevisionCount</a></td>
                    <td>@p.ModifiedByUserName</td>
                    <td>@p.ModifiedDate</td>
                    <td>
                        @Html.Raw(ConfirmActionHelper.GenerateDangerLink(GlobalConfiguration.BasePath, 
                            $"Deleting \"{p.Name}\" will move the page, all {p.Revision} revisions and associated file attachments to the deletion queue. This action can only be undone by an administrator or moderator. Continue?",
                            "Delete", "/Admin/DeletePage/" + @p.Id, Context.Request.Path.Value))
                    </td>
                </tr>
            }
        </table>

        @Html.Raw(PageSelectorGenerator.Generate(Context.Request.QueryString, Model.PaginationPageCount))
    }
    else
    {
        <div class="d-flex small text-muted mb-0">
            <strong>
                Either there are no pages or your search criteria returned no results.
            </strong>
        </div>
    }
}

<script>
    window.onload = function () {
        document.getElementById("SearchString").focus();
    }
</script>
