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

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

        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 grdFil()
        {
            clsGrideview grd = new clsGrideview();
            dataGridView1.DataSource = grd.dbConnection(@"SELECT     TOP 100 PERCENT dbo.FactForooshID.FactorForushID, dbo.FactForooshID.DtOfSodoor, dbo.Members.Name + N'  ' + dbo.Members.Sname AS Expr1, 
                      dbo.FactForooshID.ClosedBit FROM   dbo.Members INNER JOIN  dbo.FactForooshID ON dbo.Members.MemBerID = dbo.FactForooshID.MemBerID ORDER BY dbo.FactForooshID.FactorForushID DESC");

        }

        public void insSql()
        {

            int i = 0;
            if (checkBox1.Checked)
            {
                i = 1;
            }
            else
            {
                i = 0;
            }



            clsAdding insTodb = new clsAdding();
            insTodb.insSql("insert into FactForooshID (DtOfSodoor, MemBerID, ClosedBit, TasfiehBit)values('" + textBox1.Text + "' , '" + comboBox1.SelectedValue + "' , '" + i + "' , 0)");
            grdFil();
            cmbFil();

        }

        public void txtCls()
        {
            textBox1.Clear();
            checkBox1.Checked = false;
        }


        private void frmFactorKahridCode_Load(object sender, EventArgs e)
        {
            grdFil();
            cmbFil();
            txtCls();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "" || comboBox1.Text == "")
            {
                MessageBox.Show("مقادیر تمامی فیلدها بایستی وارد شوند");
            }
            else
            {
                insSql();
                grdFil();
                cmbFil();
                txtCls();
                j = 0;
            }
        }

        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
                {
                    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 dbHazf = new FirstClass();
                dbHazf.cmd.Parameters.Add("@FactorForushID", SqlDbType.Int).Value = j;
                dbHazf.exeCommand("sp_FactForooshID_DeleteRow");
                grdFil();
                MessageBox.Show("شماره فاکتور حذف شد");
                j = 0;
            }
            else
            {
                MessageBox.Show("شماره فاکتور انتخاب نشده");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            int bitVal1;
            if (checkBox1.Checked)
            {
                bitVal1 = 1;
            }
            else
            {
                bitVal1 = 0;
            }
            if (j != 0)
            {
                FirstClass dbEDT = new FirstClass();
                dbEDT.cmd.Parameters.Add("@FactorForushID", SqlDbType.Int).Value = j;
                dbEDT.cmd.Parameters.Add("@DtOfSodoor", SqlDbType.NVarChar).Value = textBox1.Text.Trim();
                dbEDT.cmd.Parameters.Add("@MemBerID", SqlDbType.NVarChar).Value = comboBox1.SelectedValue.ToString();
                dbEDT.cmd.Parameters.Add("@ClosedBit", SqlDbType.Bit).Value = bitVal1;
                dbEDT.exeCommand("sp_FactForooshID2_Update");
                grdFil();
                MessageBox.Show("تغییرات انجام شد");
                j = 0;
            }
            else
            {
                MessageBox.Show("شماره فاکتور انتخاب نشده");
            }
        }
    }
}