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

        


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

            con.Close();
        }

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

            con.Close();
        }

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

            con.Close();
        }

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

            con.Close();
        }

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

            con.Close();
        }

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

            con.Close();
        }

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

            con.Close();
        }

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

            con.Close();
        }

    

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

        private void clear()
        {

            txt_code.Clear();
            txt_name.Clear();
            txt_tvahed.Clear();
            txt_timedars.Clear();
            txt_dateexam.Clear();
            txt_timeexam.Clear();


        }

        private void add()
        {
            SqlConnection con = new SqlConnection("server=(local);database=school;integrated security=true");
            con.Open();
            string s1 = "insert into dars(id,name,tvahed,day,clock,shift,examdate,examtime)values('" + txt_code.Text + "','" + txt_name.Text + "','" + txt_tvahed.Text + "','" +cmb_days.Text+ "','" +txt_timedars.Text+ "','" +cmb_shift.Text+ "','" +txt_dateexam.Text+ "','" +txt_timeexam.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 dars set id ='" + txt_code.Text + "',name='" + txt_name.Text + "',tvahed='" + txt_tvahed.Text + "',day='" + cmb_days.Text + "',clock='" + txt_timedars.Text + "',shift='" + cmb_shift.Text + "',examdate='" + txt_dateexam.Text + "',examtime='" +txt_timeexam.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 dars 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 dgvdars_MouseClick(object sender, MouseEventArgs e)
        {
            txt_code.Text = dgvdars.CurrentRow.Cells[0].Value.ToString();
            txt_name.Text = dgvdars.CurrentRow.Cells[1].Value.ToString();
            txt_tvahed.Text = dgvdars.CurrentRow.Cells[2].Value.ToString();
            cmb_days.Text = dgvdars.CurrentRow.Cells[3].Value.ToString();
            txt_timedars.Text = dgvdars.CurrentRow.Cells[4].Value.ToString();
            cmb_shift.Text = dgvdars.CurrentRow.Cells[5].Value.ToString();
            txt_dateexam.Text = dgvdars.CurrentRow.Cells[6].Value.ToString();
            txt_timeexam.Text = dgvdars.CurrentRow.Cells[7].Value.ToString();
        }

        private void btn_searchs_Click(object sender, EventArgs e)
        {
            if (cmbsearch.Text == "کد")
            {
                search_code();
            }
            if (cmbsearch.Text == "نام درس")
            {
                search_name();
            }
            if (cmbsearch.Text == "تعداد واحد")
            {
                search_tvahed();
            }
            if (cmbsearch.Text == "روز درس")
            {
                search_days();
            }
            if (cmbsearch.Text == "ساعت درس")
            {
                search_timedars();
            }
            if (cmbsearch.Text == "شیفت")
            {
                search_shift();
            }
            if (cmbsearch.Text == "تاریخ امتحان")
            {
                search_examdate();
            }
            if (cmbsearch.Text == "ساعت امتحان")
            {
                search_examtime();
            }
           
        }

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

           
        }
    }
}
