using RGD.DBUtility;
using System;
using System.Data;
using System.Windows.Forms;

namespace RGD.WCS
{
    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; }
        }

        public FrmLogin()
        {
            _formInstance = this;
            InitializeComponent();
        }

        private void FrmLogin_Load(object sender, EventArgs e)
        {
            try
            {
                DataView dv = DbHelperSQL.Query("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;
                }

                this.textBox1.Text = "";
                this.comboBox1.SelectedItem = null;
                if (this.comboBox1.Items.Count > 0)
                {
                    this.comboBox1.SelectedIndex = 0;
                }
                this.textBox1.Text = "1";
            }
            catch (Exception ex)
            {
                MessageBox.Show("异常:" + ex.Message, "操作提示：", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Application.Exit();
            }
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            if (comboBox1.SelectedValue == null)
            {
                MessageBox.Show("用户ID值为空，禁止登录！", "操作提示：", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            DataView dv = DbHelperSQL.Query("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
            {
                this.Close();
                //FrmMain.FormInstance.WindowState = FormWindowState.Normal;
                FrmMain.FormInstance.Visible = true;
                this.DialogResult = DialogResult.OK;
            }
        }

        /// <summary>
        /// 退出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}