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 Morakhassi
{
    public partial class frmLogin : Form
    {
        public frmLogin()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Class.Class_Register_Logs _log = new Morakhassi.Class.Class_Register_Logs(this.Text + " Form Is Closed.");
            Application.Exit();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            lscArzeshGozariDataContext db = new lscArzeshGozariDataContext();
            var Query = from users in db.tblUsers
                        where users.UserName == textUserName.Text.Trim() && users.UserPassword == textPassWord.Text.Trim()
                        select new
                        {
                            UserName = users.UserName,
                            PassWord = users.UserPassword
                        };
            if (Query.Count() <= 0)
            {
                MessageBox.Show("نام كاربري يا كلمه عبور اشتباه وارد شده است");
            }
            else
            {
                Class.Class_Global_Var.ThisLogedUserName = textUserName.Text.Trim();
                Class.Class_Register_Logs _log = new Morakhassi.Class.Class_Register_Logs("User Loged In With " + textUserName.Text.Trim());
                textUserName.Text = "";
                textPassWord.Text = "";
                this.Hide();
                frmMain frm = new frmMain();
                frm.ShowDialog();
            }

        }

        private void frmLogin_Load(object sender, EventArgs e)
        {
            Class.Class_Register_Logs _log = new Morakhassi.Class.Class_Register_Logs(this.Text + " Form Is Loaded.");
        }

        private void frmLogin_FormClosed(object sender, FormClosedEventArgs e)
        {
            Class.Class_Register_Logs _log = new Morakhassi.Class.Class_Register_Logs(this.Text + " Form Is Closed. (App.Closed)");
        }

        private void textUserName_KeyDown(object sender, KeyEventArgs e)
        {
            if (textUserName.Text != "" && textPassWord.Text != "")
            {
                if (int.Parse(e.KeyValue.ToString()) == 13)
                {
                    button1_Click(this, new EventArgs());
                }
            }
        }

        private void textPassWord_KeyDown(object sender, KeyEventArgs e)
        {
            if (textUserName.Text != "" && textPassWord.Text != "")
            {
                if (int.Parse(e.KeyValue.ToString()) == 13)
                {
                    button1_Click(this, new EventArgs());
                }
            }
        }
    }
}
