<%@ Reference VirtualPath="~/bitrix/components/bitrix/blog.post.form/component.ascx" %>
<%@ Control Language="C#" Inherits="Bitrix.Blog.Components.BlogPostFormTemplate" %>
<%@ Import Namespace="Bitrix.UI"%>
<%@ Import Namespace="Bitrix.Blog.Components"%>
<%@ Import Namespace="Bitrix.Security"%>
<%@ Register TagPrefix="bx" Assembly="CommunicationUtility"  Namespace="Bitrix.CommunicationUtility" %>
<% 
	if (Component.FatalError != BlogPostFormComponent.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;
	} 

	Errors.HeaderText = string.Format(@"<div class=""blog-note-box-text"">{0}</div>", GetMessage("ErrorTitle"));
	foreach (BlogPostFormComponent.ValidationMessageCode message in Component.ValidationErrors)
		Errors.AddErrorMessage(Component.GetValidationMessageHtml(message));
%>
<bx:BXValidationSummary runat="server" ID="Errors" CssClass="blog-note-box blog-note-error" ForeColor="" /> 

<div class="blog-content">

<% 
	if (preview) {

		BXUser user = BXIdentity.Current.User;
%>
	<div class="blog-post">
		<h2 class="blog-post-title">
			<span><%= BXWordBreakingProcessor.Break(PostTitle.Text, Component.MaxWordLength, true)%></span>
		</h2>
		<div class="blog-post-info-back">
			<div class="blog-post-info">
				<% if (user != null) { %>
				<div class="blog-author">
					<a class="blog-author-icon" href="#"></a><a href="#"><%= Encode(user.GetDisplayName())%></a>
				</div>
				<%} %>
				<div class="blog-post-date" title="<%= Encode(Component.Data.DatePublished.ToString())%>"><%= Encode(Component.Data.DatePublished.ToString("g"))%></div>
			</div>
		</div>
		<div class="blog-post-content">
			<% if (user != null && user.Image != null) { %>
			<div class="blog-post-avatar"><img alt="" width="<%= user.Image.Width %>px" height="<%= user.Image.Height %>px" src="<%= user.Image.FilePath %>" /></div>
			<% } %>
			<%
				Component.Preview(Content.Text, CurrentWriter); 
			%>
			<div class="blog-clear-float">
			</div>
		</div>
	</div>
<%
	}
