using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DBFactory;
namespace wcfControlMonitorClient
{
    /// <summary>
    /// Creator:Richard.liu
    /// 用户和权限管理
    /// </summary>
    public partial class FrmUsers : Form
    {
        bool _ifupdate=false ;
        DBOperator dbo= CStaticClass.dbo;
        
        string CommomSql = "select f_userid as 用户ID,f_username as 用户名称,f_purview as 权限 from t_base_users";
        private static FrmUsers _formInstance;
        public static FrmUsers FormInstance
        {
            get
            {
                if (_formInstance == null)
                {
                    _formInstance = new FrmUsers();
                }
                return _formInstance;
            }
            set { _formInstance = value; }
        }
        public FrmUsers()
        {
            InitializeComponent();

            foreach (ToolStripMenuItem tsmi in CStaticClass.client.menuStrip1.Items)
            {
                if (tsmi.Name == "MMmasterAdmin") continue;
                this.treeView1.Nodes.Add(tsmi.Name, tsmi.Text);
                
                
                foreach (ToolStripItem tsi in tsmi.DropDown.Items)
                {
                    if (tsi.Name.IndexOf("MI") >= 0)
                    {
                        this.treeView1.Nodes[tsmi.Name].Nodes.Add(tsi.Name, tsi.Text);
                        //this.checkedListBox1.Items.Add("    "+tsi.Name + ":" + tsi.Text);
                    }
                }
            }
            
            _formInstance = this;
        }
        

       

        private void button2_Click(object sender, EventArgs e)
        {
            DataView dv = dbo.ExceSQL(CommomSql).Tables[0].DefaultView;
            this.dataGridView1.DataSource = dv;
        }

        private void btQuery_Click(object sender, EventArgs e)
        {
            try
            {
                string df, sql;
                if ((this.tbContent.Text.Trim() == "") || (this.cbField.Text.Trim() == "")) return;
                if (this.cbField.Text == "用户ID")
                {
                    df = "F_UserID";
                }
                else if (this.cbField.Text == "姓名")
                {
                    df = "f_username";
                }
                else//
                {
                    return ;
                }
                sql = CommomSql + " where " + df + "= '" + this.tbContent.Text + "'";
                DataSet ds = dbo.ExceSQL(sql);
                this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void btNull_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.treeView1.Nodes.Count; i++)
            {
               this.treeView1.Nodes[i].Checked=false ;
               for (int ii = 0; ii < this.treeView1.Nodes[i].Nodes.Count; ii++)
               {
                   this.treeView1.Nodes[i].Nodes[ii].Checked = false;

               }
            }
            
        }

