using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Globalization;

namespace Nano_System.DebitCredit
{
    public partial class sanadNumber : Form
    {
        int j = 0;
        public sanadNumber()
        {
            InitializeComponent();
        }

        public string calenFar()
        {
            string strMydate;
            PersianCalendar calFar1 = new PersianCalendar();
            strMydate = calFar1.GetYear(DateTime.Now).ToString() + "/" + calFar1.GetMonth(DateTime.Now).ToString("00") + "/" + calFar1.GetDayOfMonth(DateTime.Now).ToString("00");
            return strMydate;
        }

        private void sanadNumber_Load(object sender, EventArgs e)
        {
            textBox1.Text = calenFar();
            grdFill();
        }

        public void grdFill()
        {
            FirstClass db2 = new FirstClass();
            DataTable dt2 = new DataTable();
            dt2 = db2.SP_dbOut("sp_SanadNumers_SelectAll");
            dataGridView1.DataSource = dt2;
        }

        public void dbIn()
        {
            FirstClass db1 = new FirstClass();
            db1.cmd.Parameters.Add("@SanadDate", SqlDbType.NVarChar).Value = textBox1.Text;
            db1.cmd.Parameters.Add("@ClosBit", SqlDbType.Bit).Value = 0;
            db1.exeCommand("sp_SanadNumers_Insert");
            MessageBox.Show("شماره سند جدید ثبت شد");

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "")
            {
                dbIn();
                grdFill();
            }
            else
            {
                MessageBox.Show("تاریخ سند وارد نشده");
            }
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {

            Int32 selectedCellCount = dataGridView1.GetCellCount(DataGridViewElementStates.Selected);
            if (selectedCellCount > 0)
            {
                if (dataGridView1.AreAllCellsSelected(true))
                {
                    MessageBox.Show("All cells are selected", "Selected Cells");
                }
                else
                {
                    //System.Text.StringBuilder sb =
                    //    new System.Text.StringBuilder();

                    for (int i = 0;
                        i < selectedCellCount; i++)
                    {
                        j = int.Parse(dataGridView1.Rows[int.Parse(dataGridView1.SelectedCells[i].RowIndex.ToString())].Cells[0].Value.ToString());
                        textBox1.Text = (dataGridView1.Rows[int.Parse(dataGridView1.SelectedCells[i].RowIndex.ToString())].Cells[1].Value.ToString());
                    }

                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (j != 0)
            {
                FirstClass db1 = new FirstClass();
                db1.cmd.Parameters.Add("@SanadID", SqlDbType.Int).Value = j;
                db1.exeCommand("sp_SanadNumers_DeleteRow");
                grdFill();
                j = 0;
            }
            else
            {
                MessageBox.Show("لطفا شماره سند را از لیست انتخاب نمائید");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (j != 0)
            {
                FirstClass db1 = new FirstClass();
                db1.cmd.Parameters.Add("@SanadID", SqlDbType.Int).Value = j;
                db1.cmd.Parameters.Add("@SanadDate", SqlDbType.NVarChar).Value = textBox1.Text.Trim();
                db1.exeCommand("sp_SanadNumers_Update");
                grdFill();
                j = 0;
            }
            else
            {
                MessageBox.Show("لطفا شماره سند را از لیست انتخاب نمائید");
            }
        }
    }
}