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_car : Form
    {
        public add_car()
        {
            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 car";
            adp.Fill(ds, "t1");
            dataGridViewX1.DataSource = ds;
            dataGridViewX1.DataMember = "t1";
        }

        public void empty()
        {
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
            textBox5.Text = "";
            textBox6.Text = "";
            textBox7.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 car (car_id,car_name,tedad,model,buy_price,sale_price,vazeyat_fani,production_date) VALUES ('" + textBox1.Text + "', '" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "','" + textBox7.Text + "','" + txtdate.Text + "')";
            con.Open();
            myCommand.CommandText = query;
            myCommand.Connection = con;
            myCommand.ExecuteNonQuery();
            con.Close();
            display();
            MessageBox.Show("اتومبیل با موفقیت ثبت گردید!");
        }

        private void add_car_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[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 = "تاریخ ثبت";
            //***************************************************************************************************
            OleDbConnection con1 = new OleDbConnection();
            con1.ConnectionString = "provider=microsoft.jet.oledb.4.0; data source=cardb.mdb";
            string a = "1";
            string str;
            if (Add_Edit == true)
            {
                OleDbCommand sqlCmd = new OleDbCommand("SELECT COUNT(*) FROM car", con1);
                con1.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();
                con1.Close();
            }
            //***************************************************************************************************

        }


        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 car where car_id=@s";
            cmd.Parameters.AddWithValue("@s", x);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            MessageBox.Show("اطلاعات حذف شد");
            display();
            empty();
        }

        private void buttonX10_Click(object sender, EventArgs e)
        {

        }

        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 car set car_name='" + textBox2.Text + "',tedad='" + textBox3.Text + "',model='" + textBox4.Text + "',buy_price='" + textBox5.Text + "',sale_price='" + textBox6.Text + "',vazeyat_fani='" + textBox7.Text + "',production_date='" + txtdate.Text + "' where car_id='" + textBox1.Text + "'";
            con.Open();
            com.Connection = con;
            com.ExecuteNonQuery();
            con.Close();
            textBox1.Clear();
            textBox2.Clear();
            textBox3.Clear();
            textBox4.Clear();
            textBox5.Clear();
            textBox6.Clear();
            textBox7.Clear();
            txtdate.Text = "";
            display();
        }

        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 dataGridViewX1_MouseUp(object sender, MouseEventArgs e)
        {


        }

        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 car where car_name like @N";
            adp.SelectCommand.Parameters.AddWithValue("@N", textBox10.Text + "%");
            adp.Fill(ds, "t1");
            dataGridViewX1.DataSource = ds;
            dataGridViewX1.DataMember = "t1";
        }

        private void textBox8_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 car where car_id like @N";
            adp.SelectCommand.Parameters.AddWithValue("@N", textBox1.Text + "%");
            adp.Fill(ds, "t1");
            dataGridViewX1.DataSource = ds;
            dataGridViewX1.DataMember = "t1";
        }

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

        private void buttonX2_Click(object sender, EventArgs e)
        {
            Hide();
            add_edit_sale f20 = new add_edit_sale();
            f20.textBox2.Text = textBox1.Text;
            f20.textBox3.Text = textBox2.Text;
            f20.textBox4.Text = textBox4.Text;
            f20.textBox5.Text = textBox7.Text;
            f20.ShowDialog();
        }
    }
}
