using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using Int_to_Str;

namespace shop
{
    public partial class KHARID : Form
    {
        public KHARID()
        {
            InitializeComponent();
        }

        void display()
        {
            OleDbConnection con1 = new OleDbConnection();
            con1.ConnectionString = "provider=microsoft.jet.oledb.4.0; data source=shoping.mdb";
            OleDbDataAdapter da = new OleDbDataAdapter("select * from kharid", con1);
            DataSet ds = new DataSet();
            da.Fill(ds, "kharid");
            dataGridView1.DataSource = ds.Tables["kharid"].DefaultView;
            con1.Close();
        }

        private void KHARID_Load(object sender, EventArgs e)
        {
            display();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                OleDbConnection con = new OleDbConnection();
                con.ConnectionString = "provider=microsoft.jet.oledb.4.0; data source=shoping.mdb";
                OleDbCommand com = new OleDbCommand();
                com.CommandText = "insert into KHARID(factor,sherkat,tarikh,nam,fi,tedad,kol) values ('" + txtfactor.Text + "','" + txtnamefrushgah.Text + "','" + masked.Text + "','" + txtkala.Text + "','" + txtgheymat.Text + "','" + txttedad.Text + "','" + lblkol.Text + "')";
                con.Open();
                com.Connection = con;
                com.ExecuteNonQuery();
                con.Close();
                display();
                txtcod.Text = txtgheymat.Text = txtkala.Text = txttedad.Text = lblkol.Text = "";
            }
            catch { MessageBox.Show("خطایی رخ داده است دباره بررسی کنید", "                                            توجه"); }
        }

        private void txttedad_Leave(object sender, EventArgs e)
        {
            if (txttedad.Text == "")
            {
                MessageBox.Show("بخش تعداد كالا را وارد كنيد", "خطا", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            long a, b, s = 0;
            a = long.Parse(txtgheymat.Text);
            b = long.Parse(txttedad.Text);
            s += (a * b);
            lblkol.Text = s.ToString();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                OleDbConnection con = new OleDbConnection();
                con.ConnectionString = "provider=microsoft.jet.oledb.4.0; data source=shoping.mdb";
                OleDbCommand com = new OleDbCommand();
                com.Connection = con;
                int item;
                com.CommandText = "select count(saat) from KHARID where saat='" + txtsaat.Text + "'";
                con.Open();
                item = (int)com.ExecuteScalar();
                con.Close();
                if (item != 0)
                {
                    if (MessageBox.Show("آيا مي خواهيد بيمار مورد نظز حذف شود؟", "توجه", MessageBoxButtons.YesNo) == DialogResult.Yes)
                        com.CommandText = "delete from KHARID where nam='" + txtkala.Text + "' and factor='" + txtfactor.Text + "' and saat='" + txtsaat.Text + "'";
                    con.Open();
                    com.ExecuteNonQuery();
                    con.Close();
                    display();
                    cls();
                }
                else
                {
                    MessageBox.Show("!كالاي مورد نظر موجود نيست", "خطا", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch { MessageBox.Show("خطایی رخ داده است دباره بررسی کنید", "                                            توجه"); }
        }

        private void cls()
        {
            txtfactor.Text = txtgheymat.Text = txtkala.Text = txtnamefrushgah.Text = txttedad.Text = masked.Text = lblkol.Text = txtcod.Text = "";
        }

        private void button5_Click(object sender, EventArgs e)
        {
            cls();
            textBox1.Visible = false;
        }

        private void txtcod_TextChanged(object sender, EventArgs e)
        {
            OleDbConnection con1 = new OleDbConnection();
            con1.ConnectionString = "provider=microsoft.jet.oledb.4.0; data source=shoping.mdb";
            OleDbDataAdapter da = new OleDbDataAdapter("select * from KHARID where ID like'%" + txtcod.Text + "%'", con1);
            DataSet ds = new DataSet();
            da.Fill(ds, "KHARID");
            dataGridView1.DataSource = ds.Tables["KHARID"].DefaultView;
            con1.Close();
        }

        private void txtfactor_TextChanged(object sender, EventArgs e)
        {
            //OleDbConnection con1 = new OleDbConnection();
            //con1.ConnectionString = "provider=microsoft.jet.oledb.4.0; data source=shoping.mdb";
            //OleDbDataAdapter da = new OleDbDataAdapter("select * from KHARID where factor like'%" + txtfactor.Text + "%'", con1);
            //DataSet ds = new DataSet();
            //da.Fill(ds, "KHARID");
            //dataGridView1.DataSource = ds.Tables["KHARID"].DefaultView;
            //con1.Close();
        }

        private void txtcod_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(char.IsDigit(e.KeyChar) || char.IsControl(e.KeyChar)))
            {
                e.Handled = true;
            }
        }

        private void txtgheymat_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(char.IsDigit(e.KeyChar) || char.IsControl(e.KeyChar)))
            {
                e.Handled = true;
            }
        }

