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 master : DevComponents.DotNetBar.OfficeForm
    {
        public master()
        {
            InitializeComponent();
        }

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

            con.Close();
        }

        public void msearch_name()
        {
            dgvteacher.Rows.Clear();
            SqlConnection con = new SqlConnection("server=(local);database=school;integrated security=true");
            SqlCommand c1 = new SqlCommand();
            con.Open();
            c1.CommandText = "select * from teacher where name = '" + txt_msearch.Text + "'";
            c1.Connection = con;
            SqlDataReader dr = c1.ExecuteReader();
            object[] o1 = new object[13];
            while (dr.Read())
            {
                dr.GetValues(o1);
                dgvteacher.Rows.Add(o1);
            }

            con.Close();
        }

        public void msearch_family()
        {
            dgvteacher.Rows.Clear();
            SqlConnection con = new SqlConnection("server=(local);database=school;integrated security=true");
            SqlCommand c1 = new SqlCommand();
            con.Open();
            c1.CommandText = "select * from teacher where family = '" + txt_msearch.Text + "'";
            c1.Connection = con;
            SqlDataReader dr = c1.ExecuteReader();
            object[] o1 = new object[13];
            while (dr.Read())
            {
                dr.GetValues(o1);
                dgvteacher.Rows.Add(o1);
            }

            con.Close();
        }

        public void msearch_fname()
        {
            dgvteacher.Rows.Clear();
            SqlConnection con = new SqlConnection("server=(local);database=school;integrated security=true");
            SqlCommand c1 = new SqlCommand();
            con.Open();
            c1.CommandText = "select * from teacher where fname = '" + txt_msearch.Text + "'";
            c1.Connection = con;
            SqlDataReader dr = c1.ExecuteReader();
            object[] o1 = new object[13];
            while (dr.Read())
            {
                dr.GetValues(o1);
                dgvteacher.Rows.Add(o1);
            }

            con.Close();
        }

        public void msearch_shenasname()
        {
            dgvteacher.Rows.Clear();
            SqlConnection con = new SqlConnection("server=(local);database=school;integrated security=true");
            SqlCommand c1 = new SqlCommand();
            con.Open();
            c1.CommandText = "select * from teacher where shenasname = '" + txt_msearch.Text + "'";
            c1.Connection = con;
            SqlDataReader dr = c1.ExecuteReader();
            object[] o1 = new object[13];
            while (dr.Read())
            {
                dr.GetValues(o1);
                dgvteacher.Rows.Add(o1);
            }

            con.Close();
        }

        public void msearch_cmeli()
        {
            dgvteacher.Rows.Clear();
            SqlConnection con = new SqlConnection("server=(local);database=school;integrated security=true");
            SqlCommand c1 = new SqlCommand();
            con.Open();
            c1.CommandText = "select * from teacher where cmeli = '" + txt_msearch.Text + "'";
            c1.Connection = con;
            SqlDataReader dr = c1.ExecuteReader();
            object[] o1 = new object[13];
            while (dr.Read())
            {
                dr.GetValues(o1);
                dgvteacher.Rows.Add(o1);
            }

            con.Close();
        }

        public void msearch_sex()
        {
            dgvteacher.Rows.Clear();
            SqlConnection con = new SqlConnection("server=(local);database=school;integrated security=true");
            SqlCommand c1 = new SqlCommand();
            con.Open();
            c1.CommandText = "select * from teacher where sex = '" + txt_msearch.Text + "'";
            c1.Connection = con;
            SqlDataReader dr = c1.ExecuteReader();
            object[] o1 = new object[13];
            while (dr.Read())
            {
                dr.GetValues(o1);
                dgvteacher.Rows.Add(o1);
            }

            con.Close();
        }

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

            con.Close();
        }

        public void msearch_swork()
        {
            dgvteacher.Rows.Clear();
            SqlConnection con = new SqlConnection("server=(local);database=school;integrated security=true");
            SqlCommand c1 = new SqlCommand();
            con.Open();
            c1.CommandText = "select * from teacher where swork = '" + txt_msearch.Text + "'";
            c1.Connection = con;
            SqlDataReader dr = c1.ExecuteReader();
            object[] o1 = new object[13];
            while (dr.Read())
            {
                dr.GetValues(o1);
                dgvteacher.Rows.Add(o1);
            }

            con.Close();
        }


        public void msearch_dore()
        {
            dgvteacher.Rows.Clear();
            SqlConnection con = new SqlConnection("server=(local);database=school;integrated security=true");
            SqlCommand c1 = new SqlCommand();
            con.Open();
            c1.CommandText = "select * from teacher where dore = '" + txt_msearch.Text + "'";
            c1.Connection = con;
            SqlDataReader dr = c1.ExecuteReader();
            object[] o1 = new object[13];
            while (dr.Read())
            {
                dr.GetValues(o1);
                dgvteacher.Rows.Add(o1);
            }

            con.Close();
        }

        public void msearch_elevel()
        {
            dgvteacher.Rows.Clear();
            SqlConnection con = new SqlConnection("server=(local);database=school;integrated security=true");
            SqlCommand c1 = new SqlCommand();
            con.Open();
            c1.CommandText = "select * from teacher where elevel = '" + txt_msearch.Text + "'";
            c1.Connection = con;
            SqlDataReader dr = c1.ExecuteReader();
            object[] o1 = new object[13];
            while (dr.Read())
            {
                dr.GetValues(o1);
                dgvteacher.Rows.Add(o1);
            }

            con.Close();
        }

        public void msearch_tel()
        {
            dgvteacher.Rows.Clear();
            SqlConnection con = new SqlConnection("server=(local);database=school;integrated security=true");
            SqlCommand c1 = new SqlCommand();
            con.Open();
            c1.CommandText = "select * from teacher where tel = '" + txt_msearch.Text + "'";
            c1.Connection = con;
            SqlDataReader dr = c1.ExecuteReader();
            object[] o1 = new object[13];
            while (dr.Read())
            {
                dr.GetValues(o1);
                dgvteacher.Rows.Add(o1);
            }

            con.Close();
        }

        public void msearch_mob()
        {
            dgvteacher.Rows.Clear();
            SqlConnection con = new SqlConnection("server=(local);database=school;integrated security=true");
            SqlCommand c1 = new SqlCommand();
            con.Open();
            c1.CommandText = "select * from teacher where mob = '" + txt_msearch.Text + "'";
            c1.Connection = con;
            SqlDataReader dr = c1.ExecuteReader();
            object[] o1 = new object[13];
            while (dr.Read())
            {
                dr.GetValues(o1);
                dgvteacher.Rows.Add(o1);
            }

            con.Close();
        }


        private void mshow()
        {
            dgvteacher.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 teacher";
            c1.Connection = con;
            SqlDataReader dr = c1.ExecuteReader();
            object[] o1 = new object[13];
            while (dr.Read())
            {
                dr.GetValues(o1);
                dgvteacher.Rows.Add(o1);
            }
            con.Close();
        }

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

        }

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



            con.Close();
        }

        private void medit()
        {
            SqlConnection con = new SqlConnection("server=(local);database=school;integrated security=true");
            con.Open();
            string s1 = "update teacher 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_sex.Text + "',type='" + cmb_type.Text + "',swork='" + txt_swork.Text + "',dore='" + txt_dore.Text + "',elevel='" + txt_elevel.Text + "',tel='" + txt_tel.Text + "',mob='" + txt_mob.Text + "' where id='" + txt_code.Text + "'";
            SqlCommand c3 = new SqlCommand(s1, con);
            if (MessageBox.Show("آیا می خواهید این را ویرایش کنید؟", "اخطار ویرایش", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                try
                {
                    c3.ExecuteNonQuery();
                    MessageBox.Show("ویرایش شد");
                    mshow();
                }

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

            con.Close();

        }

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

            con.Close();

        }

        private void btn_madd_Click(object sender, EventArgs e)
        {
            madd();
            mshow();
            mclear();
        }

        private void btn_medit_Click(object sender, EventArgs e)
        {
            medit();
            mshow();
            mclear();
        }

        private void btn_mdelete_Click(object sender, EventArgs e)
        {
            mdelete();
            mshow();
            mclear();
        }

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

        private void btn_msearch_Click(object sender, EventArgs e)
        {
            if (cmb_msearch.Text == "کد")
            {
                msearch_code();
            }
            if (cmb_msearch.Text == "نام")
            {
                msearch_name();
            }
            if (cmb_msearch.Text == "نام خانوادگی")
            {
                msearch_family();
            }
            if (cmb_msearch.Text == "نام پدر")
            {
                msearch_fname();
            }
            if (cmb_msearch.Text == "شماره شناسنامه")
            {
                msearch_shenasname();
            }
            if (cmb_msearch.Text == "کد ملی")
            {
                msearch_cmeli();
            }
            if (cmb_msearch.Text == "جنسیت")
            {
                msearch_sex();
            }
            if (cmb_msearch.Text == "نوع")
            {
                msearch_type();
            }
            if (cmb_msearch.Text == "سابقه کار")
            {
                msearch_swork();
            }
            if (cmb_msearch.Text == "نام دوره")
            {
                msearch_dore();
            }
            if (cmb_msearch.Text == "مدرک تحصیلی")
            {
                msearch_elevel();
            }
            if (cmb_msearch.Text == "تلفن")
            {
                msearch_tel();
            }
            if (cmb_msearch.Text == "موبایل")
            {
                msearch_mob();
            }
        }

        private void btn_mall_Click(object sender, EventArgs e)
        {
            mshow();
            txt_msearch.Clear();
        }

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

        private void master_Load(object sender, EventArgs e)
        {
            mshow();
        }





    }
}
