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

namespace Reservtion
{
    public partial class Log_in : Form
    {
        public Log_in()
        {
            InitializeComponent();
        }

        RConection r = new RConection();
        private DataTable dt = new DataTable();
        private DataTable dt2 = new DataTable();
        int x = 4;
        Boolean Ls = true;
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                r.cmd.CommandText = "Execute PLog_in @UserName=N'" + textBox1.Text + "',@Password=N'" + textBox2.Text + "',@AccessType=N'رزروشن'";
                r.da.Fill(dt);
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message);
            }

            if (dt.Rows.Count == 1)
            {
                try
                {
                    r.cmd.CommandText = "select Name,Family from Employee where Code=" + dt.Rows[0]["EmployeeId"].ToString();
                    r.da.Fill(dt2);
                }
                catch (SqlException ex)
                {
                    MessageBox.Show(ex.Message);
                }

                Program.PUser = new PresentUser(dt2.Rows[0]["Name"].ToString(), dt2.Rows[0]["Family"].ToString(), textBox1.Text);
                textBox1.Text = "";
                textBox2.Text = "";
                textBox1.Focus();
                dt.Clear();
                dt2.Clear();
                this.Hide();
                new Reservtion(r).Show();
                this.Hide();
            }
            else
            {
                if (x == 0)
                    this.Dispose();
                MessageBox.Show("!!!نام کاربری یا رمز عبور صحیح نمی باشد", "LoginError", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                textBox2.Text = "";
                x--;
            }
        }

        private void Log_in_Shown(object sender, EventArgs e)
        {
            x = 4;
            textBox1.Focus();
        }

        private void textBox1_Click(object sender, EventArgs e)
        {
            if (Ls && textBox1.Text.Length != 0)
            {
                textBox1.SelectAll();
                Ls = false;
            }
        }

        private void textBox1_Enter(object sender, EventArgs e)
        {
            textBox1.SelectAll();
        }

        private void Log_in_Load(object sender, EventArgs e)
        {

        }

        private void Btn_Cancel_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}