        private void btSave_Click(object sender, EventArgs e)
        {
            string ts = string.Empty;
            if (this.checkBox1.Checked == true)
            {
                ts = "增加";
            }
            else
            {
                ts = "修改";
            }
            if (MessageBox.Show("您确认要"+ts+"用户信息吗?", "操作提示:", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.OK)
            {
                return;
            }


            if (this.tbUserID.Text.Trim().Length == 0)
            {
                MessageBox.Show("用户ID不允许输入空值!", "误操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.tbUserID.Focus();
                return;
            }
            if (this.tbUserName.Text.Trim()=="")
            {
                MessageBox.Show("用户姓名不允许输入空值!", "误操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.tbUserName.Focus();
                return;
            }
            if (this.tbPassword.Text.Trim().Length == 0)
            {
                MessageBox.Show("用户密码不允许输入空值!", "误操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.tbPassword.Focus();
                return;
            }


            string pur=string.Empty ;
            for (int i = 0; i < this.treeView1.Nodes.Count;i++ )
            {
                int scount = 0;
                for (int ii = 0; ii < this.treeView1.Nodes[i].Nodes.Count; ii++)
                {
                    if (this.treeView1.Nodes[i].Nodes[ii].Checked == true)
                    {
                        pur += ";" + treeView1.Nodes[i].Nodes[ii].Name;
                        this.treeView1.Nodes[i].Checked = true;
                    }
                    else
                    {
                        scount++;
                    }
                }
                if ((this.treeView1.Nodes[i].Nodes.Count != scount) || ((scount == 0) && (this.treeView1.Nodes[i].Nodes.Count==0)))
                {
                    if (this.treeView1.Nodes[i].Checked == true)
                    {
                        pur += ";" + treeView1.Nodes[i].Name;
                    }
                }
            }
            pur = pur.Substring(1);
            string sql = "";
            
            try
            {
                if (_ifupdate == true)
                {
                    sql = "UPDATE t_base_users  SET f_username = '" + this.tbUserName.Text.Trim() + "',f_purview='" + pur + "',f_password='" + tbPassword.Text.Trim() + "' where f_userid= '" + this.tbUserID.Text + "'";
                    dbo.ExceSQL(sql);
                    MessageBox.Show("用户信息修改成功!", "操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    
                }
                else
                {
                    DataView dc = dbo.ExceSQL("select f_userid from t_base_users   where f_userid= '" + this.tbUserID.Text + "'").Tables[0].DefaultView;
                    if (dc.Count > 0)
                    {
                        MessageBox.Show("用户信息已经存在!", "操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    sql = "insert into t_base_users(f_userid,f_username,f_purview,f_password) values('" + this.tbUserID.Text.Trim() + "','" + this.tbUserName.Text.Trim() + "','" + pur + "','" + tbPassword.Text.Trim() + "')";
                    dbo.ExceSQL(sql);
                    MessageBox.Show("用户信息增加成功!", "操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    
                }
                sql = CommomSql + " where f_userid= '" + this.tbUserID.Text + "'";
                DataSet ds = dbo.ExceSQL(sql);
                this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
                _ifupdate = false;
               // CommonClassLib.CCarryConvert.WriteDarkCasket("Login", "UserID:" + CStaticClass.UserID + "的操作日志", "点击【保存】按钮", "系统用户管理", "UserID:" + tbUserID.Text );
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void tsmEdit_Click(object sender, EventArgs e)
        {
            if (dataGridView1.RowCount <= 0)
            {
                return;
            }

            this.tabControl1.SelectTab("tabPage1");

            this.tbUserID.Text = this.dataGridView1.CurrentRow.Cells[0].Value.ToString() + "";
            DataView dv = dbo.ExceSQL("select f_username as UserName,f_purview as Purview,f_password as password from t_base_users where f_userid='" + this.tbUserID.Text + "'").Tables[0].DefaultView;
            if (dv.Count > 0)
            {
                this.tbUserName.Text = dv[0]["UserName"].ToString() + "";
                this.tbPassword.Text = dv[0]["password"].ToString() + "";
                char[] cc = new char[1] { ':' };
                
                for (int i = 0; i < this.treeView1.Nodes.Count; i++)
                {

                    if (dv[0]["Purview"].ToString().IndexOf(this.treeView1.Nodes[i].Name) >= 0)
                    {

                        this.treeView1.Nodes[i].Checked = true;
                    }
                    else
                    {
                        this.treeView1.Nodes[i].Checked = false;
                    }
                    for (int ii = 0; ii < this.treeView1.Nodes[i].Nodes.Count; ii++)
                    {

                        if (dv[0]["Purview"].ToString().IndexOf(this.treeView1.Nodes[i].Nodes[ii].Name) >= 0)
                        {

                            this.treeView1.Nodes[i].Nodes[ii].Checked = true;
                        }
                        else
                        {
                            this.treeView1.Nodes[i].Nodes[ii].Checked = false;
                        }

                    }
                }
            _ifupdate =true ;
            this.checkBox1.Checked = false;
            }
            
        }

        

        private void ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("您确认要删除用户信息吗?", "操作提示:", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.OK)
            {
                return;
            }
            dbo.ExceSQL("delete from t_base_users where f_userid='" + this.dataGridView1.CurrentRow.Cells[0].Value.ToString() + "'");
            button2_Click(sender, e);
            //CommonClassLib.CCarryConvert.WriteDarkCasket("Login", "UserID:" + CStaticClass.UserID + "的操作日志", "点击【删除】菜单", "系统用户管理的右键菜单", "UserID:" + tbUserID.Text);
        }

        private void FrmUsers_Load(object sender, EventArgs e)
        {
           
            
        }

        private void btAll_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.treeView1.Nodes.Count; i++)
            {
                this.treeView1.Nodes[i].Checked = true;
                for (int ii = 0; ii < this.treeView1.Nodes[i].Nodes.Count; ii++)
                {
                    this.treeView1.Nodes[i].Nodes[ii].Checked = true;

                }
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBox1.Checked == true)
            {
                _ifupdate = false;
            }
            else
            {
                _ifupdate = true;
            }
        }

        private void cbField_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                string df, sql;
                if (this.cbField.Text == "用户ID")
                {
                    df = "F_UserID";
                }
                else if (this.cbField.Text == "姓名")
                {
                    df = "f_username";
                }
                else//
                {
                    return;
                }
                sql = " select distinct " + df + " from t_base_users";
                DataView dv = dbo.ExceSQL(sql).Tables[0].DefaultView;
                tbContent.DataSource = null;
                tbContent.DisplayMember = df;
                tbContent.ValueMember = df;
                tbContent.DataSource = dv;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if ((e.RowIndex >= 0) && (e.Button == MouseButtons.Right))
            {
                dataGridView1.ClearSelection();
                dataGridView1.Rows[e.RowIndex].Selected = true;
                dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];

            }
        }



        //private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
        //{
        //    try
        //    {
        //        if (e.Node.Parent == null)
        //        {
        //            TreeNode tnc = e.Node;
        //            if (tnc.Checked == false)
        //            {
        //                for (int i = 0; i < tnc.Nodes.Count; i++)
        //                {
        //                    tnc.Nodes[i].Checked = false;
        //                }
        //            }
        //            return;
        //        }
        //        TreeNode tn = e.Node.Parent;
        //        if (tn.Nodes.Count > 0)
        //        {
        //            int icount = 0;
        //            for (int i = 0; i < tn.Nodes.Count; i++)
        //            {
        //                if (tn.Nodes[i].Checked == true)
        //                {
        //                    tn.Checked = true;
        //                }
        //                else
        //                {
        //                    icount++;
        //                }
        //            }
        //            //if (tn.Nodes.Count == icount)
        //            //{
        //            //    tn.Checked = false;
        //            //}
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        throw ex;
        //    }
        //}

       


    }
}