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;
using System.Data.SqlClient;
using System.Threading;
using System.Collections;
namespace DaroKhaneh
{
    
    public partial class login : Form
    {
        public string pass="";
        public static string fullName="";
        string systemname;
        string date;
        string time;
        public static bool voice = false;
        public static bool cadddaro = false;
        public static bool cnezarat = false;
        public static bool chart = false;
        public static bool cadduser = false;
        public static bool caddpezeshk = false;
        public static bool caddeshterak = false;
        public static string uname = "";
        public static byte rating = 0;
        public login()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            
        }

        private void login_Load(object sender, EventArgs e)
        {
            systemname=(Environment.MachineName.ToString());
            date = DateTime.Now.ToLongDateString();
            time = DateTime.Now.ToLongTimeString();
        }
        void setlogin(string vaz,string pass)
        {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = "server=(local);database=darokhane;trusted_connection=yes";

            SqlCommand com = new SqlCommand();
            com.Connection = con;
            com.CommandText = "insert into logon (systemname,date,time,vaziat,uname,upass) values (@systemname,@date,@time,@vaziat,@uname,@upass)";
            com.Parameters.AddWithValue("@systemname", systemname);
            com.Parameters.AddWithValue("@date", date);
            com.Parameters.AddWithValue("@time", time);
            com.Parameters.AddWithValue("@vaziat", vaz);
            com.Parameters.AddWithValue("@uname", txtuser.Text);
            com.Parameters.AddWithValue("@upass", pass);
            con.Open();
            com.ExecuteNonQuery();
            con.Close();
        }
        void set()
        {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = "server=(local);database=darokhane;trusted_connection=yes";

            SqlCommand com = new SqlCommand();
            com.Connection = con;
            string sql = "select * from accont where uname='{0}' and upass='{1}'";
            sql = string.Format(sql, txtuser.Text, pass);
            com.CommandText = sql;
            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = com;

            DataTable dt = new DataTable();
            da.Fill(dt);
            cadddaro = Convert.ToBoolean(dt.Rows[0]["cadddaro"].ToString());
            chart = Convert.ToBoolean(dt.Rows[0]["chart"].ToString());
            caddpezeshk = Convert.ToBoolean(dt.Rows[0]["caddpezeshk"].ToString());
            cadduser = Convert.ToBoolean(dt.Rows[0]["cadduser"].ToString());
            cnezarat = Convert.ToBoolean(dt.Rows[0]["cnezarat"].ToString());
            caddeshterak = Convert.ToBoolean(dt.Rows[0]["caddeshterak"].ToString());
            fullName = dt.Rows[0]["NameFamily"].ToString();
            con.Close();
        }

        private void txtpass_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != '\b')
            {
                pass += e.KeyChar.ToString();
                e.Handled = true;
                Bitmap bm = new Bitmap(Properties.Resources.Fatcow_Farm_Fresh_Pill);
                Clipboard.SetDataObject(bm);
                txtpass.Paste();
            }
            else
                pass = pass.Remove(pass.Length - 1);
        }

        private void btnlogin_Click(object sender, EventArgs e)
        {
            if (checkshowpass.Checked == true) pass = txtpass.Text;
            SqlConnection con = new SqlConnection();
            con.ConnectionString = "server=(local);database=darokhane;trusted_connection=yes";

            SqlCommand com = new SqlCommand();
            com.Connection = con;
            string sql = "select * from accont where uname='{0}' and upass='{1}'";
            sql = string.Format(sql, txtuser.Text.Trim(), pass.Replace("'", "\""));
            com.CommandText = sql;
            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = com;

            DataTable dt = new DataTable();
            da.Fill(dt);
            try
            {
                if (dt.Rows[0][0].ToString() == txtuser.Text && dt.Rows[0][1].ToString() == pass)
                {
                    if (swVoice.Value == true) voice = true;
                    else voice = false;
                    rating = byte.Parse(dt.Rows[0][3].ToString());
                    uname = txtuser.Text.ToString();
                    set();
                    setlogin("موفقیت", "******");
                    con.Close();
                    Form1 frm1 = new Form1();
                    frm1.Show();
                    this.Hide();
                }
            }
            catch
            {
                MessageBox.Show("نام کاربری یا کلمه عبور اشتباه است", "بروز خطا", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                setlogin("نا موفق", pass);
            }
        }

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

        private void txtuser_MouseMove(object sender, MouseEventArgs e)
        {
            txtuser.BackColor = Color.Yellow;
        }

        private void txtuser_MouseLeave(object sender, EventArgs e)
        {
            txtuser.BackColor = Color.White;
        }

        private void txtuser_Leave(object sender, EventArgs e)
        {
            txtuser.BackColor = Color.White;
        }

        private void txtpass_Leave(object sender, EventArgs e)
        {
            txtpass.BackColor = Color.White;
        }

        private void txtpass_MouseLeave(object sender, EventArgs e)
        {
            txtpass.BackColor = Color.White;
        }

        private void txtpass_MouseMove(object sender, MouseEventArgs e)
        {
            txtpass.BackColor = Color.Yellow;
        }

        private void txtuser_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter){txtpass.Focus();}
        }

        private void txtpass_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnlogin.Focus();
                btnlogin.Text = "  ورود";
            }
        }

        private void btnlogin_TextChanged(object sender, EventArgs e)
        {
            btnlogin_Click( sender, e);
        }

        private void txtpass_Click(object sender, EventArgs e)
        {
            txtpass.Clear();
            pass = "";
        }
    }
}
