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 DevComponents.DotNetBar;

namespace academy
{
    public partial class karmand : DevComponents.DotNetBar.OfficeForm
    {
        public karmand()
        {
            InitializeComponent();
        }

        public void ksearch_code()
        {
            dgvkarmand.Rows.Clear();
            SqlConnection con = new SqlConnection("server=(local);database=school;integrated security=true");
            SqlCommand c4 = new SqlCommand();
            con.Open();
            c4.CommandText = "select * from karmand where id = '" + txt_kserach.Text + "'";
            c4.Connection = con;
            SqlDataReader dr = c4.ExecuteReader();
            object[] o1 = new object[12];
            while (dr.Read())
            {
                dr.GetValues(o1);
                dgvkarmand.Rows.Add(o1);
            }

            con.Close();
        }

        public void ksearch_name()
        {
            dgvkarmand.Rows.Clear();
            SqlConnection con = new SqlConnection("server=(local);database=school;integrated security=true");
            SqlCommand c4 = new SqlCommand();
            con.Open();
            c4.CommandText = "select * from karmand where name = '" + txt_kserach.Text + "'";
            c4.Connection = con;
            SqlDataReader dr = c4.ExecuteReader();
            object[] o1 = new object[12];
            while (dr.Read())
            {
                dr.GetValues(o1);
                dgvkarmand.Rows.Add(o1);
            }

            con.Close();
        }

        public void ksearch_family()
        {
            dgvkarmand.Rows.Clear();
            SqlConnection con = new SqlConnection("server=(local);database=school;integrated security=true");
            SqlCommand c4 = new SqlCommand();
            con.Open();
            c4.CommandText = "select * from karmand where family = '" + txt_kserach.Text + "'";
            c4.Connection = con;
            SqlDataReader dr = c4.ExecuteReader();
            object[] o1 = new object[12];
            while (dr.Read())
            {
                dr.GetValues(o1);
                dgvkarmand.Rows.Add(o1);
            }

            con.Close();
        }

        public void ksearch_fname()
        {
            dgvkarmand.Rows.Clear();
            SqlConnection con = new SqlConnection("server=(local);database=school;integrated security=true");
            SqlCommand c4 = new SqlCommand();
            con.Open();
            c4.CommandText = "select * from karmand where fname = '" + txt_kserach.Text + "'";
            c4.Connection = con;
            SqlDataReader dr = c4.ExecuteReader();
            object[] o1 = new object[12];
            while (dr.Read())
            {
                dr.GetValues(o1);
                dgvkarmand.Rows.Add(o1);
            }

            con.Close();
        }

        public void ksearch_shenasname()
        {
            dgvkarmand.Rows.Clear();
            SqlConnection con = new SqlConnection("server=(local);database=school;integrated security=true");
            SqlCommand c4 = new SqlCommand();
            con.Open();
            c4.CommandText = "select * from karmand where shenasname = '" + txt_kserach.Text + "'";
            c4.Connection = con;
            SqlDataReader dr = c4.ExecuteReader();
            object[] o1 = new object[12];
            while (dr.Read())
            {
                dr.GetValues(o1);
                dgvkarmand.Rows.Add(o1);
            }

            con.Close();
        }

        public void ksearch_cmeli()
        {
            dgvkarmand.Rows.Clear();
            SqlConnection con = new SqlConnection("server=(local);database=school;integrated security=true");
            SqlCommand c4 = new SqlCommand();
            con.Open();
            c4.CommandText = "select * from karmand where cmeli = '" + txt_kserach.Text + "'";
            c4.Connection = con;
            SqlDataReader dr = c4.ExecuteReader();
            object[] o1 = new object[12];
            while (dr.Read())
            {
                dr.GetValues(o1);
                dgvkarmand.Rows.Add(o1);
            }

            con.Close();
        }

