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.OleDb;

namespace shop
{
    public partial class frush : Form
    {
        public frush()
        {
            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 frush", con1);
            DataSet ds = new DataSet();
            da.Fill(ds, "frush");
            dataGridView1.DataSource = ds.Tables["frush"].DefaultView;
            con1.Close();
        }

        private void cls()
        {
            txtfrush.Text = txtid.Text = lblkharid.Text = txtname.Text = txttedad.Text = maskedTextBox1.Text = lbldaramad.Text = lblkol.Text = "";
        }

        private void txtfrush_Leave(object sender, EventArgs e)
        {
            if (txtfrush.Text == "")
            {
                MessageBox.Show("بخش فروش را وارد كنيد", "خطا", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            long a, b, s, c = 0, d = 0;
            a = long.Parse(txttedad.Text);
            b = long.Parse(txtfrush.Text);
            s = a * b;
            lblkol.Text = s.ToString();
            d = long.Parse(lblkharid.Text);
            c = (b - d) * a;
            lbldaramad.Text = c.ToString();
        }

        public static string Tid = "";
        private void frush_Load(object sender, EventArgs e)
        {
            display();

            //try
            //{
            //    Tid = "";
            //    DataManagement.DT = DataManagement.Search("select Tid from properties where left(Tid,1)='U'");
            //    Tid = "U" + (DataManagement.DT.Rows.Count + 1).ToString();

            //    DataManagement.DT = DataManagement.Search("select Id from properties");
            //    textBox1.Text = (DataManagement.DT.Rows.Count + 1).ToString();
            //}
            //catch { }

            //this.Height = 448;
            //this.Width = 445;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            System.Globalization.PersianCalendar p = new System.Globalization.PersianCalendar();
            maskedTextBox1.Text = p.GetYear(DateTime.Now).ToString() + p.GetMonth(DateTime.Now).ToString("0#") + p.GetDayOfMonth(DateTime.Now).ToString("0#");
            SendKeys.Send("{Tab}");
        }

        private void txtname_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 ID,nam,fi from KHARID where nam like'%" + txtname.Text + "%'", con1);
            DataSet ds = new DataSet();
            da.Fill(ds, "KHARID");
            dataGridView2.DataSource = ds.Tables["KHARID"].DefaultView;
            con1.Close();
        }

        private void txtid_Leave(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.Connection = con;
            com.CommandText = "select nam,fi from kharid where cod='" + txtid.Text + "'";
            OleDbDataReader dr;
            con.Open();
            dr = com.ExecuteReader();
            if (dr.Read())
            {
                txtname.Text = dr["nam"].ToString();
                lblkharid.Text = dr["fi"].ToString();
            }
            else
            {
                MessageBox.Show("!كد مورد نظر موجود نيست", "خطا", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        
        private void button5_Click_1(object sender, EventArgs e)
        {
            cls();
        }

        private void txtid_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 txtkharid_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(char.IsDigit(e.KeyChar) || char.IsControl(e.KeyChar)))
            {
                e.Handled = true;
            }
        }

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

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView1.CurrentRow.Cells[0].Value.ToString() == "")
            {
                MessageBox.Show(".لطفا كالاي مورد نظر را انتخاب کنید", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                maskedTextBox1.Text = Convert.ToString(dataGridView1.CurrentRow.Cells["tarikh"].Value);
                txtname.Text = Convert.ToString(dataGridView1.CurrentRow.Cells["nam"].Value);
                txttedad.Text = Convert.ToString(dataGridView1.CurrentRow.Cells["tedad"].Value);
                txtfrush.Text = Convert.ToString(dataGridView1.CurrentRow.Cells["gheymat"].Value);
                lbldaramad.Text = Convert.ToString(dataGridView1.CurrentRow.Cells["daramad"].Value);
                lblkol.Text = Convert.ToString(dataGridView1.CurrentRow.Cells["kol"].Value);
            }
        }
        

        private void button5_Click(object sender, EventArgs e)
        {
            cls();
        }

        private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView2.CurrentRow.Cells[0].Value.ToString() == "")
            {
                MessageBox.Show(".لطفا كد مورد نظر را انتخاب کنید", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                txtname.Text = Convert.ToString(dataGridView2.CurrentRow.Cells["nam1"].Value);
                txtid.Text = Convert.ToString(dataGridView2.CurrentRow.Cells["ID"].Value);
                lblkharid.Text = Convert.ToString(dataGridView2.CurrentRow.Cells["fi"].Value);
            }
        }

        private void button1_Click(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 = "insert into frush(tarikh,nam,tedad,gheymat,daramad,kol) values ('" + maskedTextBox1.Text + "','" + txtname.Text + "','" + txttedad.Text + "','" + txtfrush.Text + "','" + lbldaramad.Text + "','" + lblkol.Text + "')";
            con.Open();
            com.Connection = con;
            com.ExecuteNonQuery();
            con.Close();
            display();
            cls();
        }

        private void button3_Click(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.Connection = con;
            int item;
            com.CommandText = "select count(nam) from frush where nam='" + txtname.Text + "'";
            con.Open();
            item = (int)com.ExecuteScalar();
            con.Close();
            if (item != 0)
            {
                if (MessageBox.Show("آيا مي خواهيد كالاي مورد نظز حذف شود؟", "توجه", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    com.CommandText = "delete from frush where nam='" + txtname.Text + "' and tarikh='" + maskedTextBox1.Text + "'";
                con.Open();
                com.ExecuteNonQuery();
                con.Close();
                display();
                cls();
            }
            else
            {
                MessageBox.Show("!كالاي مورد نظر موجود نيست", "خطا", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
