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

<h3>
    关于页面 <a href="@GlobalConfiguration.BasePath/@sessionState.PageNavigation">@sessionState.Page.Name</a> 的评论.
</h3>

<p>
    <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>
}

@using (Html.BeginForm(null, null, FormMethod.Post, new { action = $"{GlobalConfiguration.BasePath}{Context.Request.Path}" }))
{
    if (sessionState.IsAuthenticated == true)
    {
        <div class="container">
            <div class="d-flex justify-content-end mb-4">
                <div class="flex-grow-1 me-2">
                    <input type="text" name="Comment" id="Comment" class="form-control" placeholder="输入关键字" />
                </div>
                <button type="submit" value="Find" class="btn btn-primary">搜一搜</button>
            </div>
        </div>
    }
    else
    {
        @:<a href="@GlobalConfiguration.BasePath/Identity/Account/Login?returnUrl=@Context.Request.Path.Value">登录发表评论</a>.
    }

    if (Model.Comments.Count > 0)
    {
        <div class="container mt-4">
            @foreach (var h in Model.Comments)
            {
                <div class="border rounded mb-2 p-2">
                    <p class="mb-1">@Html.Raw(h.Body)</p>
                    <div class="d-flex justify-content-between align-items-center">
                        <div class="d-flex align-items-center small text-muted mb-0">
                            <img src="/Profile/@h.UserNavigation/Avatar?Exact=16" class="rounded-circle me-2" alt="Avatar" />
                            @if (ZelWiki.Models.GlobalConfiguration.EnablePublicProfiles)
                            {
                                <a href="@GlobalConfiguration.BasePath/Profile/@h.UserNavigation/Public" class="small text-decoration-none mb-0">@Html.DisplayTextFor(x => h.UserName)</a>
                            }
                            else
                            {
                                <span class="small text-decoration-none mb-0">@Html.DisplayTextFor(x => h.UserName)</span>
                            }
                            &nbsp;&nbsp;<p class="small text-decoration-none text-muted mb-0">@Html.DisplayTextFor(x => h.CreatedDate)</p>
                        </div>
                        @if (sessionState.CanModerate == true || h.UserId == sessionState.Profile?.UserId)
                        {
                            <a href="?Delete=@h.Id" class="small text-danger text-decoration-none" onclick="return confirm('真的要删除吗?')">Delete</a>
                        }
                    </div>
                </div>
            }
        </div>
    }
    else
    {
        <div class="d-flex small text-muted mb-0">
            <strong>暂无评论</strong>
        </div>
    }

    @Html.Raw(ZelWiki.Library.PageSelectorGenerator.Generate(Context.Request.QueryString, Model.PaginationPageCount))
}
