@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="forum-topic-content-top">
        <div class="topic-name">
            <h2>@Model.Subject</h2>
        </div>
        <div class="topic-actions upper">
            <div class="actions">
                @if (Model.IsCustomerAllowedToEditTopic)
                {
                    <div class="actions-btn">
                        <i class="fa fa-pencil" aria-hidden="true">@Html.RouteLink(T("Forum.EditTopic").Text, "TopicEdit", new { id = Model.Id }, new { @class = "edit-topic-button" })</i>
                    </div>
                }
                @if (Model.IsCustomerAllowedToDeleteTopic)
                {
                    <div class="actions-btn">
                        <i class="fa fa-trash" aria-hidden="true"><a href="#" class="delete-topic-button" onclick="return deletetopic(@(Model.Id))">@T("Forum.DeleteTopic").Text</a></i>
                    </div>
                    <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)
                {
                    <div class="actions-btn">
                        <i class="fa fa-arrows-alt" aria-hidden="true">@Html.RouteLink(T("Forum.MoveTopic").Text, "TopicMove", new { id = Model.Id }, new { @class = "move-topic-button" })</i>
                    </div>
                }
                <div class="actions-btn">
                    <i class="fa fa-mail-reply-all" aria-hidden="true">@Html.RouteLink(T("Forum.Reply").Text, "PostCreate", new { id = Model.Id }, new { @class = "reply-topic-button" })</i>
                </div>
                @if (Model.IsCustomerAllowedToSubscribe)
                {
                    <div class="actions-btn"><i class="fa fa-eye" aria-hidden="true"><a class="watch-topic-button" href="#" id="watch-topic-top">@Model.WatchTopicText</a></i></div>
                    <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>
    <div class="topic-posts">
        @foreach (var post in @Model.ForumPostModels)
        {
            await Html.RenderPartialAsync("_ForumPost", post);
        }
    </div>
    <div class="topic-actions lower">
        <div class="actions">
            <i class="fa fa-mail-reply-all" aria-hidden="true">@Html.RouteLink(T("Forum.Reply").Text, "PostCreate", new { id = Model.Id }, new { @class = "reply-topic-button" })</i>
            @if (Model.IsCustomerAllowedToSubscribe)
            {
                <i class="fa fa-eye" aria-hidden="true"><a class="watch-topic-button" href="#" id="watch-topic-bottom">@Model.WatchTopicText</a></i>
                <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>
