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 froosh : Form
    {
        public froosh()
        {
            InitializeComponent();
        }

        public bool Add_Edit = true;


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

        void display()
        {

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

        public void empty()
        {
            textBoxX1.Text = "";
            textBoxX2.Text = "";
            textBoxX5.Text = "";
            textBoxX4.Text = "";
            textBoxX6.Text = "";
            textBoxX7.Text = "";
            textBoxX8.Text = "";
            textBoxX9.Text = "";
            textBoxX10.Text = "";
            date1.Text = "";
        }

        private void froosh_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 = "توضیحات";
            dataGridViewX1.Columns[7].HeaderText = "تاریخ";
            dataGridViewX1.Columns[8].HeaderText = "نام مشتری";
            dataGridViewX1.Columns[9].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 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 factor_froosh(id_mahsol,name_mahsol,geymat_f,tedad,geymat_kol,id_factor,tozih,tarikh,name_moshtari,tell)values (@q,@w,@e,@r,@a,@s,@h,@v,@j,@m)";
                cmd.Parameters.AddWithValue("@q", textBoxX6.Text);
                cmd.Parameters.AddWithValue("@w", textBoxX7.Text);
                cmd.Parameters.AddWithValue("@e", textBoxX8.Text);
                cmd.Parameters.AddWithValue("@r", textBoxX9.Text);
                cmd.Parameters.AddWithValue("@a", textBoxX10.Text);
                cmd.Parameters.AddWithValue("@s", textBoxX1.Text);
                cmd.Parameters.AddWithValue("@h", textBoxX2.Text);
                cmd.Parameters.AddWithValue("@v", date1.Text);
                cmd.Parameters.AddWithValue("@j", textBoxX4.Text);
                cmd.Parameters.AddWithValue("@m", textBoxX5.Text);

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

        private void buttonX3_Click(object sender, EventArgs e)
        {
            try
            {
                cmd.Parameters.Clear();
                cmd.Connection = con;
                cmd.CommandText = "update factor_froosh set id_mahsol='" + textBoxX6.Text + "',name_mahsol=N'" + textBoxX7.Text + "',geymat_f='" + textBoxX8.Text + "',tedad='" + textBoxX9.Text + "',geymat_kol='" + textBoxX10.Text + "',tozih=N'" + textBoxX2.Text + "',tarikh='" + date1.Text + "',name_moshtari=N'" + textBoxX4.Text + "',tell='" + textBoxX5.Text + "'  where id_factor=" + textBoxX1.Text;
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
                MessageBox.Show("اطلاعات ویرایش شد");
                display();
                empty();
            }
            catch (Exception)
            {
                MessageBox.Show(" سطری انتخاب نشده است");
            }
            con.Close();
        }

        private void textBoxX9_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (textBoxX8.Text == "")
                {
                    return;
                }
                if (textBoxX9.Text == "")
                {
                    return;
                }
                int mull, a, b;
                a = Convert.ToInt32(textBoxX8.Text);
                b = Convert.ToInt32(textBoxX9.Text);
                mull = a * b;
                textBoxX10.Text = mull.ToString();
            }
            catch (Exception)
            {
                MessageBox.Show(" مشکلی در محاسبات پیش آمده است");
            }
            con.Close();
        }

        private void textBoxX3_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 factor_froosh where name_mahsol like @N";
                adp.SelectCommand.Parameters.AddWithValue("@N", textBoxX3.Text + "%");
                adp.Fill(ds, "t1");
                dataGridViewX1.DataSource = ds;
                dataGridViewX1.DataMember = "t1";
            }
            catch { }
        }

        private void buttonX4_Click(object sender, EventArgs e)
        {
            Hide();
            Form frm = new mahsolat();
            frm.ShowDialog();
        }

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

        private void dataGridViewX1_MouseUp(object sender, MouseEventArgs e)
        {

        }

        private void buttonX5_Click(object sender, EventArgs e)
        {
            Form frm = new froosh_rpt();
            frm.ShowDialog();
        }

        private void dataGridViewX1_MouseUp_1(object sender, MouseEventArgs e)
        {
            textBoxX6.Text = dataGridViewX1[0, dataGridViewX1.CurrentRow.Index].Value.ToString();
            textBoxX7.Text = dataGridViewX1[1, dataGridViewX1.CurrentRow.Index].Value.ToString();
            textBoxX8.Text = dataGridViewX1[2, dataGridViewX1.CurrentRow.Index].Value.ToString();
            textBoxX9.Text = dataGridViewX1[3, dataGridViewX1.CurrentRow.Index].Value.ToString();
            textBoxX10.Text = dataGridViewX1[4, dataGridViewX1.CurrentRow.Index].Value.ToString();
            textBoxX1.Text = dataGridViewX1[5, dataGridViewX1.CurrentRow.Index].Value.ToString();
            textBoxX2.Text = dataGridViewX1[6, dataGridViewX1.CurrentRow.Index].Value.ToString();
            date1.Text = dataGridViewX1[7, dataGridViewX1.CurrentRow.Index].Value.ToString();
            textBoxX4.Text = dataGridViewX1[8, dataGridViewX1.CurrentRow.Index].Value.ToString();
            textBoxX5.Text = dataGridViewX1[9, dataGridViewX1.CurrentRow.Index].Value.ToString();
        }
    }
}
