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

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

            con.Close();
        }

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

            con.Close();
        }

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

            con.Close();
        }

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

            con.Close();
        }

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

            con.Close();
        }

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

            con.Close();
        }

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

            con.Close();
        }

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

            con.Close();
        }

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

            con.Close();
        }

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

            con.Close();
        }

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

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

        }

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

            con.Close();
        }



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

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

            con.Close();

        }

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

            con.Close();

        }

        private void add_btn_Click(object sender, EventArgs e)
        {
            fadd();
            fshow();
            fclear();
        }

        private void edit_btn_Click(object sender, EventArgs e)
        {
            fedit();
            fshow();
            fclear();
        }

        private void delete_btn_Click(object sender, EventArgs e)
        {
            fdelete();
            fshow();
            fclear();
        }

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

        private void search_btn_Click(object sender, EventArgs e)
        {
            if (cmbo_fsearch.Text == "کد")
            {
                fsearch_code();
            }
            if (cmbo_fsearch.Text == "نام")
            {
                fsearch_name();
            }
            if (cmbo_fsearch.Text == "نام خانوادگی")
            {
                fsearch_family();
            }
            if (cmbo_fsearch.Text == "نام پدر")
            {
                fsearch_fname();
            }
            if (cmbo_fsearch.Text == "شماره شناسنامه")
            {
                fsearch_shenasname();
            }
            if (cmbo_fsearch.Text == "کد ملی")
            {
                fsearch_cmeli();
            }
            if (cmbo_fsearch.Text == "نام دوره")
            {
                fsearch_dore();
            }
            if (cmbsearch.Text == "مدرک تحصیلی")
            {
                fsearch_elevel();
            }
            if (cmbo_fsearch.Text == "تلفن")
            {
                fsearch_tel();
            }
            if (cmbo_fsearch.Text == "موبایل")
            {
                fsearch_mob();
            }
        }

        private void all_btn_Click(object sender, EventArgs e)
        {
            fshow();
            txt_fsearch.Clear();
        }

        private void fstudent_Load(object sender, EventArgs e)
        {
            fshow();
        }

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