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

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

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

            con.Close();
        }

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

            con.Close();
        }

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

            con.Close();
        }

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

            con.Close();
        }


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

            con.Close();
        }


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

            con.Close();
        }


        public void search_paymenttype()
        {
            dgvpayteacher.Rows.Clear();
            SqlConnection con = new SqlConnection("server=(local);database=school;integrated security=true");
            SqlCommand c1 = new SqlCommand();
            con.Open();
            c1.CommandText = "select * from salaryteacher where paymenttype= '" + txt_search.Text + "'";
            c1.Connection = con;
            SqlDataReader dr = c1.ExecuteReader();
            object[] o1 = new object[11];
            while (dr.Read())
            {
                dr.GetValues(o1);
                dgvpayteacher.Rows.Add(o1);
            }

            con.Close();
        }


        public void search_salarydate()
        {
            dgvpayteacher.Rows.Clear();
            SqlConnection con = new SqlConnection("server=(local);database=school;integrated security=true");
            SqlCommand c1 = new SqlCommand();
            con.Open();
            c1.CommandText = "select * from salaryteacher where salarydate= '" + txt_search.Text + "'";
            c1.Connection = con;
            SqlDataReader dr = c1.ExecuteReader();
            object[] o1 = new object[11];
            while (dr.Read())
            {
                dr.GetValues(o1);
                dgvpayteacher.Rows.Add(o1);
            }

            con.Close();
        }

        public void search_paymentamount()
        {
            dgvpayteacher.Rows.Clear();
            SqlConnection con = new SqlConnection("server=(local);database=school;integrated security=true");
            SqlCommand c1 = new SqlCommand();
            con.Open();
            c1.CommandText = "select * from salaryteacher where paymentamount= '" + txt_search.Text + "'";
            c1.Connection = con;
            SqlDataReader dr = c1.ExecuteReader();
            object[] o1 = new object[11];
            while (dr.Read())
            {
                dr.GetValues(o1);
                dgvpayteacher.Rows.Add(o1);
            }

            con.Close();
        }


        public void search_totalamount()
        {
            dgvpayteacher.Rows.Clear();
            SqlConnection con = new SqlConnection("server=(local);database=school;integrated security=true");
            SqlCommand c1 = new SqlCommand();
            con.Open();
            c1.CommandText = "select * from salaryteacher where totalamount= '" + txt_search.Text + "'";
            c1.Connection = con;
            SqlDataReader dr = c1.ExecuteReader();
            object[] o1 = new object[11];
            while (dr.Read())
            {
                dr.GetValues(o1);
                dgvpayteacher.Rows.Add(o1);
            }

            con.Close();
        }

        public void search_debt()
        {
            dgvpayteacher.Rows.Clear();
            SqlConnection con = new SqlConnection("server=(local);database=school;integrated security=true");
            SqlCommand c1 = new SqlCommand();
            con.Open();
            c1.CommandText = "select * from salaryteacher where debt= '" + txt_search.Text + "'";
            c1.Connection = con;
            SqlDataReader dr = c1.ExecuteReader();
            object[] o1 = new object[11];
            while (dr.Read())
            {
                dr.GetValues(o1);
                dgvpayteacher.Rows.Add(o1);
            }

            con.Close();
        }

        private void clear()
        {
            txt_code.Clear();
            txt_name.Clear();
            txt_family.Clear();
            txt_cmeli.Clear();
            txt_sex.Clear();
            txt_type.Clear();
            cmb_typecash.Text = "";
            txt_salarydate.Clear();
            txt_paymentamount.Clear();
            txt_totalamount.Clear();
            txt_debt.Clear();
            
        }

        private void add()
        {
            SqlConnection con = new SqlConnection("server=(local);database=school;integrated security=true");
            con.Open();
            string s1 = "insert into salaryteacher(id,name,family,cmeli,sex,type,paymenttype,salarydate,paymentamount,totalamount,debt)values('" + txt_code.Text + "','" + txt_name.Text + "','" + txt_family.Text + "','" + txt_cmeli.Text + "','" + txt_sex.Text + "','" + txt_type.Text + "','" + cmb_typecash.Text + "','" + txt_salarydate.Text + "','" + txt_paymentamount.Text + "','" + txt_totalamount.Text + "','" + txt_debt.Text + "')";
            SqlCommand c3 = new SqlCommand(s1, con);
            if (MessageBox.Show("آیا می خواهید این را ثبت کنید؟", "اخطار ثبت", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                c3.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 salaryteacher set id ='" + txt_code.Text + "',name='" + txt_name.Text + "',family='" + txt_family.Text + "',cmeli='" + txt_cmeli.Text + "',sex='" + txt_sex.Text + "',type='" + txt_type.Text + "',paymenttype='" + cmb_typecash.Text + "',salarydate='" + txt_salarydate.Text + "',paymentamount='" + txt_paymentamount.Text + "',totalamount='" + txt_totalamount.Text + "',debt='" + txt_debt.Text + "' where id='" + txt_code.Text + "'";
            SqlCommand c3 = new SqlCommand(s1, con);
            if (MessageBox.Show("آیا می خواهید این را ویرایش کنید؟", "اخطار ویرایش", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                try
                {
                    c3.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 salaryteacher where id='" + txt_code.Text + "'";
            SqlCommand c3 = new SqlCommand(s1, con);
            if (MessageBox.Show("آیا می خواهید این را پاک کنید؟", "اخطار حذف", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                c3.ExecuteNonQuery();
                MessageBox.Show("حذف شد");
                show();
            }

            con.Close();

        }


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

        private void txt_code_TextChanged(object sender, EventArgs e)
        {
            dgvpayteacher.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 where id='" + txt_code.Text + "'";
            c1.Connection = con;
            SqlDataReader dr = c1.ExecuteReader();
            if (dr.Read())
            {
                txt_code.Text = dr["id"].ToString();
                txt_name.Text = dr["name"].ToString();
                txt_family.Text = dr["family"].ToString();
                txt_cmeli.Text = dr["cmeli"].ToString();
                txt_sex.Text = dr["sex"].ToString();
                txt_type.Text = dr["type"].ToString();
            }


            con.Close();
            show();
        }

        private void btn_madd_Click(object sender, EventArgs e)
        {
            add();
            show();
            clear();
        }

        private void btn_medit_Click(object sender, EventArgs e)
        {
            edit();
            show();
            clear();
        }

        private void btn_mdelete_Click(object sender, EventArgs e)
        {
            delete();
            show();
            clear();
        }

        private void dgvpayteacher_MouseClick(object sender, MouseEventArgs e)
        {
            txt_code.Text = dgvpayteacher.CurrentRow.Cells[0].Value.ToString();
            txt_name.Text = dgvpayteacher.CurrentRow.Cells[1].Value.ToString();
            txt_family.Text = dgvpayteacher.CurrentRow.Cells[2].Value.ToString();
            txt_cmeli.Text = dgvpayteacher.CurrentRow.Cells[3].Value.ToString();
            txt_sex.Text = dgvpayteacher.CurrentRow.Cells[4].Value.ToString();
            txt_type.Text = dgvpayteacher.CurrentRow.Cells[5].Value.ToString();
            cmb_typecash.Text = dgvpayteacher.CurrentRow.Cells[6].Value.ToString();
            txt_salarydate.Text = dgvpayteacher.CurrentRow.Cells[7].Value.ToString();
            txt_paymentamount.Text = dgvpayteacher.CurrentRow.Cells[8].Value.ToString();
            txt_totalamount.Text = dgvpayteacher.CurrentRow.Cells[9].Value.ToString();
            txt_debt.Text = dgvpayteacher.CurrentRow.Cells[10].Value.ToString();
        }

        private void btn_mall_Click(object sender, EventArgs e)
        {
            show();
            txt_search.Clear();
        }

        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 (cmbsearch.Text == "کد")
            {
                search_code();
            }
            if (cmbsearch.Text == "نام")
            {
                search_name();
            }
            if (cmbsearch.Text == "نام خانوادگی")
            {
                search_family();
            }
            if (cmbsearch.Text == "کدملی")
            {
                search_cmeli();
            }
            if (cmbsearch.Text == "جنسیت")
            {
                search_sex();
            }
            if (cmbsearch.Text == "نوع")
            {
                search_type();
            }
            if (cmbsearch.Text == "نوع پرداخت")
            {
                search_paymenttype();
            }
            if (cmbsearch.Text == "تاریخ پرداخت")
            {
                search_salarydate();
            }
            if (cmbsearch.Text == "مبلغ پرداختی")
            {
                search_paymentamount();
            }
            if (cmbsearch.Text == "مبلغ کل حقوق")
            {
                search_totalamount();
            }
            if (cmbsearch.Text == "مانده بدهی")
            {
                search_debt();
            }


        }

        private void txt_paymentamount_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (txt_paymentamount.Text == "0")
                {
                    txt_paymentamount.Text = "";
                }

                txt_debt.Text = (Int32.Parse(txt_totalamount.Text) - Int32.Parse(txt_paymentamount.Text)).ToString();
            }
            catch (Exception)
            {

            }
        }

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



    }
}
