@using TightWiki;
@using System.Text.Encodings.Web
@using TightWiki.Models
@{
    var sessionState = ViewData["SessionState"] as SessionState ?? throw new Exception("Wiki State Context cannot be null.");
    string navBarClass = $"navbar navbar-expand-sm navbar-toggleable-sm {(TightWiki.Models.GlobalConfiguration.FixedMenuPosition ? "fixed-top" : "")} navbar-light bg-white border-bottom box-shadow mb-3";
}

<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>@(sessionState.PageTitle ?? sessionState.Page.Name)</title>

    <!-- Syntax Highlighter:Begin -->
    <script src="@Url.Content("~/syntax/highlight.min.js")"></script>
    <script>hljs.highlightAll();</script>
    <!-- Syntax Highlighter:End -->
    @if (string.IsNullOrWhiteSpace(TightWiki.Models.GlobalConfiguration.HTMLHeader) == false)
    {
        <!-- HTMLHeader:Begin -->
        @Html.Raw(TightWiki.Models.GlobalConfiguration.HTMLHeader)
        <!-- HTMLHeader:Begin -->
    }

    @if (TightWiki.Models.GlobalConfiguration.IncludeWikiDescriptionInMeta)
    {
        <!-- PageDescription:Begin -->
        <meta name="description" content="@sessionState.Page.Description">
        <!-- PageDescription:Begin -->
    }

    @if (TightWiki.Models.GlobalConfiguration.IncludeWikiTagsInMeta)
    {
        <!-- PageTags:Begin -->
        <meta name="keywords" content="@sessionState.PageTags">
        <!-- PageTags:Begin -->
    }

    <script src="@Url.Content("~/lib/jquery/dist/jquery.min.js")"></script>
    <script src="@Url.Content("~/lib/bootstrap/dist/js/bootstrap.bundle.min.js")"></script>

    <!-- Theme:Begin -->
    @foreach (var file in sessionState.UserTheme.Files)
    {
        <link rel="stylesheet" href="@Url.Content($"~{file}")" />
    }
    <!-- Theme:End-->

    <link rel="stylesheet" href="@Url.Content("~/css/base.css")" />

    <script src="@Url.Content("~/js/site.js")" asp-append-version="true"></script>

    <link id="themeStylesheet" rel="stylesheet" href="" />
    <script>
        $(document).ready(function () {
            localStorage.setItem('theme', 'dark');
            $('nav').removeClass('navbar-light bg-white').addClass('@sessionState.UserTheme.ClassNavBar');
            $('a.nav-link').removeClass('text-dark').addClass('@sessionState.UserTheme.ClassNavLink');
            $('a.dropdown-item').removeClass('text-dark').addClass('@sessionState.UserTheme.ClassDropdown');
            $('span.glyphicon').removeClass('text-dark').addClass('@sessionState.UserTheme.ClassBranding');
        });
    </script>

    @RenderSection("Scripts", required: false)
</head>

