using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MainTemplate
{
    public partial class frmLogin : Form
    {
        public frmLogin()
        {
            InitializeComponent();
        }

        private void Btn_Login_Click(object sender, EventArgs e)
        {
            if (Txt_UserName.Text == "" || Txt_Password.Text == "")
            {
                MessageBox.Show("ورود نام کاربری و کلمه عبور الزامی می باشد");
                return;
            }
            Class_Db_Connection db = new Class_Db_Connection();
            DataTable Dt = new DataTable();

            db.cmd.Parameters.Add("@UsrName", SqlDbType.NVarChar).Value = Txt_UserName.Text.Trim();
            db.cmd.Parameters.Add("@PsWord", SqlDbType.NVarChar).Value = Txt_Password.Text.Trim();

            Dt = db.dbOut(@"SELECT        RowID, UsrName, PsWord, UsrType, NameOfUser, SnameOfUser, PhoneNumber, MobilePhones, DateOfRegister
                            FROM  tblUsers  WHERE        (UsrName = @UsrName) AND (PsWord = @PsWord) ORDER BY RowID DESC ");
            if (Dt.Rows.Count <= 0)
            {
                MessageBox.Show("نام کاربری و یا کلمه عبور اشتباه وارد شده اند");
            }
            else
            {
                Txt_Password.Text = "";
                Txt_UserName.Text = "";
                Class_Global_Vars.ThisLogedUserID = Dt.Rows[0][0].ToString();
                Class_Global_Vars.ThisLogedUserName = Dt.Rows[0][1].ToString();
                Class_Global_Vars.ThisLogedUserTypeID = Dt.Rows[0][3].ToString();
                Dt.Clear();
                this.Hide();
                frmMain frm = new frmMain();
                frm.ShowDialog();
            }
        }

        private void frmLogin_Load(object sender, EventArgs e)
        {
            Class_Register_Logs LogsRegIn = new Class_Register_Logs(this.Text + " is Opened.");
        }

        private void frmLogin_FormClosed(object sender, FormClosedEventArgs e)
        {
            Class_Register_Logs LogsRegIn = new Class_Register_Logs(this.Text + " is Closed");
        }

        private void Btn_Cancel_Click(object sender, EventArgs e)
        {
            Class_Register_Logs LogsRegIn = new Class_Register_Logs(this.Text + " - Login Cancelled");
            Application.Exit();
        }

        private void Txt_UserName_KeyDown(object sender, KeyEventArgs e)
        {
            if (Txt_UserName.Text != "" && Txt_Password.Text != "")
            {
                if (int.Parse(e.KeyValue.ToString()) == 13)
                {
                    Btn_Login.PerformClick();
                }
            }
        }

        private void Txt_Password_KeyDown(object sender, KeyEventArgs e)
        {
            if (Txt_UserName.Text != "" && Txt_Password.Text != "")
            {
                if (int.Parse(e.KeyValue.ToString()) == 13)
                {
                    Btn_Login.PerformClick();
                }
            }
        }


    }
}
