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 ControlSystem
{
    /// <summary>
    /// Creator:Richard.liu
    /// 登录界面
    /// </summary>
    public partial class FrmLogin : Form
    {
        private static FrmLogin _formInstance;
        public static FrmLogin FormInstance
        {
            get
            {
                if (_formInstance == null)
                {
                    _formInstance = new FrmLogin();
                }
                return _formInstance;
            }
            set { _formInstance = value; }
        }
        DBOperator dbo = CStaticClass.dbo;
        public FrmLogin()
        {
            InitializeComponent();
            _formInstance = this;
            if (dbo.Open() == false)
            {
                
                FrmConnectDB.FormInstance.ConnectType = "Monitor";
                FrmConnectDB.FormInstance.ShowDialog();
                Application.Exit();
            }
            


            DataView dv = dbo.ExceSQL("select distinct f_userid,f_username from t_base_users ").Tables[0].DefaultView ;
            if (dv.Count > 0)
            {
                this.comboBox1.DisplayMember = "f_username";
                this.comboBox1.ValueMember = "f_userid";
                this.comboBox1.DataSource = dv;
            }
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (comboBox1.SelectedValue== null)
            {
                MessageBox.Show("用户ID值为空，禁止登录！", "操作提示：", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            string pur = string.Empty;

            DataView dv = dbo.ExceSQL("select f_userid,f_purview,f_password from t_base_users where f_userid='" + comboBox1.SelectedValue + "' and f_password='"+this.textBox1.Text.Trim()+"'").Tables[0].DefaultView;
            if (dv.Count <= 0)
            {
                MessageBox.Show("用户信息不存在，禁止登录！", "操作提示：", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                
                CStaticClass.IfRemind = true;
                CStaticClass.UserID = comboBox1.SelectedValue.ToString();
                
                //设置用户权限（菜单是否显示）
                
                //dv = dbo.ExceSQL("select f_purview  from t_base_users where f_userid='" + CStaticClass.UserID + "'").Tables[0].DefaultView;
                //if (dv.Count > 0)
                //{
                //    foreach (ToolStripMenuItem tsmi in FrmMain.FormInstance.menuStrip1.Items)
                //    {
                //        foreach (ToolStripItem tsi in tsmi.DropDown.Items)
                //        {
                            
                //            if (dv[0]["f_purview"].ToString().IndexOf(tsi.Name) >= 0)
                //            {
                //                tsi.Visible = true;
                //            }
                //            else
                //            {
                //                tsi.Visible = false;
                //            }
                //            if (tsi.Name.IndexOf("Tss") >= 0)
                //            {
                //                tsi.Visible = true;
                //            }
                            
                //        }
                //        if (dv[0]["f_purview"].ToString().IndexOf(tsmi.Name) >= 0)
                //        {
                //            tsmi.Visible = true;
                //        }
                //        else
                //        {
                //            tsmi.Visible = false;
                //        }
                        
                //    }
                    
                //}
                CommonClassLib.CCarryConvert.WriteDarkCasket("Login", "用户名：" + this.comboBox1.SelectedValue.ToString() + "的操作日志", "成功登录系统", "", "");
                this.Visible = false;
                this.Close();
               
               // FrmMain.FormInstance.Visible = true;
               
                FrmConfigManage.FormInstance.ShowDialog();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
        //    CStaticClass.IfRemind = false;
        //    Application.Exit();
            
            
        }

        private void FrmLogin_Load(object sender, EventArgs e)
        {
            CStaticClass.UserID = "";
            this.textBox1.Text = "";
            this.comboBox1.SelectedItem = null; ;

        }
    }
}