<%@ Reference VirtualPath="~/bitrix/components/bitrix/blog.edit/component.ascx" %>
<%@ Control Language="C#" Inherits="Bitrix.Blog.Components.BlogEditTemplate" EnableViewState="false" %>
<%@ Import Namespace="Bitrix.Blog" %>
<%@ Import Namespace="Bitrix.Blog.Components" %>
<%@ Import Namespace="System.Collections.Generic" %>

<script runat="server">
	protected override void LoadData(BlogEditComponent.Data data)
	{
		BlogName.Text = data.BlogName;
		BlogSlug.Text = data.BlogSlug;
		BlogDescription.Text = data.BlogDescription;
		NotifyComments.Checked = data.NotifyComments;
		DisableBlog.Checked = !data.BlogActive;

		if (data.Categories != null && data.Categories.Length > 0)
		{
			foreach (ListItem listItem in BlogCategories.Items)
			{
				bool selected = Array.Exists<int>(data.Categories, delegate(int id) { return String.Equals(id.ToString(), listItem.Value); });
				listItem.Selected = selected;
				if (selected && BlogCategories.SelectionMode == ListSelectionMode.Single)
					break;
			}
		}
	}
	
	protected override void SaveData(BlogEditComponent.Data data)
	{
		data.BlogName = BlogName.Text;
		data.BlogDescription = BlogDescription.Text;
		
		if (Component.ComponentMode == BlogEditComponent.Mode.Add)
			data.BlogSlug = BlogSlug.Text;

		if (Component.ComponentMode == BlogEditComponent.Mode.Edit && Component.CanModerate && !Component.IsOwner)
			data.BlogActive = !DisableBlog.Checked;
		
		List<int> selectedCategories = new List<int>();
		foreach (ListItem category in BlogCategories.Items)
		{
			if (category.Selected)
			{
				int categoryId;
				if (int.TryParse(category.Value, out categoryId))
					selectedCategories.Add(categoryId);
			}
		}

		data.Categories = selectedCategories.ToArray();
		data.NotifyComments = NotifyComments.Checked;
	}

	protected override void OnInit(EventArgs e)
	{
		if (Component.MaxBlogGroups == 1)
			BlogCategories.Items.Add(new ListItem(GetMessageRaw("Option.SelectCategory"), String.Empty));
		else
		{
			BlogCategories.SelectionMode = ListSelectionMode.Multiple;
			BlogCategories.Rows = Math.Max(2, Component.SiteCategories.Count > 10 || Component.SiteCategories.Count == 0 ? 10 : Component.SiteCategories.Count);
		}

		foreach (BXBlogCategory category in Component.SiteCategories)
			BlogCategories.Items.Add(new ListItem(category.Name, category.Id.ToString()));
		
		base.OnInit(e); //important last call
	}
	
	protected override void OnLoad(EventArgs e)
	{
		base.OnLoad(e);

		if (Component.FatalError == BlogEditComponent.ErrorCode.UnauthorizedNotLogIn)
		{
			Bitrix.Security.BXAuthentication.AuthenticationRequired();
			return;
		}
		else if (Component.FatalError != BlogEditComponent.ErrorCode.None)
		{
			BlogSlugEmptyValidator.Enabled = false;
			BlogSlugRegexValidator.Enabled = false;
			BlogSlugLengthValidator.Enabled = false;
			BlogNameValidator.Enabled = false;
			BlogCategoriesValidator.Enabled = false;
			Errors.Enabled = false;
			return;
		}
		
		string validationGroup = ClientID;
		Errors.ValidationGroup = validationGroup;
		BlogNameValidator.ValidationGroup = validationGroup;

		if (Component.ComponentMode == BlogEditComponent.Mode.Add)
		{
			BlogSlugEmptyValidator.Enabled = true;
			BlogSlugRegexValidator.Enabled = true;
			BlogSlugEmptyValidator.ValidationGroup = validationGroup;
			BlogSlugRegexValidator.ValidationGroup = validationGroup;
			BlogSlugRegexValidator.ValidationExpression = BXBlog.SlugRegex.ToString();

			if (Component.MinSlugLength <= Component.MaxSlugLength)
			{
				BlogSlugLengthValidator.ValidationGroup = validationGroup;
				BlogSlugLengthValidator.Enabled = true;
				BlogSlugLengthValidator.ValidationExpression = String.Concat("^.{", Component.MinSlugLength, ",", Component.MaxSlugLength, "}$");
				BlogSlugLengthValidator.ErrorMessage = String.Format(GetMessage("Error.SlugLength"), Component.MinSlugLength, Component.MaxSlugLength);
				BlogSlug.MaxLength = Component.MaxSlugLength;
			}
		}
		
		BlogCategoriesValidator.ValidationGroup = validationGroup;
		Save.ValidationGroup = validationGroup;

		if (Component.ComponentMode == BlogEditComponent.Mode.Add)
			Save.Text = GetMessageRaw("ButtonText.CreateBlog");
		else
			Save.Text = GetMessageRaw("ButtonText.EditBlog");
	}

	protected new void SaveClick(object sender, EventArgs e)
	{
		if (!Page.IsValid)
			return;
		
		base.SaveClick(sender, e);
	}
