<%@ Reference VirtualPath="~/bitrix/components/bitrix/blog.post/component.ascx" %>
<%@ Control Language="C#" Inherits="Bitrix.Blog.Components.BlogPostTemplate" %>
<%@ Import Namespace="Bitrix.Services"%>
<%@ Import Namespace="Bitrix.Security"%>
<%@ Import Namespace="Bitrix.Blog"%>
<%@ Import Namespace="Bitrix.UI"%>
<%@ Import Namespace="Bitrix.Blog.Components"%>
<% if (Component.FatalError != BlogPostComponent.ErrorCode.None) { %>
	<div class="blog-content">
		<div class="blog-note-box blog-note-error">
			<div class="blog-note-box-text"><%= Component.GetErrorHtml(Component.FatalError) %></div>
		</div>
	</div>
	<% return; %>
<% } %>

<div class="blog-content">
<div class="blog-post">
	<h2 class="blog-post-title">
		<span><%= Component.Post.TitleHtml %></span>
	</h2>
	<div class="blog-post-info-back">
		<div class="blog-post-info">
			<div class="blog-author">
				<a class="blog-author-icon" href="<%= Component.Post.UserProfileHref %>"></a><a href="<%= Component.Post.BlogHref %>"><%= Component.Post.AuthorNameHtml %></a></div>
			<div class="blog-post-date" title="<%= Encode(Component.Post.Post.DatePublished.ToString()) %>"><%= Encode(Component.Post.Post.DatePublished.ToString("g")) %></div>
		</div>
	</div>
	<div class="blog-post-content">
		<% if (Component.Post.Post.Author != null && Component.Post.Post.Author.User != null && Component.Post.Post.Author.User.Image != null) { %>
		<div class="blog-post-avatar"><img alt="" width="<%= Component.Post.Post.Author.User.Image.Width %>px" height="<%= Component.Post.Post.Author.User.Image.Height %>px" src="<%= Component.Post.Post.Author.User.Image.FilePath %>" /></div>
		<% } %>
		<%
	int i = 1;	
	Component.RenderBeginCut += delegate(object sender, BXBlogCutTagEventArgs e)
	{
		e.Writer.Write(@"<a name=""cut");
		e.Writer.Write(i++);
		e.Writer.WriteLine(@"""></a>");
	};	
	Component.Post.RenderPostContent(CurrentWriter); 
		%>
		<div class="blog-clear-float"></div>
	</div>
	<div class="blog-post-meta">
		<div class="blog-post-meta-util">
			<span class="blog-post-comments-link"><a href="<%= Component.Post.PostViewHref %>#comments"><%= GetMessage("Comments") %>:</a> <a href="<%= Component.Post.PostViewHref %>#comments"><%= Component.Post.Post.CommentCount %></a></span>
			<span class="blog-post-views-link"><a href="<%= Component.Post.PostViewHref %>"><%= GetMessage("Views") %>:</a> <a href="<%= Component.Post.PostViewHref %>"><%= Component.Post.Post.ViewCount %></a></span>
			<% if (Component.Auth.CanEditThisPost) { %>
			<span class="blog-post-edit-link"><a href="<%= Component.Post.PostEditHref %>" rel="nofollow"><%= GetMessage("Kernel.Edit") %></a></span>
			<% } %>
			<% if (Component.Auth.CanDeleteThisPost) { %>
			<span class="blog-post-delete-link"><a href="<%= GetOperationHref(DeleteOperation) %>" onclick="return confirm('<%= Encode(JSEncode(GetMessageRaw("Confirm.Delete"))) %>');" rel="nofollow"><%= GetMessage("Kernel.Delete") %></a></span>
			<% } %>
		</div>
	</div>
</div>
</div>
<script runat="server">
	private const string OperationParameter = "_action";
	private const string CsrfParameter = "_token";
	private const string SourceParameter = "_source";
	private const string DeleteOperation = "delete-post";
	
	string GetOperationHref(string operation)
	{
		NameValueCollection query = HttpUtility.ParseQueryString(BXSefUrlManager.CurrentUrl.Query);
		query.Set(SourceParameter, ClientID.GetHashCode().ToString());
		query.Set(OperationParameter, operation);
		query.Set(CsrfParameter, BXCsrfToken.GenerateToken());

		UriBuilder uri = new UriBuilder(BXSefUrlManager.CurrentUrl);
		uri.Query = query.ToString();
		return Encode(uri.Uri.ToString());
	}

	protected override void OnLoad(EventArgs e)
	{
		base.OnLoad(e);
		
		if (Request == null)
			return;
		if (Request.QueryString[OperationParameter] != DeleteOperation)
			return;
		if (Request.QueryString[SourceParameter] != ClientID.GetHashCode().ToString())
			return;
		if (!BXCsrfToken.CheckToken(Request.QueryString[CsrfParameter]))
			return;

		Component.Delete();
	}
</script>
