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 DevComponents.DotNetBar;
using System.Data.SqlClient;
namespace academy
{
    public partial class user : DevComponents.DotNetBar.OfficeForm
    {
        public user()
        {
            InitializeComponent();
        }

        public void search_code()
        {
            dgvuser.Rows.Clear();
            SqlConnection con = new SqlConnection("server=(local);database=school;integrated security=true");
            SqlCommand c1 = new SqlCommand();
            con.Open();
            c1.CommandText = "select * from login where id = '" + txt_serach.Text + "'";
            c1.Connection = con;
            SqlDataReader dr = c1.ExecuteReader();
            object[] o1 = new object[4];
            while (dr.Read())
            {
                dr.GetValues(o1);
                dgvuser.Rows.Add(o1);
            }

            con.Close();
        }

        public void search_username()
        {
            dgvuser.Rows.Clear();
            SqlConnection con = new SqlConnection("server=(local);database=school;integrated security=true");
            SqlCommand c1 = new SqlCommand();
            con.Open();
            c1.CommandText = "select * from login where username = '" + txt_serach.Text + "'";
            c1.Connection = con;
            SqlDataReader dr = c1.ExecuteReader();
            object[] o1 = new object[4];
            while (dr.Read())
            {
                dr.GetValues(o1);
                dgvuser.Rows.Add(o1);
            }

            con.Close();
        }

        public void search_password()
        {
            dgvuser.Rows.Clear();
            SqlConnection con = new SqlConnection("server=(local);database=school;integrated security=true");
            SqlCommand c1 = new SqlCommand();
            con.Open();
            c1.CommandText = "select * from login where password = '" + txt_serach.Text + "'";
            c1.Connection = con;
            SqlDataReader dr = c1.ExecuteReader();
            object[] o1 = new object[4];
            while (dr.Read())
            {
                dr.GetValues(o1);
                dgvuser.Rows.Add(o1);
            }

            con.Close();
        }

        public void search_type()
        {
            dgvuser.Rows.Clear();
            SqlConnection con = new SqlConnection("server=(local);database=school;integrated security=true");
            SqlCommand c1 = new SqlCommand();
            con.Open();
            c1.CommandText = "select * from login where type = '" + txt_serach.Text + "'";
            c1.Connection = con;
            SqlDataReader dr = c1.ExecuteReader();
            object[] o1 = new object[4];
            while (dr.Read())
            {
                dr.GetValues(o1);
                dgvuser.Rows.Add(o1);
            }

            con.Close();
        }

        private void show()
        {
            dgvuser.Rows.Clear();
            SqlConnection con = new SqlConnection();
            con.ConnectionString = "server=(local);database=school;integrated security=true";
            SqlCommand c1 = new SqlCommand();
            con.Open();
            c1.CommandText = "select * from login";
            c1.Connection = con;
            SqlDataReader dr = c1.ExecuteReader();
            object[] o1 = new object[4];
            while (dr.Read())
            {
                dr.GetValues(o1);
                dgvuser.Rows.Add(o1);
            }
            con.Close();
        }

        private void clear()
        {
            txt_code.Clear();
            txt_username.Clear();
            txt_password.Clear();
            cmb_type.Text = "";
           

        }

        private void add()
        {
            SqlConnection con = new SqlConnection("server=(local);database=school;integrated security=true");
            con.Open();
            string s1 = "insert into login(id,username,password,type)values('" + txt_code.Text + "','" + txt_username.Text + "','" + txt_password.Text + "','" + cmb_type.Text + "')";
            SqlCommand c2 = new SqlCommand(s1, con);
            if (MessageBox.Show("آیا می خواهید این را ثبت کنید؟", "اخطار ثبت", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                c2.ExecuteNonQuery();
                MessageBox.Show("ثبت شد");
                show();
            }



            con.Close();
        }

        private void edit()
        {
            SqlConnection con = new SqlConnection("server=(local);database=school;integrated security=true");
            con.Open();
            string s1 = "update login set id ='" + txt_code.Text + "',username='" + txt_username.Text + "',password='" + txt_password.Text + "',type='" + cmb_type.Text + "' where id='" + txt_code.Text + "'";
            SqlCommand c2 = new SqlCommand(s1, con);
            if (MessageBox.Show("آیا می خواهید این را ویرایش کنید؟", "اخطار ویرایش", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                try
                {
                    c2.ExecuteNonQuery();
                    MessageBox.Show("ویرایش شد");
                    show();
                }

                catch
                {
                    MessageBox.Show("خطا در ویرایش");
                }
            }

            con.Close();

        }

        private void delete()
        {
            SqlConnection con = new SqlConnection("server=(local);database=school;integrated security=true");
            con.Open();
            string s1 = "delete from login where id='" + txt_code.Text + "'";
            SqlCommand c2 = new SqlCommand(s1, con);
            if (MessageBox.Show("آیا می خواهید این را پاک کنید؟", "اخطار حذف", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                c2.ExecuteNonQuery();
                MessageBox.Show("حذف شد");
                show();
            }

            con.Close();

        }

        private void btn_add_Click(object sender, EventArgs e)
        {
            add();
            show();
            clear();
        }

        private void btn_edit_Click(object sender, EventArgs e)
        {
            edit();
            show();
            clear();
        }

        private void btn_delete_Click(object sender, EventArgs e)
        {
            delete();
            show();
            clear();
        }

        private void btn_all_Click(object sender, EventArgs e)
        {
            show();
            txt_serach.Clear();
        }

        private void btn_back_Click(object sender, EventArgs e)
        {
            main m = new main();
            m.Show();
            this.Hide();
        }

        private void btn_searchs_Click(object sender, EventArgs e)
        {
            if (cmbsearch.Text == "کد")
            {
                search_code();
            }
            if (cmbsearch.Text == "نام کاربری")
            {
                search_username();
            }
            if (cmbsearch.Text == "رمز عبور")
            {
                search_password();
            }
            if (cmbsearch.Text == "سطح دسترسی")
            {
                search_type();
            }
        }

        private void user_Load(object sender, EventArgs e)
        {
            show();
        }

        private void dgvuser_MouseClick(object sender, MouseEventArgs e)
        {
            txt_code.Text = dgvuser.CurrentRow.Cells[0].Value.ToString();
            txt_username.Text = dgvuser.CurrentRow.Cells[1].Value.ToString();
            txt_password.Text = dgvuser.CurrentRow.Cells[2].Value.ToString();
            cmb_type.Text = dgvuser.CurrentRow.Cells[3].Value.ToString();
        }
    }
}
