@using Nop.Services.Localization
@model ForumTopicPageModel
@inject ILocalizationService localizationService
@{
    Layout = "_ColumnsOne";

    //title
    NopHtml.AddTitleParts(Model.Subject);
    //page class
    NopHtml.AppendPageCssClassParts("html-forum-topic-page");
}
<div class="page forum-topic-page">
    @await Component.InvokeAsync(typeof(ForumBreadcrumbViewComponent), new { forumTopicId = Model.Id })
    @await Html.PartialAsync("_ForumHeader")
    @await Component.InvokeAsync(typeof(WidgetViewComponent), new { widgetZone = PublicWidgetZones.BoardsTopicAfterHeader, additionalData = Model })
    <div class="topic-name">
        <h1>@Model.Subject</h1>
    </div>
    <div class="topic-actions upper">
        <div class="actions">
            @if (Model.IsCustomerAllowedToEditTopic)
            {
                @Html.RouteLink(T("Forum.EditTopic").Text, "TopicEdit", new { id = Model.Id }, new { @class = "edit-topic-button" })
            }
            @if (Model.IsCustomerAllowedToDeleteTopic)
            {
                <a href="#" class="delete-topic-button" onclick="return deletetopic(@(Model.Id))">@T("Forum.DeleteTopic").Text</a>
                <script asp-location="Footer">
                    function deletetopic(topicId) {
                        if (confirm('@T("Common.AreYouSure")')) {
                            var postData = {
                                id: topicId
                            };
                            addAntiForgeryToken(postData);
                            $.ajax({
                                cache: false,
                                type: "POST",
                                url: "@Url.RouteUrl("TopicDelete")",
                                data: postData,
                                dataType: "json",
                                success: function (data, textStatus, jqXHR) {
                                    location.href = data.redirect;
                                },
                                error: function (jqXHR, textStatus, errorThrown) {
                                    alert('Failed to delete');
                                }
                            });
                        }
                        return false;
                    }
                </script>
            }
            @if (Model.IsCustomerAllowedToMoveTopic)
            {
                @Html.RouteLink(T("Forum.MoveTopic").Text, "TopicMove", new { id = Model.Id }, new { @class = "move-topic-button" })
            }
            @Html.RouteLink(T("Forum.Reply").Text, "PostCreate", new { id = Model.Id }, new { @class = "reply-topic-button" })
            @if (Model.IsCustomerAllowedToSubscribe)
            {
                <a class="watch-topic-button" href="#" id="watch-topic-top">@Model.WatchTopicText</a>
                <script asp-location="Footer">
                    $(function() {
                        $('#watch-topic-top').on('click', function () {
                            var postData = {};
                            addAntiForgeryToken(postData);
                            $.ajax({
                                cache: false,
                                type: "POST",
                                data: postData,
                                url: "@Url.RouteUrl("TopicWatch", new { id = Model.Id })",
                                dataType: "json",
                                success: function (data, textStatus, jqXHR) {
                                    $('#watch-topic-top').text(data.Text);
                                    $('#watch-topic-bottom').text(data.Text);
                                },
                                error: function (jqXHR, textStatus, errorThrown) {
                                    alert('Failed to watch');
                                }
                            });
                        });
                    });
                </script>
            }
        </div>
        @{
            var pager = await Html.PagerAsync(new PagerModel(localizationService)
            {
                PageSize = Model.PostsPageSize,
                TotalRecords = Model.PostsTotalRecords,
                PageIndex = Model.PostsPageIndex,
                ShowTotalSummary = false,
                RouteActionName = "TopicSlugPaged",
                UseRouteLinks = true,
                RouteValues = new SlugRouteValues { Id = Model.Id, Slug = Model.SeName }
            });
        }
        @await Component.InvokeAsync(typeof(WidgetViewComponent), new { widgetZone = PublicWidgetZones.BoardsTopicTop, additionalData = Model })
        @if (!string.IsNullOrEmpty(await pager.RenderHtmlContentAsync()))
        {
            <div class="pager upper">
                @pager
            </div>
        }
    </div>

    <div class="topic-posts">
        @foreach (var post in @Model.ForumPostModels)
        {
            await Html.RenderPartialAsync("_ForumPost", post);
        }
    </div>

    <div class="topic-actions lower">
        <div class="actions">
            @Html.RouteLink(T("Forum.Reply").Text, "PostCreate", new {id = Model.Id}, new {@class = "reply-topic-button"})
            @if (Model.IsCustomerAllowedToSubscribe)
            {
                <a class="watch-topic-button" href="#" id="watch-topic-bottom">@Model.WatchTopicText</a>
                <script asp-location="Footer">
                    $(function() {
                        $('#watch-topic-bottom').on('click', function() {
                            var postData = {};
                            addAntiForgeryToken(postData);

                            $.ajax({
                                cache: false,
                                type: "POST",
                                url: "@Url.RouteUrl("TopicWatch", new {id = Model.Id})",
                                data: postData,
                                dataType: "json",
                                success: function (data, textStatus, jqXHR) {
                                    $('#watch-topic-top').text(data.Text);
                                    $('#watch-topic-bottom').text(data.Text);
                                },
                                error: function (jqXHR, textStatus, errorThrown) {
                                    alert('Failed to watch');
                                }
                            });
                        });
                    });
                </script>
            }
        </div>
        @if (!string.IsNullOrEmpty(await pager.RenderHtmlContentAsync()))
        {
            <div class="pager lower">
                @pager
            </div>
        }
    </div>
    @await Component.InvokeAsync(typeof(WidgetViewComponent), new { widgetZone = PublicWidgetZones.BoardsTopicBottom, additionalData = Model })
</div>
<script asp-location="Footer">
    function deletepost(postId) {
        if (confirm('@T("Common.AreYouSure")')) {
            var postData = {
                id: postId
            };
            addAntiForgeryToken(postData);
            $.ajax({
                cache: false,
                type: "POST",
                url: "@Url.RouteUrl("PostDelete")",
                data: postData,
                dataType: "json",
                success: function (data, textStatus, jqXHR) {
                    location.href = data.redirect;
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert('Failed to delete');
                }
            });
        }
        return false;
    }
</script>