        public void ksearch_sex()
        {
            dgvkarmand.Rows.Clear();
            SqlConnection con = new SqlConnection("server=(local);database=school;integrated security=true");
            SqlCommand c4 = new SqlCommand();
            con.Open();
            c4.CommandText = "select * from karmand where sex = '" + txt_kserach.Text + "'";
            c4.Connection = con;
            SqlDataReader dr = c4.ExecuteReader();
            object[] o1 = new object[12];
            while (dr.Read())
            {
                dr.GetValues(o1);
                dgvkarmand.Rows.Add(o1);
            }

            con.Close();
        }


        
        public void ksearch_swork()
        {
            dgvkarmand.Rows.Clear();
            SqlConnection con = new SqlConnection("server=(local);database=school;integrated security=true");
            SqlCommand c4 = new SqlCommand();
            con.Open();
            c4.CommandText = "select * from karmand where swork = '" + txt_kserach.Text + "'";
            c4.Connection = con;
            SqlDataReader dr = c4.ExecuteReader();
            object[] o1 = new object[12];
            while (dr.Read())
            {
                dr.GetValues(o1);
                dgvkarmand.Rows.Add(o1);
            }

            con.Close();
        }


        public void ksearch_jobname()
        {
            dgvkarmand.Rows.Clear();
            SqlConnection con = new SqlConnection("server=(local);database=school;integrated security=true");
            SqlCommand c4 = new SqlCommand();
            con.Open();
            c4.CommandText = "select * from karmand where jobname = '" + txt_kserach.Text + "'";
            c4.Connection = con;
            SqlDataReader dr = c4.ExecuteReader();
            object[] o1 = new object[12];
            while (dr.Read())
            {
                dr.GetValues(o1);
                dgvkarmand.Rows.Add(o1);
            }

            con.Close();
        }

        public void ksearch_elevel()
        {
            dgvkarmand.Rows.Clear();
            SqlConnection con = new SqlConnection("server=(local);database=school;integrated security=true");
            SqlCommand c4 = new SqlCommand();
            con.Open();
            c4.CommandText = "select * from karmand where elevel = '" + txt_kserach.Text + "'";
            c4.Connection = con;
            SqlDataReader dr = c4.ExecuteReader();
            object[] o1 = new object[12];
            while (dr.Read())
            {
                dr.GetValues(o1);
                dgvkarmand.Rows.Add(o1);
            }

            con.Close();
        }

        public void ksearch_tel()
        {
            dgvkarmand.Rows.Clear();
            SqlConnection con = new SqlConnection("server=(local);database=school;integrated security=true");
            SqlCommand c4 = new SqlCommand();
            con.Open();
            c4.CommandText = "select * from karmand where tel = '" + txt_kserach.Text + "'";
            c4.Connection = con;
            SqlDataReader dr = c4.ExecuteReader();
            object[] o1 = new object[12];
            while (dr.Read())
            {
                dr.GetValues(o1);
                dgvkarmand.Rows.Add(o1);
            }

            con.Close();
        }

        public void ksearch_mob()
        {
            dgvkarmand.Rows.Clear();
            SqlConnection con = new SqlConnection("server=(local);database=school;integrated security=true");
            SqlCommand c4 = new SqlCommand();
            con.Open();
            c4.CommandText = "select * from karmand where mob = '" + txt_kserach.Text + "'";
            c4.Connection = con;
            SqlDataReader dr = c4.ExecuteReader();
            object[] o1 = new object[12];
            while (dr.Read())
            {
                dr.GetValues(o1);
                dgvkarmand.Rows.Add(o1);
            }

            con.Close();
        }

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

        private void kclear()
        {
            txt_code.Clear();
            txt_name.Clear();
            txt_family.Clear();
            txt_fname.Clear();
            txt_shenasname.Clear();
            txt_cmeli.Clear();
            txt_swork.Clear();
            txt_jobname.Clear();
            txt_elevel.Clear();
            txt_tel.Clear();
            txt_mob.Clear();

        }

