using System;
using System.Reflection ;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using JXC_MIS.MyClass;
using JXC_MIS.DataTier ;

namespace JXC_MIS.UserTier.ToolForm
{
	/// <summary>
	/// frmMenuEdit 菜单编辑器。
	/// </summary>
	public class frmMenuEdit : System.Windows.Forms.Form
	{
		private System.Windows.Forms.TreeView tre_MainMenu;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Button cmd_Cancel;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.Label label5;
		//		ClsDataBase newAdo=new ClsDataBase();
		ClsInitData InitData=new ClsInitData();
		ClsDataBase newDb=new ClsDataBase();
		ClsDataBaseAccess newADO=new ClsDataBaseAccess();
		bool w_EidtFlag;
		string w_keyId;
		private System.Windows.Forms.TextBox txt_MenuCode;
		private System.Windows.Forms.TextBox txt_MenuName;
		private System.Windows.Forms.TextBox txt_MenuLevel;
		private System.Windows.Forms.ComboBox cbo_LoadFormName;
		private System.Windows.Forms.TextBox txt_MenuCaption;
		private System.Windows.Forms.TextBox txt_MenuCaptionEN;
		private System.Windows.Forms.Label label6;
		private System.Windows.Forms.Button cmd_Del;
		private System.Windows.Forms.Button cmd_Modify;
		private System.Windows.Forms.Button cmd_Add;
		private System.Windows.Forms.Button cmd_Refresh;
		private System.Windows.Forms.Button cmd_Save;
		private System.Windows.Forms.Button cmd_Exit;
		private System.Windows.Forms.CheckBox chk_Visible;

		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;

