using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace JXC_MIS.UserTier.CommonForm
{
	public class frmRightSetup : JXC_MIS.UserTier.BaseForm.frmTreeFlexGrid
	{
		private System.ComponentModel.IContainer components = null;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.ComboBox cbo_RightLevel;
		private System.Windows.Forms.TextBox txt_Pws;
		private System.Windows.Forms.TextBox txt_Operator;
		private System.Data.DataSet dst = new System.Data.DataSet();
//		protected ClsDataBase newDB=new ClsDataBase() ;
//		protected ClsReceiptAttrib newR_Attrib=new ClsReceiptAttrib();
//		//		protected ClsReceiptMethod newR_Method=new ClsReceiptMethod();
//		protected ClsCellAttrib newCellAttrib=new ClsCellAttrib();
      private bool w_EidtFlag;
	  private	string w_keyId;

		public frmRightSetup()
		{
			// 该调用是 Windows 窗体设计器所必需的。
			InitializeComponent();

			// TODO: 在 InitializeComponent 调用后添加任何初始化
		}

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

		#region 设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.cbo_RightLevel = new System.Windows.Forms.ComboBox();
			this.txt_Operator = new System.Windows.Forms.TextBox();
			this.label1 = new System.Windows.Forms.Label();
			this.txt_Pws = new System.Windows.Forms.TextBox();
			this.SuspendLayout();
			// 
			// panel1
			// 
			this.panel1.Name = "panel1";
			this.panel1.Size = new System.Drawing.Size(710, 446);
			// 
			// spt_H
			// 
			this.spt_H.Location = new System.Drawing.Point(3, 0);
			this.spt_H.Name = "spt_H";
			this.spt_H.Size = new System.Drawing.Size(707, 3);
			// 
			// spt_V
			// 
			this.spt_V.Location = new System.Drawing.Point(0, 0);
			this.spt_V.Name = "spt_V";
			this.spt_V.Size = new System.Drawing.Size(3, 446);
			// 
			// tv_Tree
			// 
			this.tv_Tree.Name = "tv_Tree";
			this.tv_Tree.Size = new System.Drawing.Size(0, 446);
			// 
			// cbo_SetupName
			// 
			this.cbo_SetupName.Name = "cbo_SetupName";
			this.cbo_SetupName.Size = new System.Drawing.Size(124, 20);
			// 
			// t_Find
			// 
			this.t_Find.Visible = false;
			// 
			// pnl_Filter
			// 
			this.pnl_Filter.Name = "pnl_Filter";
			// 
			// cmd_Cancel
			// 
			this.cmd_Cancel.Name = "cmd_Cancel";
			// 
			// cmd_ExeQuery
			// 
			this.cmd_ExeQuery.Name = "cmd_ExeQuery";
			// 
			// grd_Filter
			// 
			this.grd_Filter.Name = "grd_Filter";
			// 
			// sbr_StatusBar
			// 
			this.sbr_StatusBar.Name = "sbr_StatusBar";
			this.sbr_StatusBar.Size = new System.Drawing.Size(710, 25);
			// 
			// t_Modify
			// 
			this.t_Modify.Visible = true;
			// 
			// tbr_Toolbar
			// 
			this.tbr_Toolbar.Name = "tbr_Toolbar";
			this.tbr_Toolbar.Size = new System.Drawing.Size(710, 28);
			// 
			// t_new
			// 
			this.t_new.Visible = true;
			// 
			// t_Save
			// 
			this.t_Save.Visible = true;
			// 
			// t_Cancel
			// 
			this.t_Cancel.Visible = true;
			// 
			// grd_Top
			// 
			this.grd_Top.Location = new System.Drawing.Point(3, 0);
			this.grd_Top.Name = "grd_Top";
			this.grd_Top.Size = new System.Drawing.Size(707, 0);
			// 
			// grd_Bottom
			// 
			this.grd_Bottom.Location = new System.Drawing.Point(3, 3);
			this.grd_Bottom.Name = "grd_Bottom";
			this.grd_Bottom.Size = new System.Drawing.Size(705, 443);
			this.grd_Bottom.Grid_Click += new ljs.FlexGridGrid_Click(this.grd_Bottom_Grid_Click);
			// 
			// TxtTemp
			// 
			this.TxtTemp.Name = "TxtTemp";
			// 
			// t_Menu
			// 
			this.t_Menu.Visible = false;
			// 
			// t_Delete
			// 
			this.t_Delete.Visible = true;
			// 
			// img_ImageList
			// 
			this.img_ImageList.ImageStream = null;
			// 
			// btnDown
			// 
			this.btnDown.Name = "btnDown";
			// 
			// cbo_RightLevel
			// 
			this.cbo_RightLevel.Items.AddRange(new object[] {
																"普通权限",
																"超级权限"});
			this.cbo_RightLevel.Location = new System.Drawing.Point(602, 6);
			this.cbo_RightLevel.Name = "cbo_RightLevel";
			this.cbo_RightLevel.Size = new System.Drawing.Size(108, 20);
			this.cbo_RightLevel.TabIndex = 23;
			// 
			// txt_Operator
			// 
			this.txt_Operator.Location = new System.Drawing.Point(306, 4);
			this.txt_Operator.Name = "txt_Operator";
			this.txt_Operator.Size = new System.Drawing.Size(82, 21);
			this.txt_Operator.TabIndex = 24;
			this.txt_Operator.Text = "";
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(256, 8);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(428, 14);
			this.label1.TabIndex = 25;
			this.label1.Text = "操作员：              密码：                    权限级别：";
			// 
			// txt_Pws
			// 
			this.txt_Pws.Location = new System.Drawing.Point(428, 4);
			this.txt_Pws.Name = "txt_Pws";
			this.txt_Pws.PasswordChar = '*';
			this.txt_Pws.Size = new System.Drawing.Size(106, 21);
			this.txt_Pws.TabIndex = 26;
			this.txt_Pws.Text = "";
			// 
			// frmRightSetup
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(710, 499);
			this.Controls.Add(this.txt_Pws);
			this.Controls.Add(this.cbo_RightLevel);
			this.Controls.Add(this.txt_Operator);
			this.Controls.Add(this.label1);
			this.Name = "frmRightSetup";
			this.Load += new System.EventHandler(this.frmRightSetup_Load);
			this.Controls.SetChildIndex(this.tbr_Toolbar, 0);
			this.Controls.SetChildIndex(this.sbr_StatusBar, 0);
			this.Controls.SetChildIndex(this.panel1, 0);
			this.Controls.SetChildIndex(this.pnl_Filter, 0);
			this.Controls.SetChildIndex(this.label1, 0);
			this.Controls.SetChildIndex(this.txt_Operator, 0);
			this.Controls.SetChildIndex(this.cbo_RightLevel, 0);
			this.Controls.SetChildIndex(this.txt_Pws, 0);
			this.ResumeLayout(false);

		}
		#endregion




		private void frmRightSetup_Load(object sender, System.EventArgs e)
		{
			tv_Tree.Width =140;
			spt_H.Visible =false;
			grd_Top.Visible =false;
			this.ShowResize();
			RightRefresh();
			ShowData(grd_Bottom,txt_Operator.Text);
			grd_Bottom.SetColWidth("500,600,600,3000,600,600"); 
			ToolEnabled(true);

		}
		protected override void tv_Tree_Click(object sender, EventArgs e)
		{

			TreeNode tn = new TreeNode();
			tn = tv_Tree.SelectedNode;
			if (tn != null)
			{
				try
				{
					string[] arrTemp=tn.Text.Split(':')  ;
					this.txt_Operator.Text =arrTemp[0];
					this.cbo_RightLevel.Text= arrTemp[1];
					ShowData(grd_Bottom,txt_Operator.Text);

//					ShowData(grd_Bottom,"SELECT  '' as 选中标记,fcRightDesc AS 菜单名称, fbRightRead AS 只读, fbRightWrite AS 可编辑 FROM I_RightList Where fcOperator='"+txt_Operator.Text+"'");
				}
				catch
				{
				}
			}

		}
		private  void ShowData(ljs.MyFlexGrid p_FlexGrid ,string p_Filter)
		{
			string sSql="Select  Top 100 Percent (CASE isnull(T .代码, '')  WHEN '' THEN '' ELSE '√' END) as 选中标记,fcNodeId 菜单代码, fcMenuCaption 菜单名称,T.只读,T.编辑 from (SELECT  fcMenuCode 代码,(case fbRightRead When 1 then '√' else '' end ) AS 只读, (case fbRightWrite When 1 then '√' else '' end ) AS 编辑 FROM I_RightList  ";
			sSql=sSql+" Where fcOperator='"+p_Filter+"'";
			sSql=sSql+" ) T Right OUTER JOIN S_MenuItem ON S_MenuItem.fcNodeId = T.代码 Where fiLevel<>0 ORDER BY S_MenuItem.fcNodeId";
			p_FlexGrid.Clear();
			InitData.ShowGridData(p_FlexGrid,"up_ljs_RightList '"+p_Filter+"'");

		}
		private void grd_Bottom_Grid_Click(object sender, System.EventArgs e)
		{
			grd_Bottom.OnlyRead=false;

			if (grd_Bottom.Col==1 || grd_Bottom.Col==4  || grd_Bottom.Col==5)
			{
				grd_Bottom.InputType=ljs.eInputType.CheckBox;
			}
			else
			{
				grd_Bottom.InputType=ljs.eInputType.None;
				grd_Bottom.OnlyRead=true;

			}

		}

		protected override void tbr_Toolbar_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
		{
			switch (e.Button.Tag.ToString())
			{
				case "新增":
					RecordAdd();
					break;

				case "删除":
					 RecordDel();
					break;
				case "保存":
                      RecordSave();
					break;
				case "修改":
					try
					{
						RecordModify();
					}
					catch
					{}
					break;
				case "放弃":
					    RecordCancel();
					break;
				case "预览":

					break;
				case "打印":
					//					ReceiptPrint();
					break;
				case "刷新":
					RightRefresh();
					break;
				case "查询":
					//					ReceiptFind();
//					InitFilter(m_colCaption);
					break;
				case "退出":
					this.Close();
					break;

				default:
 
					break;
			}
		}
		private void RecordDel()
		{
			string sSQL="";

			try
			{
				sSQL="Delete  FROM I_Operator Where fcOperator='"+txt_Operator.Text+"'";
				newADO.ExeSql(sSQL);
				sSQL="Delete  FROM I_RightList Where fcOperator='"+txt_Operator.Text+"'";
				newADO.ExeSql(sSQL);
				RightRefresh();
			}
			catch
			{
			
			}


		}
		private void RecordModify()
		{
            w_keyId=txt_Operator.Text;
			ToolEnabled(false);
			w_EidtFlag=true;

		}
		private void RecordCancel()
		{
			ToolEnabled(true);
			w_EidtFlag=false;

		}

		private void RecordSave()
		{
			string sSQL="";
			if (txt_Operator.Text=="") 
			{
				MessageBox.Show("操作员不能为空！", "提示", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information );
				return;
			}
//			if (txt_Pws.Text=="") 
//			{
//				MessageBox.Show("不能为空！", "提示", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information );
//				return;
//			}
//
			if (cbo_RightLevel.Text=="") 
			{
				MessageBox.Show("权限级别不能为空！", "提示", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information );
				return;
			}
			newADO.BeginTrans(); //事务开始

			if (w_EidtFlag==false)
			{
				sSQL="Insert Into I_Operator (fcOperator, fcPassWord, fcRightlevel";
				sSQL=sSQL+"	) Values ('"+txt_Operator.Text+"','"+txt_Pws.Text+"','"+cbo_RightLevel.Text+"')";
			}
			else
			{
				sSQL="Update I_Operator set fcOperator='"+txt_Operator.Text+"',fcPassWord='"+txt_Pws.Text+"', fcRightLevel='"+cbo_RightLevel.Text+"'";
				sSQL=sSQL+"	 Where fcOperator='"+w_keyId+"'";
				newADO.ExeSql("Delete From I_RightList Where fcOperator='"+w_keyId+"'"); 
			}
				 
			if (newADO.ExeSql(sSQL)<0)
			{
				newADO.Rollback() ;  //事务回滚
			}

			//明细表数据保存
			int cs=grd_Bottom.Cols;
			int rs=grd_Bottom.Rows;
			int iRead;
			int iWrite;
			for (int r=1;r<rs;r++)
			{
				if (grd_Bottom.GetCellText(r,1)!="" && grd_Bottom.GetCellText(r,1)=="√")
				{
					if (grd_Bottom.GetCellText(r,4)=="√") iRead=1;else iRead=0;
					if (grd_Bottom.GetCellText(r,5)=="√") iWrite=1;else iWrite=0;

					sSQL="Insert Into I_RightList (fcOperator, fcMenuCode, fbRightRead, fbRightWrite)";
					sSQL=sSQL+" Values ('"+txt_Operator.Text+"','"+grd_Bottom.GetCellText(r,2)+"',"+iRead.ToString() +","+iWrite.ToString()+")";
					if (newADO.ExeSql(sSQL)<0)
					{
						newADO.Rollback() ;  //事务回滚
						return ;
					}
				}

			}
			newADO.Commit();             //事务提交
			RightRefresh();
			w_EidtFlag=false;


			ToolEnabled(true);
		}
		private void ToolEnabled(bool p_TF)
		{
			this.t_Modify.Enabled =p_TF;
			this.t_new.Enabled =p_TF;
			this.t_Save.Enabled=!p_TF;
			this.t_Exit.Enabled =p_TF;
			this.t_Cancel.Enabled =!p_TF;
		}

		private void RecordAdd()
		{
			string sSql="SELECT '' as 选中标记,fcNodeId 菜单代码, fcMenuCaption 菜单名称, '' AS 只读, '√' AS 可编辑 FROM S_MenuItem  ";
			sSql=sSql+" Where fiLevel<>0 ";
			sSql=sSql+" ORDER BY S_MenuItem.fcNodeId ";
			grd_Bottom.Clear();
			InitData.ShowGridData(grd_Bottom,sSql);
			ToolEnabled(false);
			w_EidtFlag=false;

		}
		private void RightRefresh()
		{
			InitData.InitTreeView (tv_Tree,"SELECT fcOperator+':'+isnull(fcRightlevel,'普通权限') FROM I_Operator");
	
		}








	}
}