<body>
    <header>
        <nav class="@navBarClass">
            <div class="container">
                <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target=".navbar-collapse" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
                    <span class="navbar-toggler-icon"></span>
                </button>
                <a class="navbar-brand" href="@GlobalConfiguration.BasePath/"><span class="glyphicon text-dark"><img src="@Url.Content(TightWiki.Models.GlobalConfiguration.BrandImageSmall)" /> @TightWiki.Models.GlobalConfiguration.Name</span></a>
                <div class="navbar-collapse collapse d-sm-inline-flex flex-sm-row-reverse">
                    <ul class="navbar-nav flex-grow-1">
                        @foreach (TightWiki.Models.DataModels.MenuItem item in TightWiki.Models.GlobalConfiguration.MenuItems)
                        {
                            if (item.Link.StartsWith("http://", StringComparison.InvariantCultureIgnoreCase) || item.Link.StartsWith("https://", StringComparison.InvariantCultureIgnoreCase))
                            {
                                <li class="nav-item"><a class="nav-link text-dark" href="@item.Link">@item.Name</a></li>
                            }
                            <li class="nav-item"><a class="nav-link text-dark" href="@GlobalConfiguration.BasePath@item.Link">@item.Name</a></li>
                        }

                        @if (sessionState.CanCreate == true
                        || (sessionState.ShouldCreatePage == true && sessionState.CanCreate == true)
                        || (sessionState.Page.Exists == true && (sessionState.CanEdit == true || sessionState.CanModerate == true || sessionState.CanDelete == true))
                        )
                        {
                            <li class="nav-item dropdown">
                                <a class="nav-link dropdown-toggle text-dark" href="#" id="navbarDropdownMenuLink" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">页面</a>
                                <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">

                                    @if (sessionState.CanCreate == true)
                                    {
                                        if (sessionState.ShouldCreatePage == true)
                                        {
                                            <a class="dropdown-item text-dark" href="@GlobalConfiguration.BasePath/@sessionState.PageNavigation/Page/Create">Create "@sessionState.PageNavigation"</a>
                                        }

                                        <a class="dropdown-item text-dark" href="@GlobalConfiguration.BasePath/Page/Create">新建页面</a>
                                    }

                                    @if (sessionState.Page.Exists == true)
                                    {
                                        @if (sessionState.CanCreate == true)
                                        {
                                            <div class="dropdown-divider"></div>
                                        }
                                        if (sessionState.CanView == true && TightWiki.Models.GlobalConfiguration.EnablePageComments)
                                        {
                                            <a class="dropdown-item text-dark" href="@GlobalConfiguration.BasePath/@sessionState.PageNavigation/Comments">评论</a>
                                        }
                                        if (sessionState.CanDelete == true)
                                        {
                                            <a class="dropdown-item text-dark" href="@GlobalConfiguration.BasePath/@sessionState.PageNavigation/Delete">删除 this page</a>
                                        }
                                        if (sessionState.CanEdit == true && sessionState.Page.IsHistoricalVersion == false)
                                        {
                                            <a class="dropdown-item text-dark" href="@GlobalConfiguration.BasePath/@sessionState.PageNavigation/Edit">编辑</a>
                                        }
                                        if (sessionState.CanView == true)
                                        {
                                            <a class="dropdown-item text-dark" href="@GlobalConfiguration.BasePath/@sessionState.PageNavigation/Revisions">修订</a>
                                        }
                                        if (sessionState.CanCreate == true)
                                        {
                                            <a class="dropdown-item text-dark" href="@GlobalConfiguration.BasePath/@sessionState.PageNavigation/Refresh">刷新</a>
                                        }
                                        if (sessionState.CanModerate == true && sessionState.Page.IsHistoricalVersion == true)
                                        {
                                            <a class="dropdown-item text-dark" href="@GlobalConfiguration.BasePath/@sessionState.PageNavigation/revert/@sessionState.Page.Revision">恢复至 @sessionState.Page.Revision</a>
                                        }
                                        if (sessionState.CanView == true && TightWiki.Models.GlobalConfiguration.EnablePageComments)
                                        {
                                            <a class="dropdown-item text-dark" href="@GlobalConfiguration.BasePath/@sessionState.PageNavigation">浏览</a>
                                        }
                                    }
                                </div>
                            </li>
                        }

                        @if (sessionState.IsAuthenticated == true && (sessionState.CanModerate == true || sessionState.Role == TightWiki.Library.Constants.Roles.Administrator))
                        {
                            <li class="nav-item dropdown">
                                <a class="nav-link dropdown-toggle text-dark" href="#" id="navbarDropdownMenuLink" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Admin</a>
                                <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
                                    @if (sessionState.Role == TightWiki.Library.Constants.Roles.Administrator)
                                    {
                                        <a class="dropdown-item text-dark" href="@GlobalConfiguration.BasePath/Admin/Accounts">账号</a>
                                        <a class="dropdown-item text-dark" href="@GlobalConfiguration.BasePath/Admin/Roles">角色</a>
                                        <div class="dropdown-divider"></div>
                                        <a class="dropdown-item text-dark" href="@GlobalConfiguration.BasePath/Admin/Config">配置</a>
                                        <a class="dropdown-item text-dark" href="@GlobalConfiguration.BasePath/Admin/Emojis">Emojis</a>
                                        <a class="dropdown-item text-dark" href="@GlobalConfiguration.BasePath/Admin/MenuItems">站点菜单</a>
                                        <div class="dropdown-divider"></div>
                                        <a class="dropdown-item text-dark" href="@GlobalConfiguration.BasePath/Admin/CompilationStatistics">计算</a>
                                        <a class="dropdown-item text-dark" href="@GlobalConfiguration.BasePath/Admin/Database">数据库</a>
                                        <a class="dropdown-item text-dark" href="@GlobalConfiguration.BasePath/Admin/Exceptions">异常</a>
                                        <a class="dropdown-item text-dark" href="@GlobalConfiguration.BasePath/Admin/Metrics">指标</a>
                                        <div class="dropdown-divider"></div>
                                        <a class="dropdown-item text-dark" href="@GlobalConfiguration.BasePath/Admin/DeletedPages">删除页面</a>
                                        <a class="dropdown-item text-dark" href="@GlobalConfiguration.BasePath/Admin/MissingPages">丢失页</a>
                                        <a class="dropdown-item text-dark" href="@GlobalConfiguration.BasePath/Admin/Moderate">页面说明</a>
                                        <a class="dropdown-item text-dark" href="@GlobalConfiguration.BasePath/Admin/Namespaces">命名空间</a>
                                        <a class="dropdown-item text-dark" href="@GlobalConfiguration.BasePath/Admin/OrphanedPageAttachments">独立附件</a>
                                        <a class="dropdown-item text-dark" href="@GlobalConfiguration.BasePath/Admin/Pages">页面</a>
                                    }
                                    else if (sessionState.CanModerate == true)
                                    {
                                        <a class="dropdown-item text-dark" href="@GlobalConfiguration.BasePath/Admin/DeletedPages">删除页面</a>
                                        <a class="dropdown-item text-dark" href="@GlobalConfiguration.BasePath/Admin/MissingPages">丢失页</a>
                                        <a class="dropdown-item text-dark" href="@GlobalConfiguration.BasePath/Admin/Moderate">页面说明</a>
                                        <a class="dropdown-item text-dark" href="@GlobalConfiguration.BasePath/Admin/Namespaces">命名空间</a>
                                        <a class="dropdown-item text-dark" href="@GlobalConfiguration.BasePath/Admin/Pages">页面</a>
                                    }
                                </div>
                            </li>
                        }

                        @if (sessionState.IsAuthenticated == true)
                        {
                            <li class="nav-item dropdown">
                                <a class="nav-link dropdown-toggle text-dark" href="#" id="navbarDropdownMenuLink" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">我</a>
                                <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
                                    <a class="dropdown-item text-dark" href="@GlobalConfiguration.BasePath/Profile/My">个人信息</a>
                                    <a class="dropdown-item text-dark" href="@GlobalConfiguration.BasePath/Identity/Account/Manage">账号</a>
                                    <div class="dropdown-divider"></div>
                                    <a class="dropdown-item text-dark" href="@GlobalConfiguration.BasePath/Identity/Account/Logout?returnUrl=/">登出</a>
                                </div>
                            </li>
                        }

                        @if (sessionState.IsAuthenticated == false)
                        {
                            <li class="nav-item"><a class="nav-link text-dark" href="@GlobalConfiguration.BasePath/Identity/Account/Login?returnUrl=@UrlEncoder.Default.Encode($"{GlobalConfiguration.BasePath}{Context.Request.Path.Value}")">登录</a></li>
                        }

                        @if (TightWiki.Models.GlobalConfiguration.IncludeSearchOnNavbar)
                        {
                            <li class="nav-item ms-auto d-none d-lg-block">
                                <div class="collapse navbar-collapse justify-content-end">
                                    <form class="d-flex" role="search" method="GET" action="@GlobalConfiguration.BasePath/Page/Search">
                                        <input class="form-control me-2" type="search" placeholder="搜索" name="SearchString" aria-label="Search">
                                        <button class="btn btn-outline-primary" style="word-break:keep-all;" type="submit">搜索</button>
                                    </form>
                                </div>
                            </li>
                        }
                    </ul>
                </div>
            </div>
        </nav>
    </header>

    <div class="container" @(TightWiki.Models.GlobalConfiguration.FixedMenuPosition ? "style=margin-top:80px;" : "")>
        <main role="main" class="pb-3">
            @Html.Raw(TightWiki.Models.GlobalConfiguration.HTMLPreBody)

            @if (TightWiki.Repository.UsersRepository.AdminPasswordStatus() != TightWiki.Library.Constants.AdminPasswordChangeState.HasBeenChanged)
            {
                <div class="card bg-warning mb-3">
                    <div class="card-header"><strong>Default password has not been changed</strong></div>
                    <div class="card-body">
                        <p class="card-text">
                            管理密码为默认值，<i>建议</i> 您立即更换！<br />
                            <br />
                            您可以登录账号后, 导航至 “我的” -&gt; “账号” -&gt; “密码”来更换密码<br />
                            <br />
                            <strong>Current admin login:</strong><br />
                            &nbsp;&nbsp;&nbsp;<strong>Username:</strong>"admin@tightwiki.com"<br />
                            &nbsp;&nbsp;&nbsp;<strong>Password:</strong>"@TightWiki.Library.Constants.DEFAULTPASSWORD"<br />
                            <br />
                        </p>
                    </div>
                </div>
            }

            @RenderBody()
            @Html.Raw(TightWiki.Models.GlobalConfiguration.HTMLPostBody)
        </main>
    </div>

    <div class="container">
        @if (TightWiki.Models.GlobalConfiguration.FooterBlurb != null || TightWiki.Models.GlobalConfiguration.Copyright != null)
        {
            <hr class="mt-5 mb-5">

            @if (TightWiki.Models.GlobalConfiguration.FooterBlurb != null)
            {
                @Html.Raw(TightWiki.Models.GlobalConfiguration.FooterBlurb)
            }
            @if (TightWiki.Models.GlobalConfiguration.Copyright != null)
            {
                <br />
                @Html.Raw(TightWiki.Models.GlobalConfiguration.Copyright)
            }
        }
        @Html.Raw(TightWiki.Models.GlobalConfiguration.HTMLFooter)
    </div>

</body>
</html>