		public frmMenuEdit()
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();

			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//
		}

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows 窗体设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.tre_MainMenu = new System.Windows.Forms.TreeView();
			this.txt_MenuCode = new System.Windows.Forms.TextBox();
			this.txt_MenuName = new System.Windows.Forms.TextBox();
			this.txt_MenuCaption = new System.Windows.Forms.TextBox();
			this.label1 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			this.cmd_Cancel = new System.Windows.Forms.Button();
			this.cmd_Exit = new System.Windows.Forms.Button();
			this.label4 = new System.Windows.Forms.Label();
			this.txt_MenuLevel = new System.Windows.Forms.TextBox();
			this.txt_MenuCaptionEN = new System.Windows.Forms.TextBox();
			this.label5 = new System.Windows.Forms.Label();
			this.cbo_LoadFormName = new System.Windows.Forms.ComboBox();
			this.label6 = new System.Windows.Forms.Label();
			this.cmd_Add = new System.Windows.Forms.Button();
			this.cmd_Del = new System.Windows.Forms.Button();
			this.cmd_Modify = new System.Windows.Forms.Button();
			this.cmd_Refresh = new System.Windows.Forms.Button();
			this.cmd_Save = new System.Windows.Forms.Button();
			this.chk_Visible = new System.Windows.Forms.CheckBox();
			this.SuspendLayout();
			// 
			// tre_MainMenu
			// 
			this.tre_MainMenu.BackColor = System.Drawing.Color.White;
			this.tre_MainMenu.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
			this.tre_MainMenu.FullRowSelect = true;
			this.tre_MainMenu.HotTracking = true;
			this.tre_MainMenu.ImageIndex = -1;
			this.tre_MainMenu.Location = new System.Drawing.Point(8, 10);
			this.tre_MainMenu.Name = "tre_MainMenu";
			this.tre_MainMenu.Nodes.AddRange(new System.Windows.Forms.TreeNode[] {
																					 new System.Windows.Forms.TreeNode("管理信息系统", new System.Windows.Forms.TreeNode[] {
																																										 new System.Windows.Forms.TreeNode("报表管理"),
																																										 new System.Windows.Forms.TreeNode("采购管理", new System.Windows.Forms.TreeNode[] {
																																																														   new System.Windows.Forms.TreeNode("采购单"),
																																																														   new System.Windows.Forms.TreeNode("到货单")}),
																																										 new System.Windows.Forms.TreeNode("单据设置"),
																																										 new System.Windows.Forms.TreeNode("库存管理", new System.Windows.Forms.TreeNode[] {
																																																														   new System.Windows.Forms.TreeNode("出库单"),
																																																														   new System.Windows.Forms.TreeNode("调拨单"),
																																																														   new System.Windows.Forms.TreeNode("入库单"),
																																																														   new System.Windows.Forms.TreeNode("生产计划单")}),
																																										 new System.Windows.Forms.TreeNode("审核管理", new System.Windows.Forms.TreeNode[] {
																																																														   new System.Windows.Forms.TreeNode("出库审核"),
																																																														   new System.Windows.Forms.TreeNode("销售审核")}),
																																										 new System.Windows.Forms.TreeNode("销售管理", new System.Windows.Forms.TreeNode[] {
																																																														   new System.Windows.Forms.TreeNode("合同评审报告"),
																																																														   new System.Windows.Forms.TreeNode("送货单"),
																																																														   new System.Windows.Forms.TreeNode("销售单"),
																																																														   new System.Windows.Forms.TreeNode("销售合同"),
																																																														   new System.Windows.Forms.TreeNode("销售合同"),
																																																														   new System.Windows.Forms.TreeNode("销售意向单")}),
																																										 new System.Windows.Forms.TreeNode("帐务管理", new System.Windows.Forms.TreeNode[] {
																																																														   new System.Windows.Forms.TreeNode("期初收款单"),
																																																														   new System.Windows.Forms.TreeNode("其他收款"),
																																																														   new System.Windows.Forms.TreeNode("销售收款"),
																																																														   new System.Windows.Forms.TreeNode("销售预付款")}),
																																										 new System.Windows.Forms.TreeNode("质量管理", new System.Windows.Forms.TreeNode[] {
																																																														   new System.Windows.Forms.TreeNode("验质单")})}),
																					 new System.Windows.Forms.TreeNode("退出")});
			this.tre_MainMenu.SelectedImageIndex = -1;
			this.tre_MainMenu.Size = new System.Drawing.Size(302, 364);
			this.tre_MainMenu.TabIndex = 2;
			this.tre_MainMenu.MouseDown += new System.Windows.Forms.MouseEventHandler(this.tre_MainMenu_MouseDown);
			this.tre_MainMenu.Click += new System.EventHandler(this.tre_MainMenu_Click);
			this.tre_MainMenu.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.tre_MainMenu_AfterSelect);
			// 
			// txt_MenuCode
			// 
			this.txt_MenuCode.Location = new System.Drawing.Point(324, 36);
			this.txt_MenuCode.Name = "txt_MenuCode";
			this.txt_MenuCode.Size = new System.Drawing.Size(260, 21);
			this.txt_MenuCode.TabIndex = 3;
			this.txt_MenuCode.Text = "";
			// 
			// txt_MenuName
			// 
			this.txt_MenuName.Location = new System.Drawing.Point(324, 88);
			this.txt_MenuName.Name = "txt_MenuName";
			this.txt_MenuName.Size = new System.Drawing.Size(260, 21);
			this.txt_MenuName.TabIndex = 4;
			this.txt_MenuName.Text = "";
			// 
			// txt_MenuCaption
			// 
			this.txt_MenuCaption.Location = new System.Drawing.Point(324, 142);
			this.txt_MenuCaption.Name = "txt_MenuCaption";
			this.txt_MenuCaption.Size = new System.Drawing.Size(260, 21);
			this.txt_MenuCaption.TabIndex = 5;
			this.txt_MenuCaption.Text = "";
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(324, 12);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(212, 22);
			this.label1.TabIndex = 6;
			this.label1.Text = "菜单编码：";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(324, 64);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(212, 16);
			this.label2.TabIndex = 7;
			this.label2.Text = "菜单名称：";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(324, 222);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(212, 20);
			this.label3.TabIndex = 8;
			this.label3.Text = "菜单级别：    1、2、3、4、5";
			// 
			// cmd_Cancel
			// 
			this.cmd_Cancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.cmd_Cancel.Enabled = false;
			this.cmd_Cancel.Location = new System.Drawing.Point(382, 386);
			this.cmd_Cancel.Name = "cmd_Cancel";
			this.cmd_Cancel.Size = new System.Drawing.Size(70, 26);
			this.cmd_Cancel.TabIndex = 49;
			this.cmd_Cancel.Text = "取消(&C)";
			this.cmd_Cancel.Click += new System.EventHandler(this.cmd_Cancel_Click);
			// 
			// cmd_Exit
			// 
			this.cmd_Exit.BackColor = System.Drawing.SystemColors.Control;
			this.cmd_Exit.ForeColor = System.Drawing.SystemColors.ControlText;
			this.cmd_Exit.Location = new System.Drawing.Point(466, 386);
			this.cmd_Exit.Name = "cmd_Exit";
			this.cmd_Exit.Size = new System.Drawing.Size(70, 26);
			this.cmd_Exit.TabIndex = 48;
			this.cmd_Exit.Text = "退出(&Q)";
			this.cmd_Exit.Click += new System.EventHandler(this.cmd_Exit_Click);
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(324, 116);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(212, 18);
			this.label4.TabIndex = 50;
			this.label4.Text = "菜单标题(中文)：";
			// 
			// txt_MenuLevel
			// 
			this.txt_MenuLevel.Location = new System.Drawing.Point(324, 248);
			this.txt_MenuLevel.Name = "txt_MenuLevel";
			this.txt_MenuLevel.Size = new System.Drawing.Size(260, 21);
			this.txt_MenuLevel.TabIndex = 51;
			this.txt_MenuLevel.Text = "";
			// 
			// txt_MenuCaptionEN
			// 
			this.txt_MenuCaptionEN.Location = new System.Drawing.Point(324, 192);
			this.txt_MenuCaptionEN.Name = "txt_MenuCaptionEN";
			this.txt_MenuCaptionEN.Size = new System.Drawing.Size(260, 21);
			this.txt_MenuCaptionEN.TabIndex = 53;
			this.txt_MenuCaptionEN.Text = "";
			// 
			// label5
			// 
			this.label5.Location = new System.Drawing.Point(324, 320);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(212, 20);
			this.label5.TabIndex = 52;
			this.label5.Text = "单击事件连接窗体：";
			// 
			// cbo_LoadFormName
			// 
			this.cbo_LoadFormName.Location = new System.Drawing.Point(324, 352);
			this.cbo_LoadFormName.Name = "cbo_LoadFormName";
			this.cbo_LoadFormName.Size = new System.Drawing.Size(260, 20);
			this.cbo_LoadFormName.TabIndex = 54;
			// 
			// label6
			// 
			this.label6.Location = new System.Drawing.Point(324, 168);
			this.label6.Name = "label6";
			this.label6.Size = new System.Drawing.Size(212, 18);
			this.label6.TabIndex = 55;
			this.label6.Text = "菜单标题(英文)：";
			// 
			// cmd_Add
			// 
			this.cmd_Add.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.cmd_Add.Location = new System.Drawing.Point(98, 386);
			this.cmd_Add.Name = "cmd_Add";
			this.cmd_Add.Size = new System.Drawing.Size(70, 26);
			this.cmd_Add.TabIndex = 56;
			this.cmd_Add.Text = "添加(&A)";
			this.cmd_Add.Click += new System.EventHandler(this.cmd_Add_Click);
			// 
			// cmd_Del
			// 
			this.cmd_Del.Location = new System.Drawing.Point(311, 386);
			this.cmd_Del.Name = "cmd_Del";
			this.cmd_Del.Size = new System.Drawing.Size(70, 26);
			this.cmd_Del.TabIndex = 57;
			this.cmd_Del.Text = "删除(&D)";
			this.cmd_Del.Click += new System.EventHandler(this.cmd_Del_Click);
			// 
			// cmd_Modify
			// 
			this.cmd_Modify.Location = new System.Drawing.Point(169, 386);
			this.cmd_Modify.Name = "cmd_Modify";
			this.cmd_Modify.Size = new System.Drawing.Size(70, 26);
			this.cmd_Modify.TabIndex = 58;
			this.cmd_Modify.Text = "修改(&M)";
			this.cmd_Modify.Click += new System.EventHandler(this.cmd_Modify_Click);
			// 
			// cmd_Refresh
			// 
			this.cmd_Refresh.Location = new System.Drawing.Point(6, 386);
			this.cmd_Refresh.Name = "cmd_Refresh";
			this.cmd_Refresh.Size = new System.Drawing.Size(70, 26);
			this.cmd_Refresh.TabIndex = 59;
			this.cmd_Refresh.Text = "刷新(&R)";
			this.cmd_Refresh.Click += new System.EventHandler(this.cmd_Refresh_Click);
			// 
			// cmd_Save
			// 
			this.cmd_Save.Enabled = false;
			this.cmd_Save.Location = new System.Drawing.Point(240, 386);
			this.cmd_Save.Name = "cmd_Save";
			this.cmd_Save.Size = new System.Drawing.Size(70, 26);
			this.cmd_Save.TabIndex = 60;
			this.cmd_Save.Text = "保存(&S)";
			this.cmd_Save.Click += new System.EventHandler(this.cmd_Save_Click);
			// 
			// chk_Visible
			// 
			this.chk_Visible.Location = new System.Drawing.Point(326, 284);
			this.chk_Visible.Name = "chk_Visible";
			this.chk_Visible.Size = new System.Drawing.Size(204, 24);
			this.chk_Visible.TabIndex = 62;
			this.chk_Visible.Text = "是否可见";
			// 
			// frmMenuEdit
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(592, 431);
			this.Controls.Add(this.chk_Visible);
			this.Controls.Add(this.cmd_Save);
			this.Controls.Add(this.cmd_Refresh);
			this.Controls.Add(this.cmd_Modify);
			this.Controls.Add(this.cmd_Del);
			this.Controls.Add(this.cmd_Add);
			this.Controls.Add(this.label6);
			this.Controls.Add(this.cbo_LoadFormName);
			this.Controls.Add(this.txt_MenuCaptionEN);
			this.Controls.Add(this.txt_MenuLevel);
			this.Controls.Add(this.txt_MenuCaption);
			this.Controls.Add(this.txt_MenuName);
			this.Controls.Add(this.txt_MenuCode);
			this.Controls.Add(this.label5);
			this.Controls.Add(this.label4);
			this.Controls.Add(this.cmd_Cancel);
			this.Controls.Add(this.cmd_Exit);
			this.Controls.Add(this.label3);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.tre_MainMenu);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.MaximizeBox = false;
			this.Name = "frmMenuEdit";
			this.Text = "菜单编辑器";
			this.Load += new System.EventHandler(this.frmMenuEdit_Load);
			this.ResumeLayout(false);

		}
		#endregion

		private void frmMenuEdit_Load(object sender, System.EventArgs e)
		{
			MenuRefresh();
			//System.Windows.Forms.Shortcut sc=new System.Windows.Forms.Shortcut();

			System.Reflection.Assembly MyAssembly = Assembly.GetExecutingAssembly();
				
			//System.Reflection.Module[] modules=  MyAssembly.GetModules();
			
			System.Type[] typs =  MyAssembly.GetTypes();
			int rs=typs.Length;
			for (int i = 0; i < rs; i ++)
			{
				cbo_LoadFormName.Items.Add(typs[i].ToString())  ;
			}

		}

		private void tre_MainMenu_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
		{
		
		}
		private void tre_MainMenu_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			try
			{
				tre_MainMenu.SelectedNode = tre_MainMenu.GetNodeAt(e.X,e.Y);	
			}
			catch
			{
				return;
			}
		}
		private void tre_MainMenu_Click(object sender, System.EventArgs e)
		{
			string sSQL="";
			sSQL="SELECT fcNodeId,  fcMenuCaption, fcMenuCaptionEn, fiLevel,fbVisible,";
			sSQL=sSQL+"	fcLoadFormName";
			sSQL=sSQL+" FROM S_MenuItem ";

			TreeNode tn = new TreeNode();
			tn = tre_MainMenu.SelectedNode;
			if (tn != null)
			{
				System.Data.DataSet dst = new System.Data.DataSet();
				dst= newADO.GetDataSet(sSQL+" Where fcNodeId='"+tn.Tag.ToString()+"'");
				System.Data.DataRow dr =dst.Tables[0].Rows[0];
				txt_MenuCode.Text =dr[0].ToString();
//				txt_MenuName.Text =dr[1].ToString();
				txt_MenuCaption.Text=dr[1].ToString();
				txt_MenuCaptionEN.Text =dr[2].ToString();
				txt_MenuLevel.Text =dr[3].ToString();
				if (dr[4].ToString()=="True") {chk_Visible.Checked=true;} 
				else{chk_Visible.Checked=false;}
				cbo_LoadFormName.Text =dr[5].ToString();
				
			}
	
		}

		private void cmd_Cancel_Click(object sender, System.EventArgs e)
		{
			w_EidtFlag=false;
			CmdEnable(true);
		}

		private void cmd_Add_Click(object sender, System.EventArgs e)
		{
			txt_MenuCode.Text ="";
//			txt_MenuName.Text ="";
			txt_MenuCaption.Text="";
			txt_MenuCaptionEN.Text ="";
			txt_MenuLevel.Text ="";
			cbo_LoadFormName.Text ="";
			chk_Visible.Checked=false;
			CmdEnable(false);
			w_EidtFlag=false;

		}

		private void cmd_Del_Click(object sender, System.EventArgs e)
		{
			string sSQL="";
			sSQL="Delete  FROM S_MenuItem Where fcNodeId='"+txt_MenuCode.Text+"'";
			try
			{
				if (newADO.ExeSql(sSQL)>=0)
				{
					MenuRefresh();
				}
			}
			catch
			{
			
			}
		}

		private void cmd_Modify_Click(object sender, System.EventArgs e)
		{
				w_EidtFlag=true;
				CmdEnable(false);
				w_keyId=txt_MenuCode.Text;
		}

		private void cmd_Save_Click(object sender, System.EventArgs e)
		{
		RecordSave();

		}
		private void RecordSave()
		{
			string sSQL="";
			string sVisible="0";
			if (chk_Visible.Checked==true) {sVisible="1";}
			if (txt_MenuCode.Text=="") 
			{
				MessageBox.Show("菜单编码不能为空！", "提示", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information );
				return;
			}
//			if (txt_MenuName.Text=="") 
//			{
//				MessageBox.Show("菜单名称不能为空！", "提示", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information );
//				return;
//			}

			if (txt_MenuLevel.Text=="") 
			{
				MessageBox.Show("菜单级别不能为空！", "提示", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information );
				return;
			}


			if (w_EidtFlag==false)
			{

				sSQL="Insert Into S_MenuItem (fcNodeId, fcMenuCaption, fcMenuCaptionEn, fiLevel,fbVisible, ";
				sSQL=sSQL+"	fcLoadFormName) Values ('"+txt_MenuCode.Text+"','"+txt_MenuCaption.Text+"','"+txt_MenuCaptionEN.Text+"',"+txt_MenuLevel.Text+","+sVisible+",'"+cbo_LoadFormName.Text+"')";
			}
			else
			{
				sSQL="Update S_MenuItem set fcNodeId='"+txt_MenuCode.Text+"',fcMenuCaption='"+txt_MenuCaption.Text+"', fcMenuCaptionEn='"+txt_MenuCaptionEN.Text+"', fiLevel="+txt_MenuLevel.Text+",fbVisible="+sVisible+",";
				sSQL=sSQL+"	fcLoadFormName ='"+cbo_LoadFormName.Text+"' Where fcNodeId='"+w_keyId+"'";

			}
				 
			if (newADO.ExeSql(sSQL)>=0)
			{
				MenuRefresh();
				CmdEnable(true);
				w_EidtFlag=false;
			}
		}
		private void MenuRefresh()
		{
			InitData.InitTreeView (tre_MainMenu,newDb.GetDataArray(newADO.GetDataSet("Sys_ProMenuItem 'MenuEdit'")));
	
		}
		private void CmdEnable(bool p_YesNo)
		{
			cmd_Add.Enabled =p_YesNo;
			cmd_Modify.Enabled =p_YesNo;
			cmd_Save.Enabled =! p_YesNo;
			cmd_Del.Enabled =p_YesNo;
			cmd_Cancel.Enabled =!p_YesNo;
			cmd_Refresh.Enabled =p_YesNo;
			cmd_Exit.Enabled =p_YesNo;
		
		}
		private void cmd_Refresh_Click(object sender, System.EventArgs e)
		{
			MenuRefresh();
		}

		private void cmd_Exit_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}
	}
}