        private void kadd()
        {
            SqlConnection con = new SqlConnection("server=(local);database=school;integrated security=true");
            con.Open();
            string s1 = "insert into karmand(id,name,family,fname,shenasname,cmeli,sex,swork,jobname,elevel,tel,mob)values('"+txt_code.Text+"','"+txt_name.Text+"','"+txt_family.Text+"','"+txt_fname.Text+"','"+txt_shenasname.Text+"','"+txt_cmeli.Text+"','"+cmb_ksex.Text+"','"+txt_swork.Text+"','"+txt_jobname.Text+"','"+txt_elevel.Text+"','"+txt_tel.Text+"','"+txt_mob.Text+"')";
            SqlCommand c5 = new SqlCommand(s1, con);
            if (MessageBox.Show("آیا می خواهید این را ثبت کنید؟", "اخطار ثبت", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                c5.ExecuteNonQuery();
                MessageBox.Show("ثبت شد");
                kshow();
            }



            con.Close();
        }

        private void kedit()
        {
            SqlConnection con = new SqlConnection("server=(local);database=school;integrated security=true");
            con.Open();
            string s1 = "update karmand set id ='" + txt_code.Text + "',name='" + txt_name.Text + "',family='" + txt_family.Text + "',fname='" + txt_fname.Text + "',shenasname='" + txt_shenasname.Text + "',cmeli='" + txt_cmeli.Text + "',sex='" + cmb_ksex.Text + "',swork='" + txt_swork.Text + "',jobname='"+txt_jobname.Text+"',elevel='" + txt_elevel.Text + "',tel='" + txt_tel.Text + "',mob='" + txt_mob.Text + "' where id='" + txt_code.Text + "'";
            SqlCommand c5 = new SqlCommand(s1, con);
            if (MessageBox.Show("آیا می خواهید این را ویرایش کنید؟", "اخطار ویرایش", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                try
                {
                    c5.ExecuteNonQuery();
                    MessageBox.Show("ویرایش شد");
                    kshow();
                }

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

            con.Close();

        }

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

            con.Close();

        }

        private void bn_add_Click(object sender, EventArgs e)
        {
            kadd();
            kshow();
            kclear();
        }

        private void bn_edit_Click(object sender, EventArgs e)
        {
            kedit();
            kshow();
            kclear();
        }

        private void bn_delete_Click(object sender, EventArgs e)
        {
            kdelete();
            kshow();
            kclear();
        }

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

        private void bn_all_Click(object sender, EventArgs e)
        {
            kshow();
            txt_kserach.Clear();
        }

        private void bn_searchs_Click(object sender, EventArgs e)
        {
            if (cmbksearch.Text == "کد")
            {
                ksearch_code();
            }
            if (cmbksearch.Text == "نام")
            {
                ksearch_name();
            }
            if (cmbksearch.Text == "نام خانوادگی")
            {
                ksearch_family();
            }
            if (cmbksearch.Text == "نام پدر")
            {
                ksearch_fname();
            }
            if (cmbksearch.Text == "شماره شناسنامه")
            {
                ksearch_shenasname();
            }
            if (cmbksearch.Text == "کد ملی")
            {
                ksearch_cmeli();
            }
            if (cmbksearch.Text == "جنسیت")
            {
                ksearch_sex();
            }

            if (cmbksearch.Text == "سابقه کار")
            {
                ksearch_swork();
            }

            if (cmbksearch.Text == "سمت")
            {
                ksearch_jobname();
            }

            if (cmbksearch.Text == "مدرک تحصیلی")
            {
                ksearch_elevel();
            }
            if (cmbksearch.Text == "تلفن")
            {
                ksearch_tel();
            }
            if (cmbksearch.Text == "موبایل")
            {
                ksearch_mob();
            }
        }

        private void dgvkarmand_MouseClick(object sender, MouseEventArgs e)
        {
            txt_code.Text = dgvkarmand.CurrentRow.Cells[0].Value.ToString();
            txt_name.Text = dgvkarmand.CurrentRow.Cells[1].Value.ToString();
            txt_family.Text = dgvkarmand.CurrentRow.Cells[2].Value.ToString();
            txt_fname.Text = dgvkarmand.CurrentRow.Cells[3].Value.ToString();
            txt_shenasname.Text = dgvkarmand.CurrentRow.Cells[4].Value.ToString();
            txt_cmeli.Text = dgvkarmand.CurrentRow.Cells[5].Value.ToString();
            cmb_ksex.Text = dgvkarmand.CurrentRow.Cells[6].Value.ToString();
            txt_swork.Text = dgvkarmand.CurrentRow.Cells[7].Value.ToString();
            txt_jobname.Text = dgvkarmand.CurrentRow.Cells[8].Value.ToString();
            txt_elevel.Text = dgvkarmand.CurrentRow.Cells[9].Value.ToString();
            txt_tel.Text = dgvkarmand.CurrentRow.Cells[10].Value.ToString();
            txt_mob.Text = dgvkarmand.CurrentRow.Cells[11].Value.ToString(); 
        }

        private void karmand_Load(object sender, EventArgs e)
        {
            kshow();
        }



    }
}
