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 System.Data.SqlClient;
using DevComponents.DotNetBar;

namespace academy
{
    public partial class paymentstudent : DevComponents.DotNetBar.OfficeForm
    {
        public paymentstudent()
        {
            InitializeComponent();
        }

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

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

            con.Close();
        }

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

            con.Close();
        }

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

            con.Close();
        }

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

            con.Close();
        }


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

            con.Close();
        }


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

            con.Close();
        }


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

            con.Close();
        }


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

            con.Close();
        }

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

            con.Close();
        }


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

            con.Close();
        }

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

            con.Close();
        }


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

            con.Close();
        }

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

            con.Close();
        }

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

            con.Close();
        }

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

            con.Close();
        }

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

            con.Close();
        }

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

            con.Close();
        }


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

            con.Close();
        }


        private void clear()
        {
            txt_code.Clear();
            txt_name.Clear();
            txt_family.Clear();
            txt_cmeli.Clear();
            cmb_typepay.Text = "";
            cmb_typecash.Text = "";
            cmb_typeaghsat.Text = "";
            txt_numberaghsat.Clear();
            txt_numbercheck.Clear();
            txt_datecheck.Clear();
            txt_amountcheck.Clear();
            cmb_statuscheck.Text = "";
            txt_amountpayment.Clear();
            txt_amountpayable.Clear();
            txt_datepayment.Clear();
            txt_trackingcode.Clear();
            cmb_namedore.Text = "";
            txt_debt.Clear();
        }


        private void add()
        {
            SqlConnection con = new SqlConnection("server=(local);database=school;integrated security=true");
            con.Open();
            string s1 = "insert into paymentstudent(id,name,family,cmeli,typepay,typecash,typeaghsat,numberaghsat,numbercheck,datecheck,amountcheck,statuscheck,namedars,amountpayment,amountpayable,datepayment,trackingcode,debt)values('" + txt_code.Text + "','" + txt_name.Text + "','" + txt_family.Text + "','" + txt_cmeli.Text + "','" + cmb_typepay.Text + "','" + cmb_typecash.Text + "','" + cmb_typeaghsat.Text + "','" + txt_numberaghsat.Text + "','" + txt_numbercheck.Text + "','" + txt_datecheck.Text + "','" + txt_amountcheck.Text + "','" + cmb_statuscheck.Text + "','" + cmb_namedore.Text + "','" + txt_amountpayment.Text + "','" + txt_amountpayable.Text + "','" + txt_datepayment.Text + "','" + txt_trackingcode.Text + "','" + txt_debt.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 paymentstudent set id ='" + txt_code.Text + "',name='" + txt_name.Text + "',family='" + txt_family.Text + "',cmeli='" + txt_cmeli.Text + "',typepay='" + cmb_typepay.Text + "',typecash='" + cmb_typecash.Text + "',typeaghsat='" + cmb_typeaghsat.Text + "',numberaghsat='" + txt_numberaghsat.Text + "',numbercheck='" + txt_numbercheck.Text + "',datecheck='" + txt_datecheck.Text + "',amountcheck='" + txt_amountcheck.Text + "',statuscheck='" + cmb_statuscheck.Text + "',namedars='" + cmb_namedore.Text + "',amountpayment='" + txt_amountpayment.Text + "',amountpayable='" + txt_amountpayable.Text + "',datepayment='" + txt_datepayment.Text + "',trackingcode='" + txt_trackingcode.Text + "',debt='" + txt_debt.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 paymentstudent 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()
        {
            dgvpays.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 paymentstudent";
            c1.Connection = con;
            SqlDataReader dr = c1.ExecuteReader();
            object[] o1 = new object[18];
            while (dr.Read())
            {
                dr.GetValues(o1);
                dgvpays.Rows.Add(o1);
            }
            con.Close();
        }

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

        private void txt_code_TextChanged(object sender, EventArgs e)
        {
            dgvpays.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 student 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();

            }


            con.Close();
            show();
        }

        private void cmb_typepay_SelectedIndexChanged(object sender, Telerik.WinControls.UI.Data.PositionChangedEventArgs e)
        {
            if (cmb_typepay.SelectedIndex == 0)
            {
                cmb_typeaghsat.Enabled = false;
                txt_numberaghsat.Enabled = false;
                txt_numbercheck.Enabled = false;
                txt_datecheck.Enabled = false;
                txt_amountcheck.Enabled = false;
                cmb_statuscheck.Enabled = false;
                txt_amountpayable.Text = txt_amountpayment.Text;
                txt_debt.Text = "0";
                cmb_typeaghsat.Text = "";
                txt_numberaghsat.Clear();
                txt_numbercheck.Clear();
                txt_datecheck.Clear();
                txt_amountcheck.Clear();
                cmb_statuscheck.Text = "";


            }

            else
            {
                cmb_typeaghsat.Enabled = true;
                txt_numberaghsat.Enabled = true;
                txt_numbercheck.Enabled = true;
                txt_datecheck.Enabled = true;
                txt_amountcheck.Enabled = true;
                cmb_statuscheck.Enabled = true;

 
            }
        }

        private void cmb_namedore_TextChanged(object sender, EventArgs e)
        {
            dgvpays.Rows.Clear();
            SqlConnection con = new SqlConnection();
            con.ConnectionString = "server=(local);database=school;integrated security=true";
            SqlCommand c1 = new SqlCommand();
            con.Open();
            c1.CommandText = "Select price from priceclass where namedars='" + cmb_namedore.Text + "'";
            c1.Connection = con;
            SqlDataReader dr = c1.ExecuteReader();
            if (dr.Read())
            {
                txt_amountpayment.Text = dr["price"].ToString();

            }


            con.Close();
            show();
        }

        private void txt_numberaghsat_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (txt_numberaghsat.Text== "0")
                {
                    txt_numberaghsat.Text = "";
                }
                txt_amountpayable.Text = (Int32.Parse(txt_amountpayment.Text) / Int32.Parse(txt_numberaghsat.Text)).ToString();
                txt_debt.Text = (Int32.Parse(txt_amountpayment.Text) - Int32.Parse(txt_amountpayable.Text)).ToString();
            }
            catch (Exception)
            {

            }
        }

        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 dgvpays_MouseClick(object sender, MouseEventArgs e)
        {
            txt_code.Text = dgvpays.CurrentRow.Cells[0].Value.ToString();
            txt_name.Text = dgvpays.CurrentRow.Cells[1].Value.ToString();
            txt_family.Text = dgvpays.CurrentRow.Cells[2].Value.ToString();
            txt_cmeli.Text = dgvpays.CurrentRow.Cells[3].Value.ToString();
            cmb_typepay.Text = dgvpays.CurrentRow.Cells[4].Value.ToString();
            cmb_typecash.Text = dgvpays.CurrentRow.Cells[5].Value.ToString();
            cmb_typeaghsat.Text = dgvpays.CurrentRow.Cells[6].Value.ToString();
            txt_numberaghsat.Text = dgvpays.CurrentRow.Cells[7].Value.ToString();
            txt_numbercheck.Text = dgvpays.CurrentRow.Cells[8].Value.ToString();
            txt_datecheck.Text = dgvpays.CurrentRow.Cells[9].Value.ToString();
            txt_amountcheck.Text = dgvpays.CurrentRow.Cells[10].Value.ToString();
            cmb_statuscheck.Text = dgvpays.CurrentRow.Cells[11].Value.ToString();
            cmb_namedore.Text = dgvpays.CurrentRow.Cells[12].Value.ToString();
            txt_amountpayment.Text = dgvpays.CurrentRow.Cells[13].Value.ToString();
            txt_datepayment.Text = dgvpays.CurrentRow.Cells[14].Value.ToString();
            txt_trackingcode.Text = dgvpays.CurrentRow.Cells[15].Value.ToString();
            txt_amountpayable.Text = dgvpays.CurrentRow.Cells[16].Value.ToString();
            txt_debt.Text = dgvpays.CurrentRow.Cells[17].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_family();
            }
            if (cmbsearch.Text == "کدملی")
            {
                search_cmeli();
            }
            if (cmbsearch.Text == "نوع پرداخت")
            {
                search_typepay();
            }
            if (cmbsearch.Text == "روش پرداخت نقدی")
            {
                search_typecash();
            }
            if (cmbsearch.Text == "روش پرداخت اقساط")
            {
                search_typeaghsat();
            }
            if (cmbsearch.Text == "تعداد اقساط")
            {
                search_numberaghsat();
            }
            if (cmbsearch.Text == "شماره چک / سفته")
            {
                search_numbercheck();
            }
            if (cmbsearch.Text == "تاریخ چک / سفته")
            {
                search_datecheck();
            }
            if (cmbsearch.Text == "مبلغ چک / سفته")
            {
                search_amountcheck();
            }
            if (cmbsearch.Text == "وضعیت چک / سفته")
            {
                search_statuscheck();
            }
            if (cmbsearch.Text == "بابت دوره")
            {
                search_namedars();
            }
            if (cmbsearch.Text == "مبلغ قابل پرداخت")
            {
                search_amountpayment();
            }
            if (cmbsearch.Text == "تاریخ پرداخت")
            {
                search_datepayment();
            }
            if (cmbsearch.Text == "کد رهگیری / شماره فیش")
            {
                search_trackingcode();
            }
            if (cmbsearch.Text == "مبلغ  پرداختی")
            {
                search_amountpayable();
            }
            if (cmbsearch.Text == "مانده بدهی")
            {
                search_debt();
            }


        }






    }
}
