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

namespace lavazem_behdashti
{
    public partial class moshtari : Form
    {
        public moshtari()
        {
            InitializeComponent();
        }

        SqlConnection con = new SqlConnection("Data Source=(local);Initial Catalog=lavazem behdashti;Integrated Security=True");
        SqlCommand cmd = new SqlCommand();
        public bool Add_Edit = true;

        void display()
        {

            DataSet ds = new DataSet();
            SqlDataAdapter adp = new SqlDataAdapter();
            adp.SelectCommand = new SqlCommand();
            adp.SelectCommand.Connection = con;
            adp.SelectCommand.CommandText = "select * from moshtari1";
            adp.Fill(ds, "t1");
            dataGridViewX1.DataSource = ds;
            dataGridViewX1.DataMember = "t1";
        }

        public void empty()
        {
            textBoxX1.Text = "";
            textBoxX2.Text = "";
            textBoxX3.Text = "";
            textBoxX5.Text = "";
            textBoxX4.Text = "";
            textBoxX13.Text = "";
        }


        private void moshtari_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 = " تاریخ خرید";
            dataGridViewX1.Columns[6].HeaderText = " تاریخ پرداخت";
        }

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

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

        private void buttonX1_Click(object sender, EventArgs e)
        {
            if (textBoxX1.Text == "")
            {
                MessageBox.Show("کد مشتری وارد نشده است");
                textBoxX1.Focus();
                return;
            }

            try
            {
                cmd.Connection = con;
                cmd.Parameters.Clear();
                cmd.CommandText = "insert into moshtari1(id_moshtari,name_moshtari,tell,address,bedehi,tarikh_kharid,tarikh)values (@q,@w,@e,@r,@a,@s,@d)";
                cmd.Parameters.AddWithValue("@q", textBoxX1.Text);
                cmd.Parameters.AddWithValue("@w", textBoxX2.Text);
                cmd.Parameters.AddWithValue("@e", textBoxX3.Text);
                cmd.Parameters.AddWithValue("@r", textBoxX5.Text);
                cmd.Parameters.AddWithValue("@a", textBoxX4.Text);
                cmd.Parameters.AddWithValue("@s", date1.Text);
                cmd.Parameters.AddWithValue("@d", date2.Text);

                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
                MessageBox.Show("اطلاعات اضافه شد");
                display();
                //textBoxX1.Text = anbar1TableAdapter.getmaxcode().ToString();
                empty();
            }
            catch (Exception)
            {
                MessageBox.Show(" کد تکراری وارد شده است لطفا کد دیگری را وارد کنید");
            }
            con.Close();
        }

        private void buttonX2_Click(object sender, EventArgs e)
        {
            try
            {
                int x = Convert.ToInt32(dataGridViewX1.SelectedCells[0].Value);
                cmd.Parameters.Clear();
                cmd.Connection = con;
                cmd.CommandText = "delete from moshtari1 where id_moshtari=@s";
                cmd.Parameters.AddWithValue("@s", x);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
                MessageBox.Show("اطلاعات حذف شد");
                display();
                empty();
            }
            catch (Exception)
            {
                MessageBox.Show(" سطری انتخاب نشده است");
            }
            con.Close();
        }

        private void buttonX3_Click(object sender, EventArgs e)
        {
            try
            {
                cmd.Parameters.Clear();
                cmd.Connection = con;
                cmd.CommandText = "update moshtari1 set name_moshtari=N'" + textBoxX2.Text + "',tell='" + textBoxX3.Text + "',address=N'" + textBoxX5.Text + "',bedehi='" + textBoxX4.Text + "',tarikh='" + date1.Text + "',tarikh_kharid='" + date2.Text + "'  where id_moshtari=" + textBoxX1.Text;
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
                MessageBox.Show("اطلاعات ویرایش شد");
                display();
                empty();
            }
            catch (Exception)
            {
                MessageBox.Show(" سطری انتخاب نشده است");
            }
            con.Close();
        }

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

        private void textBoxX13_TextChanged(object sender, EventArgs e)
        {
            try
            {
                DataSet ds = new DataSet();
                SqlDataAdapter adp = new SqlDataAdapter();
                adp.SelectCommand = new SqlCommand();
                adp.SelectCommand.Connection = con;
                adp.SelectCommand.CommandText = "select * from moshtari1 where name_moshtari like @N";
                adp.SelectCommand.Parameters.AddWithValue("@N", textBoxX13.Text + "%");
                adp.Fill(ds, "t1");
                dataGridViewX1.DataSource = ds;
                dataGridViewX1.DataMember = "t1";
            }
            catch
            { }
        }
    }
}
