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;

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

        private void cls()
        {
            txtadres.Text = txtfactor.Text = 
            txtfamili.Text = txtnam.Text = 
            txtpish.Text = txtsaheb.Text = 
            txtshomare.Text = txttel.Text = 
            mask1.Text = mask2.Text = "";
        }

        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 ghest(tkharid,nam,famili,adres,tel,factor,pish,mande,shomare,saheb,tvsul) values ('" + mask1.Text + "','" + txtnam.Text + "','" + txtfamili.Text + "','" + txtadres.Text + "','" + txttel.Text + "','" + txtfactor.Text + "','" + txtpish.Text + "','" + lblmande.Text + "','" + txtshomare.Text + "','" + txtsaheb.Text + "','" + mask2.Text + "')";
            con.Open();
            com.Connection = con;
            com.ExecuteNonQuery();
            con.Close();
            display();
            cls();
        }

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

        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 ghest where nam='" + txtnam.Text + "'";
            con.Open();
            item = (int)com.ExecuteScalar();
            con.Close();
            if (item != 0)
            {
                if (MessageBox.Show("آيا مي خواهيد شخص مورد نظز حذف شود؟", "توجه", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    com.CommandText = "delete from ghest where nam='" + txtnam.Text + "' and famili='" + txtfamili.Text + "'and tkharid='" + mask1.Text + "'";
                con.Open();
                com.ExecuteNonQuery();
                con.Close();
                display();
                cls();
            }
            else
            {
                MessageBox.Show("!شخص مورد نظر موجود نيست", "خطا", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

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

        private void txtpish_Leave(object sender, EventArgs e)
        {
            if (txtfactor.Text == "")
            {
                MessageBox.Show("بخش جمع فاكتور را وارد گنيد", "خطا", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            if (txtpish.Text == "")
            {
                MessageBox.Show("بخش مقدار پيش پرداخت را وارد كنيد", "خطا", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                long j, p, m = 0;
                j = long.Parse(txtfactor.Text);
                p = long.Parse(txtpish.Text);
                m += (j - p);
                lblmande.Text = m.ToString();
            }
        }

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

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

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

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

        private void txtnam_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 ghest where nam like'%" + txtnam.Text + "%'", con1);
            DataSet ds = new DataSet();
            da.Fill(ds, "ghest");
            dataGridView1.DataSource = ds.Tables["ghest"].DefaultView;
            con1.Close();
        }

        private void txtfamili_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 ghest where famili like'%" + txtfamili.Text + "%'", con1);
            DataSet ds = new DataSet();
            da.Fill(ds, "ghest");
            dataGridView1.DataSource = ds.Tables["ghest"].DefaultView;
            con1.Close();
        }

        private void txtsaheb_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 ghest where saheb like'%" + txtsaheb.Text + "%'", con1);
            DataSet ds = new DataSet();
            da.Fill(ds, "ghest");
            dataGridView1.DataSource = ds.Tables["ghest"].DefaultView;
            con1.Close();
        }

        private void txtshomare_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 ghest where shomare like'%" + txtshomare.Text + "%'", con1);
            DataSet ds = new DataSet();
            da.Fill(ds, "ghest");
            dataGridView1.DataSource = ds.Tables["ghest"].DefaultView;
            con1.Close();
        }

        private void txttel_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 ghest where tel like'%" + txttel.Text + "%'", con1);
            DataSet ds = new DataSet();
            da.Fill(ds, "ghest");
            dataGridView1.DataSource = ds.Tables["ghest"].DefaultView;
            con1.Close();
        }
    }
}
