using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;
using MySql.Data.MySqlClient;

using Security;
namespace Security
{
    public partial class TreeAdd : PageBase
    {
		MysqlHelp mh = null;
		DataTable dt = null;
		protected void Page_Load(object sender, EventArgs e)
		{
			if(!IsPostBack)
            {
			
            }
			///获取数据源
			mh = new MysqlHelp();
			dt = mh.GetDatabysql("Select * From Class4DeepSoon");
			
			string addID = Request.QueryString["addID"];
			string addName = Request.QueryString["addName"];
			if(addID != null && addName != null){
			
				///初始化表单数据
				this.labelAddInfor.Text = "在【"+addName+"】下创建子栏目";
				this.txtFatherName.Text = addName;
				this.txtFatherID.Text = addID;
			}
			
        }
		
		///添加新节点时，更新其父节点的childID字段
		private void updateChildId4AddFunction(TreeNode node,string NodeId,string addChildId)
        {
			DataRow [] rows = dt.Select("Id = '"+NodeId+"'"); //筛选出当前节点的父亲节点数据
			///创建父亲节点
            foreach(DataRow dr in rows) //循环子节点集合
            {
				TreeNode nd = new TreeNode();
				nd.Value=dr["fatherId"].ToString();//父亲节点ID
				nd.Text=dr["name"].ToString();//父亲节点Name
				if(nd.Value != "0")///id=0表示到达最顶的根
				{
					///获取间接父亲节点的原childID值：
					DataTable fatherData = mh.GetDatabysql("Select childId From Class4DeepSoon Where Id="+nd.Value);
					string fatherChildID = ""+fatherData.Rows[0]["childID"];
					///新ChildId值：
					string newFatherChildID = fatherChildID+","+addChildId;
					Response.Write("<font color=red>更新间接父亲节点(Id="+nd.Value+")的childId: "+newFatherChildID+"</font><br>");
					mh.Runsql("Update Class4DeepSoon Set childId='"+newFatherChildID+"' Where Id="+nd.Value);
				
					this.updateChildId4AddFunction(nd,nd.Value,addChildId);    //递归调用自身
				}
            }
		}

		protected void btnAdd_Click(object sender, EventArgs e)
		{
			string menuName = this.txtName.Text.Replace("'", "").Replace("\"", "").Replace(" ", "");
			if(menuName.Length>20)menuName = menuName.Substring(0,20);			
			string fatherID = this.txtFatherID.Text;
			string outLinkHref = this.txtOutLinkHref.Text.Replace("'", "").Replace("\"", "").Replace(" ", "");
			if(outLinkHref.Length>250)outLinkHref = outLinkHref.Substring(0,250);
			string childID = "";
			string isHidden = this.dropIsHidden.SelectedItem.Value;
			string linkTarget = this.dropLinkTarget.SelectedItem.Value;
			string openArea = this.dropOpenArea.SelectedItem.Value;
			string themeFile = this.txtThemeFile.Text.Replace("'", "").Replace("\"", "").Replace(" ", "");
			
			if(menuName != ""){
				///添加新账户：
				mh.Runsql("Insert Into Class4DeepSoon(Name,fatherID,OutLinkHref,isHidden,ThemeFile,linkTarget,openArea) Values('"+menuName+"','"+fatherID+"','"+outLinkHref+"',"+isHidden+",'"+themeFile+"','"+linkTarget+"','"+openArea+"')");
				this.labelAddInfor.Text = "新栏目 "+menuName+" 已经成功添加!";
				
				Response.Write("更新新栏目自身的ChildID<br>");
				DataTable newNodeData = mh.GetDatabysql("Select * From Class4DeepSoon order by id desc limit 1");
				string currNodeID = ""+newNodeData.Rows[0]["Id"];
				mh.Runsql("Update Class4DeepSoon Set childId='"+currNodeID+"' Where Id="+currNodeID);
				Response.Write("自身ChildID="+currNodeID+"<br>");
				
				
				Response.Write("新栏目的ID是"+currNodeID+" 其直接父亲节点是"+fatherID+" <br>");
				if(fatherID != "0"){///如果其直接父节点不是顶端根目录
					///获取直接父亲节点的原childID值：
					DataTable fatherData = mh.GetDatabysql("Select childId From Class4DeepSoon Where Id="+fatherID);
					string fatherChildID = ""+fatherData.Rows[0]["childID"];
					///新ChildId值：
					string newFatherChildID = fatherChildID+","+currNodeID;
					Response.Write("<font color=red>更新直接父亲节点(Id="+fatherID+")的childId: "+newFatherChildID+"</font><br>");
					mh.Runsql("Update Class4DeepSoon Set childId='"+newFatherChildID+"' Where Id="+fatherID);
				}
				///递归方式，修改间接父亲节点的childID
				TreeNode updateNd = new TreeNode();
				updateNd.Value=""+fatherID;//当前节点ID
				updateNd.Text=""+menuName;//当前节点Name
				this.updateChildId4AddFunction(updateNd,updateNd.Value,currNodeID);
				
				///添加成功，跳转到栏目树
				Response.Redirect("DeepSoonTree.aspx");
			}
			else{
				this.labelAddInfor.Text = "新栏目名称不能为空!";
			}
		}

	}//end class
}//end nameSpace