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 car
{
    public partial class add_edit_customer : Form
    {
        public add_edit_customer()
        {
            InitializeComponent();
        }
        OleDbConnection con = new OleDbConnection("provider=microsoft.jet.oledb.4.0; data source=cardb.mdb");
        OleDbCommand cmd = new OleDbCommand();
        public bool Add_Edit = true;

        void display()
        {
            DataSet ds = new DataSet();
            OleDbDataAdapter adp = new OleDbDataAdapter();
            adp.SelectCommand = new OleDbCommand();
            adp.SelectCommand.Connection = con;
            adp.SelectCommand.CommandText = "select * from customer";
            adp.Fill(ds, "t1");
            dataGridViewX1.DataSource = ds;
            dataGridViewX1.DataMember = "t1";
        }

        public void empty()
        {
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
            textBox8.Text = "";
            textBox6.Text = "";
            textBox7.Text = "";
            textBox9.Text = "";
            txtdate.Text = "";
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            OleDbConnection con = new OleDbConnection();
            con.ConnectionString = "provider=microsoft.jet.oledb.4.0; data source=cardb.mdb";
            OleDbCommand myCommand = new OleDbCommand();
            string query = "insert into customer (customer_id,cname,caddress,ctell,date_register,car_id,car_name,sale_price,price_in,debit) VALUES ('" + textBox1.Text + "', '" + textBox2.Text + "','" + textBox4.Text + "','" + textBox3.Text + "','" + txtdate.Text + "','" + textBox5.Text + "','" + textBox6.Text + "','" + textBox7.Text + "','" + textBox9.Text + "','" + textBox8.Text + "')";
            con.Open();
            myCommand.CommandText = query;
            myCommand.Connection = con;
            myCommand.ExecuteNonQuery();
            con.Close();
            display();
            MessageBox.Show("مشتری با موفقیت ثبت گردید!");
            empty();
        }

        private void add_edit_customer_Load(object sender, EventArgs e)
        {
            InputLanguage.CurrentInputLanguage = InputLanguage.FromCulture(System.Globalization.CultureInfo.CreateSpecificCulture("fa-IR"));

            display();

            dataGridViewX1.Columns[0].Width = 100;
            dataGridViewX1.Columns[1].Width = 110;
            dataGridViewX1.Columns[2].Width = 110;
            dataGridViewX1.Columns[3].Width = 118;
            dataGridViewX1.Columns[4].Width = 110;
            dataGridViewX1.Columns[5].Width = 110;
            dataGridViewX1.Columns[6].Width = 113;
            dataGridViewX1.Columns[7].Width = 115;
            dataGridViewX1.Columns[8].Width = 113;
            dataGridViewX1.Columns[9].Width = 115;

            dataGridViewX1.Columns[0].HeaderText = "کد مشتری";
            dataGridViewX1.Columns[1].HeaderText = "نام ونام خانوادگی";
            dataGridViewX1.Columns[2].HeaderText = "آدرس";
            dataGridViewX1.Columns[3].HeaderText = "تلفن";
            dataGridViewX1.Columns[4].HeaderText = "تاریخ ثبت";
            dataGridViewX1.Columns[5].HeaderText = "کد اتومبیل";
            dataGridViewX1.Columns[6].HeaderText = "نام اتومبیل";
            dataGridViewX1.Columns[7].HeaderText = "قیمت فروش";
            dataGridViewX1.Columns[8].HeaderText = "پرداختی";
            dataGridViewX1.Columns[9].HeaderText = "بدهی";

            string a = "1";
            string str;
            if (Add_Edit == true)
            {
                OleDbCommand sqlCmd = new OleDbCommand("SELECT COUNT(*) FROM customer", con);
                con.Open();
                //label7.Text = "تعداد کالاهای ثبت شده : " + Convert.ToString((int)sqlCmd.ExecuteScalar()) + "";

                str = Convert.ToString((int)sqlCmd.ExecuteScalar());
                int b = Int32.Parse(str) + Int32.Parse(a);
                textBox1.Text = b.ToString();
                con.Close();
            }
        }

        private void buttonX2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttonX4_Click(object sender, EventArgs e)
        {
            OleDbConnection con = new OleDbConnection();
            con.ConnectionString = "provider=microsoft.jet.oledb.4.0; data source=cardb.mdb";
            OleDbCommand com = new OleDbCommand();
            com.CommandText = "update customer set cname='" + textBox2.Text + "',ctell='" + textBox3.Text + "',caddress='" + textBox4.Text + "',date_register='" + txtdate.Text + "',car_id='" + textBox5.Text + "',car_name='" + textBox6.Text + "',sale_price='" + textBox7.Text + "',price_in='" + textBox9.Text + "',debit='" + textBox8.Text + "' where customer_id='" + textBox1.Text + "'";
            con.Open();
            com.Connection = con;
            com.ExecuteNonQuery();
            con.Close();
            empty();
            display();
        }

        private void buttonX3_Click(object sender, EventArgs e)
        {
            int x = Convert.ToInt32(dataGridViewX1.SelectedCells[0].Value);
            cmd.Parameters.Clear();
            cmd.Connection = con;
            cmd.CommandText = "delete from customer where customer_id=@s";
            cmd.Parameters.AddWithValue("@s", x);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            MessageBox.Show("اطلاعات حذف شد");
            display();
            empty();
        }

        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            Bitmap bm = new Bitmap(this.dataGridViewX1.Width, this.dataGridViewX1.Height);
            dataGridViewX1.DrawToBitmap(bm, new Rectangle(0, 0, this.dataGridViewX1.Width, this.dataGridViewX1.Height));
            e.Graphics.DrawImage(bm, 0, 0);
        }

        private void buttonX5_Click(object sender, EventArgs e)
        {
            printDocument1.Print();
        }

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

        private void textBox10_TextChanged(object sender, EventArgs e)
        {
            DataSet ds = new DataSet();
            OleDbDataAdapter adp = new OleDbDataAdapter();
            adp.SelectCommand = new OleDbCommand();
            adp.SelectCommand.Connection = con;
            adp.SelectCommand.CommandText = "select * from customer where cname like @N";
            adp.SelectCommand.Parameters.AddWithValue("@N", textBox10.Text + "%");
            adp.Fill(ds, "t1");
            dataGridViewX1.DataSource = ds;
            dataGridViewX1.DataMember = "t1";
        }

        private void textBox11_TextChanged(object sender, EventArgs e)
        {
            DataSet ds = new DataSet();
            OleDbDataAdapter adp = new OleDbDataAdapter();
            adp.SelectCommand = new OleDbCommand();
            adp.SelectCommand.Connection = con;
            adp.SelectCommand.CommandText = "select * from customer where customer_id like @N";
            adp.SelectCommand.Parameters.AddWithValue("@N", textBox11.Text + "%");
            adp.Fill(ds, "t1");
            dataGridViewX1.DataSource = ds;
            dataGridViewX1.DataMember = "t1";
        }

        private void dataGridViewX1_MouseUp(object sender, MouseEventArgs e)
        {
            textBox1.Text = dataGridViewX1[0, dataGridViewX1.CurrentRow.Index].Value.ToString();
            textBox2.Text = dataGridViewX1[1, dataGridViewX1.CurrentRow.Index].Value.ToString();
            textBox4.Text = dataGridViewX1[2, dataGridViewX1.CurrentRow.Index].Value.ToString();
            textBox3.Text = dataGridViewX1[3, dataGridViewX1.CurrentRow.Index].Value.ToString();
            txtdate.Text = dataGridViewX1[4, dataGridViewX1.CurrentRow.Index].Value.ToString();
            textBox5.Text = dataGridViewX1[5, dataGridViewX1.CurrentRow.Index].Value.ToString();
            textBox6.Text = dataGridViewX1[6, dataGridViewX1.CurrentRow.Index].Value.ToString();
            textBox7.Text = dataGridViewX1[7, dataGridViewX1.CurrentRow.Index].Value.ToString();
            textBox9.Text = dataGridViewX1[8, dataGridViewX1.CurrentRow.Index].Value.ToString();
            textBox8.Text = dataGridViewX1[9, dataGridViewX1.CurrentRow.Index].Value.ToString();
        }

        private void textBox9_TextChanged(object sender, EventArgs e)
        {
            if (textBox7.Text == "")
            {
                return;
            }
            if (textBox9.Text == "")
            {
                return;
            }
            int sub = 1, a, b;
            a = Convert.ToInt32(textBox7.Text);
            b = Convert.ToInt32(textBox9.Text);
            sub = a - b;
            textBox8.Text = sub.ToString();
        }

        private void textBox5_Click(object sender, EventArgs e)
        {
            Hide();
            add_edit_sale objform = new add_edit_sale();
            objform.ShowDialog();
        }

        private void btndeleteZero_Click(object sender, EventArgs e)
        {
            Hide();
            Form1 f20 = new Form1();
            f20.textBoxX2.Text = textBox1.Text;
            f20.textBoxX3.Text = textBox2.Text;
            f20.label3.Text = textBox6.Text;
            f20.label2.Text = textBox7.Text;
            f20.ShowDialog();
        }


    }
}