%>

	<div class="blog-edit-form blog-edit-post-form">

		<div class="blog-edit-form-title">
		<%= (Component.Data.IsNew ? GetMessage("FormTitle.CreatePost") : GetMessage("FormTitle.EditPost")) %>
		</div>
		<div class="blog-edit-fields">	
			<%
				if (!IsPostBack)
				{
					PostTitle.Text = Component.Data.Title;
					Content.Text = Component.Data.Content;
					DatePublished.Text = Component.Data.DatePublished.ToString();

					if (string.IsNullOrEmpty(PostTitle.Text))
						PostTitle.Text = GetMessageRaw("Title");
				}
			%>
			<%
				PostTitle.Attributes.Add("onfocus", string.Format("if (this.value=='{0}') {{this.value=''; this.className='blog-editable-field'}}", JSEncode(GetMessageRaw("Title"))));
				PostTitle.Attributes.Add("onblur", string.Format("if (this.value=='') {{this.value='{0}'; this.className=''}};", JSEncode(GetMessageRaw("Title"))));
				if (PostTitle.Text != JSEncode(GetMessageRaw("Title")))
					PostTitle.CssClass = "blog-editable-field";		
			%>
			<div class="blog-edit-field blog-edit-field-title">
				<asp:TextBox ID="PostTitle" runat="server" TabIndex="1"/>
				<asp:RequiredFieldValidator ID="PostTitleValidator" runat="server" ControlToValidate="PostTitle" Display="None" />
			</div>

			<div class="blog-edit-field blog-edit-field-post-date">
				<script type="text/javascript">
					window.document.writeln('<a href="" onclick="document.getElementById(\'<%= JSEncode(Encode(JSEncode(DatePublished.ClientID))) %>$date\').style.display=\'block\'; this.style.display=\'none\'; return false;"><%= JSEncode(Component.Data.DatePublished.ToString()) %></a>');
					window.document.writeln('<div style="display:none;" id="<%= JSEncode(Encode(JSEncode(DatePublished.ClientID))) %>$date">');
				</script>
				<asp:TextBox ID="DatePublished" runat="server" TabIndex="2" />
				<script type="text/javascript">
					window.document.write('<%= JSEncode(BXCalendarHelper.GetMarkupByInputElement("document.getElementById('" + JSEncode(DatePublished.ClientID) + "')", true)) %>');
					window.document.writeln('</div>');
				</script>
			</div>

			<div class="blog-edit-field blog-edit-field-bbcode">
				<bx:BBCodeLine runat="server" ID="BBCode" TextControl="Content" TagList="b,u,i,s,url,img,quote,cut,code,list,color"/>
				<div class="blog-clear-float"></div>
			</div>

			<div class="blog-edit-field blog-edit-field-text">
				<asp:TextBox ID="Content" runat="server" TextMode="MultiLine" Rows="20" Columns="50" TabIndex="3" />
			</div>

			<%
				Publish.Text = (Component.Data.IsNew || !Component.Data.IsPublished) ? GetMessageRaw("ButtonText.Publish") : GetMessageRaw("ButtonText.Save");
				Store.Text = (Component.Data.IsNew || !Component.Data.IsPublished) ? GetMessageRaw("ButtonText.SaveAsDraft") : GetMessageRaw("ButtonText.ToDrafts");
				if (Component.Data.IsNew || !Component.Data.IsPublished)	
					Publish.OnClientClick = string.Format("if (!confirm('{0}')) return false;", JSEncode(GetMessageRaw("Confirm.Publish")));
			%>
			<div class="blog-edit-buttons">
				<asp:Button runat="server" ID="Publish" OnClick="Publish_Click" TabIndex="4" />
				<asp:Button runat="server" ID="Store" OnClick="Store_Click" TabIndex="5" />
				<asp:Button runat="server" ID="Preview" Text="<%$ LocRaw:ButtonText.View %>" OnClick="Preview_Click" CausesValidation="false" TabIndex="6" />
			</div>
		</div>
	</div>
</div>
<script runat="server">
	private bool preview;

	protected override void OnInit(EventArgs e)
	{
		base.OnInit(e);
		Errors.ValidationGroup = ClientID;
		PostTitleValidator.ValidationGroup = ClientID;
		Publish.ValidationGroup = ClientID;
		Store.ValidationGroup = ClientID;
		
		PostTitleValidator.ErrorMessage = Component.GetValidationMessageHtml(BlogPostFormComponent.ValidationMessageCode.TitleIsRequired);
	}
	protected override void OnPreRender(EventArgs e)
	{
		base.OnPreRender(e);
		if (Component.FatalError != BlogPostFormComponent.ErrorCode.None)
		{
			Errors.Enabled = false;
			PostTitleValidator.Enabled = false;
			return;
		}
		BXCalendarHelper.RegisterScriptFiles();
	}

	void Save(bool draft)
	{
		if (!Page.IsValid)
			return;

		DateTime d;
		Component.Data.Title = PostTitle.Text;
		Component.Data.Content = Content.Text;
		Component.Data.DatePublished = DateTime.TryParse(DatePublished.Text, out d) ? d : DateTime.MinValue;
		Component.Data.IsPublished = !draft;

		if (!Component.Validate())
			return;

		Component.Save();
	}

	void Publish_Click(object sender, EventArgs e)
	{
		Save(false);
	}
	void Store_Click(object sender, EventArgs e)
	{
		Save(true);
	}
	void Preview_Click(object sender, EventArgs e)
	{
		preview = true;
	}
</script>
