//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.ControlPanel.UI;
using CommunityServer.Controls;
using FormLabel = CommunityServer.ControlPanel.Controls.FormLabel;
using Style = CommunityServer.Controls.Style;

namespace CommunityServer.ControlPanel.Blogs
{
	/// <summary>
	/// Summary description for CategoryForm.
	/// </summary>
	public class CategoryForm : BaseBlogPage
	{
		protected Style ControlPanelStyle;
		protected FormLabel tt;
		protected TextBox CategoryName;
		protected FormLabel Formlabel1;
		protected TextBox CategoryDesc;
		protected FormLabel Formlabel2;
		protected CheckBox CategoryEnabled;
		protected RequiredFieldValidator RequiredFieldValidator1;
		protected MPContent bcr;
		protected MPContainer MPContainer;
		protected IButton Button1;
	
		private void Page_Load(object sender, EventArgs e)
		{
            if(!IsPostBack)
            {
            	PostCategory pc = GetCategory();	        
                CategoryName.Text = Server.HtmlDecode(pc.Name) ;
                CategoryDesc.Text = pc.Description;
                CategoryEnabled.Checked = pc.IsEnabled;
            }

			Head.AddTitle(CommunityServer.ControlPanel.Components.ResourceManager.GetString("CP_Blog_CategoryForm_Title"), this.Context);
		}

        protected PostCategory GetCategory()
        {
            PostCategory pc = null;

            if(Request.QueryString["CategoryID"] != null && Request.QueryString["CategoryID"] != "-1")
            {
                
                pc = PostCategories.GetCategory(Int32.Parse(Request.QueryString["CategoryID"]),CurrentWeblog.SectionID,true,true);
                if(pc == null)
                    throw new Exception("Category could not be found");
            }
            else
            {
                pc = new PostCategory();
                pc.SectionID = CurrentWeblog.SectionID;
            
            }

            return pc;
        }

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.Button1.Click += new EventHandler(this.Button1_Click);
			this.Load += new EventHandler(this.Page_Load);

		}
		#endregion

		private void Button1_Click(object sender, EventArgs e)
		{
            if(Page.IsValid)
            {
                PostCategory pc = GetCategory();
                pc.Name = CategoryName.Text;
                pc.Description = CategoryDesc.Text;
                pc.IsEnabled = CategoryEnabled.Checked;

                if(pc.CategoryID > 0)
                    PostCategories.UpdateCategory(pc);
                else
                    PostCategories.CreateCategory(pc);

				Modal.ClosePage(Page,"true") ;

            }
			
		}
	}
}