        private void txttedad_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(char.IsDigit(e.KeyChar) || char.IsControl(e.KeyChar)))
            {
                e.Handled = true;
            }
        }

        private void txtgheymat_Leave(object sender, EventArgs e)
        {
            if (txtgheymat.Text == "")
            {
                MessageBox.Show("بخش قيمت خريد را وارد كنيد", "خطا", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                OleDbConnection con1 = new OleDbConnection();
                con1.ConnectionString = "provider=microsoft.jet.oledb.4.0; data source=shoping.mdb";
                OleDbDataAdapter da = new OleDbDataAdapter("select * from KHARID where factor='" + txtfactor.Text + "'", con1);
                DataSet ds = new DataSet();
                da.Fill(ds, "KHARID");
                dataGridView1.DataSource = ds.Tables["KHARID"].DefaultView;
                con1.Close();
                textBox1.Visible = true;
                decimal sum = 0;
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    sum += Convert.ToDecimal(dataGridView1.Rows[i].Cells["kol"].Value);
                    dataGridView1.Rows[i].Cells["sherkat"].Value = i + 1;
                }
                txt1.Text = sum.ToString();
                textBox1.Text = Persian_Number_To_String.GET_Number_To_PersianString(txt1.Text);
            }
            catch { MessageBox.Show("خطایی رخ داده است دباره بررسی کنید", "                                            توجه"); }
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView1.CurrentRow.Cells[0].Value.ToString() == "")
            {
                MessageBox.Show(".لطفا كالاي مورد نظر را انتخاب کنید", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                txtfactor.Text = Convert.ToString(dataGridView1.CurrentRow.Cells["factor"].Value);
                txtnamefrushgah.Text = Convert.ToString(dataGridView1.CurrentRow.Cells["sherkat"].Value);
                masked.Text = Convert.ToString(dataGridView1.CurrentRow.Cells["tarikh"].Value);
                txtkala.Text = Convert.ToString(dataGridView1.CurrentRow.Cells["nam"].Value);
                txtcod.Text = Convert.ToString(dataGridView1.CurrentRow.Cells["ID"].Value);
                txtgheymat.Text = Convert.ToString(dataGridView1.CurrentRow.Cells["fi"].Value);
                txttedad.Text = Convert.ToString(dataGridView1.CurrentRow.Cells["tedad"].Value);
                lblkol.Text = Convert.ToString(dataGridView1.CurrentRow.Cells["kol"].Value);
            }
        }

        public void con(string s, int num)
        {
            OleDbConnection co = new OleDbConnection();
            co.ConnectionString = "provider=microsoft.jet.oledb.4.0; data source=shoping.mdb";
            co.Open();
            OleDbCommand cmd = new OleDbCommand();
            cmd.CommandText = s;
            cmd.Connection = co;
            switch (num)
            {
                case 1:
                    {
                        try
                        {
                            cmd.ExecuteNonQuery();
                            MessageBox.Show("     عملیات با موفقیت انجام شد     ");
                        }
                        catch
                        {
                            MessageBox.Show("     عملیات با موفقیت انجام نشد     ");
                        }
                        break;
                    }
                case 2:
                    {
                        OleDbDataReader dr = cmd.ExecuteReader();
                        object[] o1 = new object[6];
                        while (dr.Read())
                        {
                            dr.GetValues(o1);
                            dataGridView1.Rows.Add(o1);
                        }
                        break;
                    }
            }
            co.Close();
        }

        //private void button3_Click_1(object sender, EventArgs e)
        //{
        //    OleDbConnection con = new OleDbConnection();
        //    con.ConnectionString = "provider=microsoft.jet.oledb.4.0; data source=shoping.mdb";
        //    OleDbCommand com = new OleDbCommand();
        //    com.CommandText = "update KHARID set factor='" + txtfactor.Text + "',sherkat='" + txtnamefrushgah.Text + "',tarikh='" + masked.Text + "',nam='" + txtkala.Text + "',fi='" + txtgheymat.Text + "',tedad='" + txttedad.Text + "',kol='" + lblkol.Text + "' where ID='" + txtcod.Text + "'";
        //    con.Open();
        //    com.Connection = con;
        //    com.ExecuteNonQuery();
        //    con.Close();
        //    cls();
        //    display();

        //}

        private void dataGridView1_CellClick_1(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView1.CurrentRow.Cells[0].Value.ToString() == "")
            {
                MessageBox.Show(".لطفا كالاي مورد نظر را انتخاب کنید", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                txtcod.Text = Convert.ToString(dataGridView1.CurrentRow.Cells["id"].Value);
                txtfactor.Text = Convert.ToString(dataGridView1.CurrentRow.Cells["factor"].Value);
                txtnamefrushgah.Text = Convert.ToString(dataGridView1.CurrentRow.Cells["sherkat"].Value);
                masked.Text = Convert.ToString(dataGridView1.CurrentRow.Cells["tarikh"].Value);
                txtkala.Text = Convert.ToString(dataGridView1.CurrentRow.Cells["nam"].Value);
                txtgheymat.Text = Convert.ToString(dataGridView1.CurrentRow.Cells["fi"].Value);
                txttedad.Text = Convert.ToString(dataGridView1.CurrentRow.Cells["tedad"].Value);
                lblkol.Text = Convert.ToString(dataGridView1.CurrentRow.Cells["kol"].Value);
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            lblkol.Text = Persian_Number_To_String.GET_Number_To_PersianString(textBox1.Text);
        }
    }
}
    
