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

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

        public void empty()
        {
            textBoxX3.Text = "";
            textBoxX1.Text = "";
            textBoxX2.Text = "";
            c1.Text = "";
            date2.Text = "";
            date1.Text = "";
        }

        void display()
        {

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

        private void chek_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 = "سر رسید";
        }

        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)
        {
            cmd.Connection = con;
            cmd.Parameters.Clear();
            cmd.CommandText = "insert into chek(id,mablagh,hesab,vajh,sdor,sarresid)values (@a,@w,@e,@r,@f,@g)";
            cmd.Parameters.AddWithValue("@a", textBoxX3.Text);
            cmd.Parameters.AddWithValue("@w", textBoxX1.Text);
            cmd.Parameters.AddWithValue("@e", c1.Text);
            cmd.Parameters.AddWithValue("@r", textBoxX2.Text);
            cmd.Parameters.AddWithValue("@f", date1.Text);
            cmd.Parameters.AddWithValue("@g", date2.Text);

            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            MessageBox.Show("اطلاعات اضافه شد");
            display();
            empty();
        }

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

        private void buttonX3_Click(object sender, EventArgs e)
        {
            cmd.Parameters.Clear();
            cmd.Connection = con;
            cmd.CommandText = "update chek set mablagh='" + textBoxX1.Text + "',hesab=N'" + c1.Text + "',vajh=N'" + textBoxX2.Text + "',sdor='" + date1.Text + "',sarresid='" + date2.Text + "'  where id=" + textBoxX3.Text;
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            MessageBox.Show("اطلاعات ویرایش شد");
            display();
            empty();
        }

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