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

        SqlConnection objconnection = new SqlConnection("server=(local);database=school;integrated security=true");
        SqlCommand objcommand = new SqlCommand();
        DataSet ds = new DataSet();
        //-------------------------/

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

            con.Close();
        }

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

            con.Close();
        }

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

            con.Close();
        }

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

            con.Close();
        }

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

            con.Close();
        }

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

            con.Close();
        }

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

            con.Close();
        }

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

            con.Close();
        }

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

            con.Close();
        }

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

            con.Close();
        }


        private void clear()
        {
            txt_code.Clear();
            txt_classname.Clear();
            txt_classid.Clear();
            txt_classstartdate.Clear();
            txt_classenddate.Clear();
            txt_classstarttime.Clear();
            txt_teachername.Clear();
        }

        private void add()
        {
            SqlConnection con = new SqlConnection("server=(local);database=school;integrated security=true");
            con.Open();
            string s1 = "insert into class(id,classname,classtype,classid,namedars,mehvariyat,classstartdate,classenddate,classstarttime,teachername)values('" + txt_code.Text + "','" + txt_classname.Text + "','" + cmb_typekelas.Text + "','" + txt_classid.Text + "','" + cmb_namedars.Text + "','" + cmb_mehvariyat.Text + "','" + txt_classstartdate.Text + "','" + txt_classenddate.Text + "','" + txt_classstarttime.Text + "','" + txt_teachername.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 class set id ='" + txt_code.Text + "',classname='" + txt_classname.Text + "',classtype='" + cmb_typekelas.Text + "',classid='" + txt_classid.Text + "',namedars='" + cmb_namedars.Text + "',mehvariyat='" + cmb_mehvariyat.Text + "',classstartdate='" + txt_classstartdate.Text + "',classenddate='" + txt_classenddate.Text + "',classstarttime='" + txt_classstarttime.Text + "',teachername='" + txt_teachername.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 class 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 show()
        {
            dgvkelas.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 class";
            c1.Connection = con;
            SqlDataReader dr = c1.ExecuteReader();
            object[] o1 = new object[10];
            while (dr.Read())
            {
                dr.GetValues(o1);
                dgvkelas.Rows.Add(o1);
            }
            con.Close();
        }


        private void kelas_Load(object sender, EventArgs e)
        {
            show();
            DataTable dt1 = new DataTable();
            DataBase db1 = new DataBase();
            dt1 = db1.MySelect("select * from dars");
            cmb_namedars.DataSource = dt1;
            cmb_namedars.DisplayMember= "name";
            Font f = new System.Drawing.Font("Tahoma", 8);
            cmb_namedars.ListElement.Font = f;
            
        }

        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_back_Click(object sender, EventArgs e)
        {
            main m = new main();
            m.Show();
            this.Hide();
        }

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

        private void btn_searchs_Click(object sender, EventArgs e)
        {
            if (cmbsearch.Text == "کد")
            {
                search_code();
            }
            if (cmbsearch.Text == "نام کلاس")
            {
                search_classname();
            }
            if (cmbsearch.Text == "نوع کلاس")
            {
                search_classtype();
            }
            if (cmbsearch.Text == "شماره کلاس")
            {
                search_classid();
            }
            if (cmbsearch.Text == "نام درس")
            {
                search_namedars();
            }
            if (cmbsearch.Text == "محوریت")
            {
                search_mehvariyat();
            }
            if (cmbsearch.Text == "تاریخ شروع کلاس")
            {
                search_classstartdate();
            }
            if (cmbsearch.Text == "تاریخ پایان کلاس")
            {
                search_classenddate();
            }
            if (cmbsearch.Text == "ساعت شروع کلاس")
            {
                search_classstarttime();
            }
            if (cmbsearch.Text == "نام استاد")
            {
                search_teachername();
            }
        }

        private void dgvkelas_MouseClick(object sender, MouseEventArgs e)
        {
            txt_code.Text = dgvkelas.CurrentRow.Cells[0].Value.ToString();
            txt_classname.Text = dgvkelas.CurrentRow.Cells[1].Value.ToString();
            cmb_typekelas.Text = dgvkelas.CurrentRow.Cells[2].Value.ToString();
            txt_classid.Text = dgvkelas.CurrentRow.Cells[3].Value.ToString();
            cmb_namedars.Text = dgvkelas.CurrentRow.Cells[4].Value.ToString();
            cmb_mehvariyat.Text = dgvkelas.CurrentRow.Cells[5].Value.ToString();
            txt_classstartdate.Text = dgvkelas.CurrentRow.Cells[6].Value.ToString();
            txt_classenddate.Text = dgvkelas.CurrentRow.Cells[7].Value.ToString();
            txt_classstarttime.Text = dgvkelas.CurrentRow.Cells[8].Value.ToString();
            txt_teachername.Text = dgvkelas.CurrentRow.Cells[9].Value.ToString();
        }
    }
}