</script>

<% 
	if (Component.FatalError != BlogEditComponent.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-edit-form blog-edit-blog-form">

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

		<div class="blog-edit-form-title"><%= (Component.ComponentMode == BlogEditComponent.Mode.Add ? GetMessage("FormTitle.CreateBlog") : GetMessage("FormTitle.BlogSettings")) %></div>

		<div class="blog-edit-fields">
		
			<div class="blog-edit-field blog-edit-field-title">
				<label for="<%= Encode(BlogName.ClientID) %>"><%= GetMessage("Label.Name") %><span class="blog-required-field">*</span></label>
				<asp:TextBox ID="BlogName" runat="server" MaxLength="255" TabIndex="1" Columns="60"></asp:TextBox>
				<asp:RequiredFieldValidator runat="server" ID="BlogNameValidator" ErrorMessage="<%$ Loc:Message.NameNotSpecified %>" ControlToValidate="BlogName" Display="None" SetFocusOnError="true" />
			</div>
			
			<% if (Component.ComponentMode == BlogEditComponent.Mode.Add) { %>
			<div class="blog-edit-field blog-edit-field-slug">
				<label for="<%= Encode(BlogSlug.ClientID) %>"><%= GetMessage("Label.Slug") %><span class="blog-required-field">*</span></label>
				<asp:TextBox ID="BlogSlug" TabIndex="2" Columns="60" runat="server"></asp:TextBox>
				<asp:RequiredFieldValidator runat="server" Enabled="False" ID="BlogSlugEmptyValidator"	ErrorMessage="<%$ Loc:Message.SlugNotSpecified %>" ControlToValidate="BlogSlug" Display="None" SetFocusOnError="true" />
				<asp:RegularExpressionValidator runat="server" Enabled="False" ID="BlogSlugRegexValidator" ErrorMessage="<%$ Loc:Message.InvalidSlug %>" ControlToValidate="BlogSlug" Display="None" SetFocusOnError="true" />
				<asp:RegularExpressionValidator runat="server" Enabled="False" ID="BlogSlugLengthValidator" ControlToValidate="BlogSlug" Display="None" SetFocusOnError="true" />
			</div>
			<%} %>
			
			<div class="blog-edit-field blog-edit-field-cats">
				<label for="<%= Encode(BlogCategories.ClientID) %>"><%= GetMessage("Label.BlogCategory") %><span class="blog-required-field">*</span></label>
				<asp:ListBox ID="BlogCategories" TabIndex="3" Rows="1" runat="server"></asp:ListBox>
				<asp:RequiredFieldValidator runat="server" ID="BlogCategoriesValidator"	ErrorMessage="<%$ Loc:Message.CategoryNotSpecified %>" ControlToValidate="BlogCategories" Display="None" SetFocusOnError="true" />
			</div>

			<div class="blog-edit-field blog-edit-field-desc">
				<label for="<%= Encode(BlogDescription.ClientID) %>"><%= GetMessage("Label.Description") %></label>
				<asp:TextBox ID="BlogDescription"  TabIndex="4" TextMode="MultiLine" Rows="7" runat="server"></asp:TextBox>	
			</div>
			
			<div class="blog-edit-field blog-edit-field-settings">
				<div class="blog-edit-field-setting"><asp:CheckBox ID="NotifyComments" TabIndex="5" runat="server" Text="<%$ Loc:CheckBoxText.SendEmailNotifications %>" /></div>
			</div>
			
			<% if (Component.ComponentMode == BlogEditComponent.Mode.Edit && Component.CanModerate && !Component.IsOwner) { %>
			<div class="blog-edit-field blog-edit-field-settings">
				<div class="blog-edit-field-setting"><asp:CheckBox ID="DisableBlog" TabIndex="6" runat="server" Text="<%$ Loc:CheckBoxText.DisableBlog %>" /></div>
			</div>
			<%} %>
			
			<div class="blog-edit-buttons">
				<asp:Button runat="server" ID="Save" TabIndex="7" OnClick="SaveClick" /> 
			</div>
		</div>
	</div>
</div>