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

namespace amlakyar
{
    public partial class frm_update_user : Form
    {
        static SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\amlak.mdf;integrated security=true;User Instance=True");
        SqlDataAdapter da = new SqlDataAdapter("", con);
        DataSet ds = new DataSet();
        string s;
        public frm_update_user()
        {
            InitializeComponent();
        }
        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)]
        public static extern short GetKeyState(int keyCode);

        private void frm_update_user_Load(object sender, EventArgs e)
        {
            da.SelectCommand.CommandText = "select * from login";

            da.Fill(ds, "l");

            dglogin.DataSource = ds;
            dglogin.DataMember = "l";
            dglogin.Columns[0].HeaderText = "نام";
            dglogin.Columns[1].HeaderText = "نام کاربری";
            dglogin.Columns[2].HeaderText = "رمز عبور";
            dglogin.Columns[3].HeaderText = "نوع";
        }

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

        private void txt_pass_Enter(object sender, EventArgs e)
        {
            bool isCapsLock = (((ushort)GetKeyState(0x14 /*VK_CAPITAL*/)) & 0xffff) != 0;
            if (isCapsLock == true)
                MessageBox.Show("کپسلوک را خاموش کنید");
            foreach (InputLanguage m in InputLanguage.InstalledInputLanguages)//farsi kardaneh keybord
                if (m.Culture.Name == "en-US") InputLanguage.CurrentInputLanguage = m;
        }

        private void txt_family_Enter(object sender, EventArgs e)
        {
            foreach (InputLanguage l in InputLanguage.InstalledInputLanguages)//farsi kardaneh keybord
                if (l.Culture.Name == "fa-IR") InputLanguage.CurrentInputLanguage = l;
        }

        private void txt_user_Enter(object sender, EventArgs e)
        {
            foreach (InputLanguage m in InputLanguage.InstalledInputLanguages)//farsi kardaneh keybord
                if (m.Culture.Name == "en-US") InputLanguage.CurrentInputLanguage = m;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
                

            }


        private void dglogin_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            s = Convert.ToString(dglogin.CurrentRow.Cells["id"].Value);
            da.SelectCommand.CommandText = "select * from login where id='" + s + "'";
            da.Fill(ds, "q");
            txt_family.Text = Convert.ToString(dglogin.CurrentRow.Cells["na_user"].Value);
            txt_user.Text = Convert.ToString(dglogin.CurrentRow.Cells["users"].Value);
            txt_pass .Text = Convert.ToString(dglogin.CurrentRow.Cells["id"].Value);
            combo_type.Text = Convert.ToString(dglogin.CurrentRow.Cells["type"].Value);
            txt_user .Enabled = false;
            
 
        }

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

        private void Button1_Click_1(object sender, EventArgs e)
        {
            string d;
            if (MessageBox.Show("آیا می خواهید اطللاعات حذف شود", "اخطار", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                d = "delete from login where na_user='" + txt_user.Text + "'";
                SqlCommand cmd = new SqlCommand(d, con);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
                ds.Clear();
                da.SelectCommand.CommandText = "select * from login";
                da.Fill(ds, "d");
                dglogin.DataSource = ds;
                dglogin.DataMember = "d";
                dglogin.Columns[0].HeaderText = "نام";
                dglogin.Columns[1].HeaderText = "نام کاربری";
                dglogin.Columns[2].HeaderText = "رمز عبور";
                dglogin.Columns[3].HeaderText = "نوع";
                MessageBox.Show("اطلاعات حذف شد");
            }
            txt_family.Text = "";
            txt_pass.Text = "";
            txt_user.Text = "";
            txt_user.Enabled = true;
        }

        private void glassButton1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Button4_Click_1(object sender, EventArgs e)
        {
            string p;

            p = "update  login  set users='" + txt_family.Text + "',na_user='" + txt_user.Text + "',id='" + txt_pass.Text + "', type='" + combo_type.Text + "' where na_user='" + txt_user.Text + "' or id='" + txt_pass.Text + "'";
            SqlCommand cmd = new SqlCommand(p, con);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            ds.Clear();
            da.SelectCommand.CommandText = "select * from login";
            da.Fill(ds, "x");
            dglogin.DataSource = ds;
            dglogin.DataMember = "x";
            txt_family.Text = "";
            txt_pass.Text = "";
            txt_user.Text = "";
            txt_user.Enabled = true;
        }

        private void Button3_Click_1(object sender, EventArgs e)
        {
            string struser, s2;
            if (txt_family.Text != "" && txt_pass.Text != "" && txt_user.Text != "")
            {


                ds.Clear();
                da.SelectCommand.CommandText = "select id as[نام کاربری],users as[رمز عبور],type as[نحوه ی دسترسی] from login where id='" + txt_user.Text + "'";
                da.Fill(ds, "d1");
                if (ds.Tables["d1"].Rows.Count > 0)
                {
                    DialogResult d = MessageBox.Show("کاربری با این نام کاربری قبلا ثبت شده است، لطفا نام کاربری را تغییر دهید ", "اخطار", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txt_user.Focus();
                }

                else
                {

                    string s;
                    ds.Clear();

                    s = "insert into login(users,id,na_user,type) values ('" + txt_user.Text + "','" + txt_pass.Text + "','" + txt_family.Text + "','" + combo_type.Text + "')";

                    SqlCommand cmd = new SqlCommand(s, con);


                    try
                    {
                        ds.Clear();
                        con.Open();
                        cmd.ExecuteNonQuery();
                        con.Close();
                        da.Fill(ds, "t1");


                        dglogin.DataSource = ds;
                        dglogin.DataMember = "t1";
                        con.Close();
                        MessageBox.Show("ثبت شد   " + "  " + txt_user.Text + "  " + "کاربری با نام کاربری", "مدیریت املاک", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    }
                    catch
                    {
                        MessageBox.Show("یکی از داده ها اشتباه وارد شده است");
                        con.Close();
                    }
                }
            }

            else
            {
                MessageBox.Show("اطلاعات را کامل وارد نمایید", "خطا", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }
        }

            
   
            }
            
        }

                

        

        
   
