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

        public void empty()
        {
            textBoxX1.Text = "";
            textBoxX2.Text = "";
            textBoxX3.Text = "";
            textBoxX4.Text = "";
            txtdate.Text = "";

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

            display();
            dataGridViewX1.Columns[0].HeaderText = "کد فاکتور";
            dataGridViewX1.Columns[1].HeaderText = "تاریخ";
            dataGridViewX1.Columns[2].HeaderText = "کد مشتری";
            dataGridViewX1.Columns[3].HeaderText = "نام مشتری";
            dataGridViewX1.Columns[4].HeaderText = "نام اتومبیل";
            dataGridViewX1.Columns[5].HeaderText = "مبلغ قابل پرداخت";

            string a = "1";
            string str;
            if (Add_Edit == true)
            {

                //SqlConnection con = new SqlConnection(ConnectionString);
                OleDbCommand sqlCmd = new OleDbCommand("SELECT COUNT(*) FROM factor_f", con);
                con.Open();
                str = Convert.ToString((int)sqlCmd.ExecuteScalar());
                int b = Int32.Parse(str) + Int32.Parse(a);
                textBoxX1.Text = b.ToString();
                con.Close();
            }
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            cmd.Connection = con;
            cmd.Parameters.Clear();
            cmd.CommandText = "insert into factor_f(code_f_kh,tarekh,customer_id,cname,car_name,sale_price)values (@q,@w,@e,@r,@k,@t)";
            cmd.Parameters.AddWithValue("@q", textBoxX1.Text);
            cmd.Parameters.AddWithValue("@w", txtdate.Text);
            cmd.Parameters.AddWithValue("@e", textBoxX2.Text);
            cmd.Parameters.AddWithValue("@r", textBoxX3.Text);
            cmd.Parameters.AddWithValue("@k", label3.Text);
            cmd.Parameters.AddWithValue("@t", label2.Text);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            MessageBox.Show("اطلاعات اضافه شد");
            display();
            empty();
        }

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

        private void textBoxX2_TextChanged(object sender, EventArgs e)
        {

        }

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

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

        private void dataGridViewX1_MouseUp(object sender, MouseEventArgs e)
        {
            textBoxX1.Text = dataGridViewX1[0, dataGridViewX1.CurrentRow.Index].Value.ToString();
            txtdate.Text = dataGridViewX1[1, dataGridViewX1.CurrentRow.Index].Value.ToString();
            textBoxX2.Text = dataGridViewX1[2, dataGridViewX1.CurrentRow.Index].Value.ToString();
            textBoxX3.Text = dataGridViewX1[3, dataGridViewX1.CurrentRow.Index].Value.ToString();
            label3.Text = dataGridViewX1[4, dataGridViewX1.CurrentRow.Index].Value.ToString();
            label2.Text = dataGridViewX1[5, dataGridViewX1.CurrentRow.Index].Value.ToString();
        }


    }
}
