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

<h3>
    Database
</h3>

<p>
    Various utilities to assist in management of the SQLite database.
</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>
}

@using (Html.BeginForm(null, null, FormMethod.Get, new { action = $"{GlobalConfiguration.BasePath}{Context.Request.Path}" }))
{
    <table class="table fixedTable100 table-striped" border="0" width="100%" cellspacing="0" cellpadding="0">
        <thead>
            <tr>
                <td><strong>Name</strong></td>
                <td><strong>Version</strong></td>
                <td><strong>Size</strong></td>
                <td><strong>Action</strong></td>
            </tr>
        </thead>

        @foreach (var info in Model.Info)
        {
            <tr>
                <td>@info.Name</td>
                <td>@info.Version</td>
                <td>@NTDLS.Helpers.Formatters.FileSize((long)info.DatabaseSize)</td>
                <td>
                    @Html.Raw(TightWiki.Library.ConfirmActionHelper.GenerateSafeLink(GlobalConfiguration.BasePath, 
                        "This will optimize the database indexes and structure. Continue?",
                        "Optimize", $"/Admin/Database/Optimize/{info.Name}", Context.Request.Path.Value))
                    @Html.Raw(TightWiki.Library.ConfirmActionHelper.GenerateSafeLink(GlobalConfiguration.BasePath,
                        "This will remove empty space from the database, which can free space if a lot of data has been deleted. Continue?",
                        "Vacuum", $"/Admin/Database/Vacuum/{info.Name}", Context.Request.Path.Value))
                    @Html.Raw(TightWiki.Library.ConfirmActionHelper.GenerateSafeLink(GlobalConfiguration.BasePath,
                        "This will check the database integrity and validate all foreign keys. Continue?",
                        "Verify", $"/Admin/Database/Verify/{info.Name}", Context.Request.Path.Value))
                </td>
            </tr>
        }
    </table>
}
