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

namespace Nano_System.Cheeks
{
    public partial class CheckPardakhtani : Form
    {
        public CheckPardakhtani()
        {
            InitializeComponent();
        }

        public void grdFill()
        {

            FirstClass db1 = new FirstClass();
            DataTable dt1 = new DataTable();
            dt1 = db1.dbOut(@"SELECT ChekhaiePardakhTani.CheeckNumber, ChekhaiePardakhTani.BankNameShobeh, Members.Name + N' ' + Members.Sname AS Expr1, 
                            ChekhaiePardakhTani.DatOfCheek, ChekhaiePardakhTani.MabLaghOfChek, ChekhaiePardakhTani.Notes
                            FROM  ChekhaiePardakhTani INNER JOIN Members ON ChekhaiePardakhTani.MemBerID = Members.MemBerID");

            dataGridView1.DataSource = dt1;
        }

        public void cmbFil()
        {

            clsForCombo cmbP = new clsForCombo();
            comboBox1.DataSource = cmbP.dbConnection2(@"SELECT  TOP 100 PERCENT MemBerID, Name + N'  ' + Sname AS Expr1 FROM  Members ORDER BY MemBerID");
            comboBox1.ValueMember = "MemBerID";
            comboBox1.DisplayMember = "Expr1";
        }

        public void dbIns()
        {
            FirstClass db2 = new FirstClass();
            db2.cmd.Parameters.Add("@CheeckNumber", SqlDbType.NVarChar).Value = textBox1.Text.Trim();
            db2.cmd.Parameters.Add("@BankNameShobeh", SqlDbType.NVarChar).Value = textBox2.Text.Trim();
            db2.cmd.Parameters.Add("@MemBerID", SqlDbType.NVarChar).Value = comboBox1.SelectedValue;
            db2.cmd.Parameters.Add("@DatOfCheek", SqlDbType.NVarChar).Value = textBox4.Text.Trim();
            db2.cmd.Parameters.Add("@MabLaghOfChek", SqlDbType.Float).Value = float.Parse(textBox5.Text.Trim());
            db2.cmd.Parameters.Add("@Notes", SqlDbType.NVarChar).Value = textBox11.Text.Trim();
            db2.exeCommand("sp_ChekhaiePardakhTani_Insert");
        }
        public void dbDel()
        {
            if (textBox1.Text != "" && textBox2.Text != "")
            {
                FirstClass db2 = new FirstClass();
                db2.cmd.Parameters.Add("@CheeckNumber", SqlDbType.NVarChar).Value = textBox1.Text.Trim();
                db2.cmd.Parameters.Add("@BankNameShobeh", SqlDbType.NVarChar).Value = textBox2.Text.Trim();
                db2.exeCommand("sp_ChekhaiePardakhTani_DeleteRow");
            }
            else
            {
                MessageBox.Show("مقادیر شماره چک و شعبه بانک باید وارد شوند");
            }
        }
        public void dbEdit()
        {
            FirstClass db2 = new FirstClass();
            db2.cmd.Parameters.Add("@CheeckNumber", SqlDbType.NVarChar).Value = textBox1.Text.Trim();
            db2.cmd.Parameters.Add("@BankNameShobeh", SqlDbType.NVarChar).Value = textBox2.Text.Trim();
            db2.cmd.Parameters.Add("@MemBerID", SqlDbType.NVarChar).Value = comboBox1.SelectedValue;
            db2.cmd.Parameters.Add("@DatOfCheek", SqlDbType.NVarChar).Value = textBox4.Text.Trim();
            db2.cmd.Parameters.Add("@MabLaghOfChek", SqlDbType.Float).Value = float.Parse(textBox5.Text.Trim());
            db2.cmd.Parameters.Add("@Notes", SqlDbType.NVarChar).Value = textBox11.Text.Trim();
            db2.exeCommand("sp_ChekhaiePardakhTani_Update");
        }

        private void CheckDariaftani_Load(object sender, EventArgs e)
        {
            grdFill();
            cmbFil();

        }

        private int txtCheker()
        {
            int MyNum = 0;
            if (textBox1.Text == "" || textBox2.Text == "" || textBox4.Text == "" || textBox5.Text == "" || textBox11.Text == "")
            {
                MyNum = 1;
            }
            return MyNum;
        }
        private void txtClear()
        {
            textBox1.Text = "";
            textBox2.Text = "";
            textBox4.Text = "";
            textBox5.Text = "";
            textBox11.Text = "";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int intTxtCheking = txtCheker();
            if (intTxtCheking == 0)
            {
                dbIns();
                grdFill();
                cmbFil();
                txtClear();
            }
            else
            {
                MessageBox.Show("مقادیر تمامی فیلدها باید وارد شوند");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                dbDel();
                grdFill();
                cmbFil();
                txtClear();
            }
            catch
            {
                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++)
                    {

                        textBox1.Text = (dataGridView1.Rows[int.Parse(dataGridView1.SelectedCells[i].RowIndex.ToString())].Cells[0].Value.ToString());
                        textBox2.Text = (dataGridView1.Rows[int.Parse(dataGridView1.SelectedCells[i].RowIndex.ToString())].Cells[1].Value.ToString());
                        textBox4.Text = (dataGridView1.Rows[int.Parse(dataGridView1.SelectedCells[i].RowIndex.ToString())].Cells[3].Value.ToString());
                        textBox5.Text = (dataGridView1.Rows[int.Parse(dataGridView1.SelectedCells[i].RowIndex.ToString())].Cells[4].Value.ToString());
                        textBox11.Text = (dataGridView1.Rows[int.Parse(dataGridView1.SelectedCells[i].RowIndex.ToString())].Cells[5].Value.ToString());
                    }


                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            int intTxtCheking = txtCheker();
            if (intTxtCheking == 0)
            {
                dbEdit();
                grdFill();
                cmbFil();
                txtClear();
            }
            else
            {
                MessageBox.Show("مقادیر تمامی فیلدها باید وارد شوند");
            }
        }

        
   
    }
}