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

namespace Nano_System
{
    public partial class frmAghlam : Form
    {
        string facCode = "";
        public frmAghlam()
        {
            InitializeComponent();
        }

        public void grdFil()
        {
            clsGrideview grd = new clsGrideview();
            dataGridView1.DataSource = grd.dbConnection(@"SELECT     dbo.Goods.GoodsID, dbo.Goods.Title, dbo.Subjects.Subject, dbo.Goods.CopyrightYear, dbo.Goods.ISBNNumber, dbo.Goods.PublisherName, 
                      dbo.Goods.WriterN, dbo.Goods.PurchasePrice, dbo.Goods.Pages, dbo.Goods.Notes, dbo.Goods.TahYeNo
                        FROM         dbo.Goods INNER JOIN
                      dbo.Subjects ON dbo.Goods.TopicID = dbo.Subjects.SubId");

        }

        public void cmbFil()
        {
            
            clsForCombo cmbP = new clsForCombo();
            comboBox1.DataSource = cmbP.dbConnection2(@"SELECT TOP 100 PERCENT SubId, Subject FROM Subjects ORDER BY Subject");
            comboBox1.ValueMember = "SubId";
            comboBox1.DisplayMember = "Subject";
        }

        public void insSql()
        {

                bool  i = false ;
                if (checkBox1.Checked)
                {
                    i = true ;
                }
                else
                {
                    i = false ;
                }
            
            
            
                //clsAdding insTodb = new clsAdding();
                //SqlParameter param1 = new SqlParameter("@geimat", SqlDbType.Money);
                //param1.Value = textBox10.Text;
                //insTodb.insSql("insert into Goods (GoodsID, Title, TopicID, CopyrightYear, ISBNNumber, PublisherName, WriterN, PurchasePrice, Pages, Notes, TahYeNo)values('" + textBox1.Text + "' , '" + textBox2.Text + "' , '" + comboBox1.SelectedValue + "' , '" + textBox4.Text + "' , '" + textBox5.Text + "' , '" + textBox9.Text + "' , '" + textBox8.Text + "' , '" + Convert.ToDecimal(textBox10.Text) + "' , '" + textBox7.Text + "' , '" + textBox11.Text + "', '" + i + "')");
            try{
                Lsc_DarookhaneDataContext db = new Lsc_DarookhaneDataContext();
                Good Add = new Good();
            Add.GoodsID=textBox1.Text;
            Add.Notes=textBox11.Text ;
            Add.PublisherName=textBox9.Text ;
            Add.PurchasePrice=decimal.Parse(textBox10 .Text) ;
            Add.TahYeNo=i;
            Add.Title=textBox2.Text ;
            Add.TopicID=int.Parse(comboBox1.SelectedValue.ToString() );
            Add.WriterN=textBox8.Text ;
            db.Goods.InsertOnSubmit(Add);
            db.SubmitChanges();
            grdFil();
            cmbFil();
            }
            catch(Exception ex)
            {
                MessageBox.Show("عملیات درج انجام نشد");
            }
           
        }


        private void button1_Click(object sender, EventArgs e)
        {

            if (textBox1.Text == "" || textBox2.Text == "" || textBox4.Text == "" || textBox5.Text == "" || textBox11.Text == "" || textBox7.Text == "" || textBox8.Text == "" || textBox9.Text == "" || textBox10.Text == "" || comboBox1.Text == "")
            {
                MessageBox.Show("مقادیر تمامی فیلدها بایستی انتخاب گردند");
            }
            else
            {
                insSql();
                grdFil();
                cmbFil();
                string facCode = "";
                MessageBox.Show("عملیات با موفقیت انجام شد");
            }
        }

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

        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++)
                    {

                        facCode = (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[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());
                        textBox9.Text = (dataGridView1.Rows[int.Parse(dataGridView1.SelectedCells[i].RowIndex.ToString())].Cells[5].Value.ToString());
                        textBox10.Text = (dataGridView1.Rows[int.Parse(dataGridView1.SelectedCells[i].RowIndex.ToString())].Cells[7].Value.ToString());
                        textBox8.Text = (dataGridView1.Rows[int.Parse(dataGridView1.SelectedCells[i].RowIndex.ToString())].Cells[6].Value.ToString());
                        textBox7.Text = (dataGridView1.Rows[int.Parse(dataGridView1.SelectedCells[i].RowIndex.ToString())].Cells[8].Value.ToString());
                        checkBox1.Checked = bool.Parse(dataGridView1.Rows[int.Parse(dataGridView1.SelectedCells[i].RowIndex.ToString())].Cells[10].Value.ToString());
                        textBox11.Text = (dataGridView1.Rows[int.Parse(dataGridView1.SelectedCells[i].RowIndex.ToString())].Cells[9].Value.ToString());

                    }

                }
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {

                if (textBox1.Text != "")
                {
                    FirstClass dbDel = new FirstClass();
                    dbDel.cmd.Parameters.Add("@GoodsID", SqlDbType.NVarChar).Value = textBox1.Text.Trim();
                    dbDel.exeCommand("sp_Goods_DeleteRow");
                    grdFil();
                    cmbFil();
                    string facCode = "";
                    MessageBox.Show("عملیات با موفقیت انجام شد");
                }
                else
                {
                    MessageBox.Show("کد جنس وارد نشده است");
                }
            }
            catch
            {
                MessageBox.Show("کد جنس وارد نشده است");
            }
        }

        private int txtNool()
        {
            if (textBox1.Text == "" || textBox10.Text == "" || textBox11.Text == "" || textBox2.Text == "" || textBox4.Text == "" || textBox5.Text == "" || textBox7.Text == "" || textBox8.Text == "" || textBox9.Text == "")
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            int txtChk = txtNool() , i = 0;
                           
                if (checkBox1.Checked)
                {
                    i = 1;
                }
                else
                {
                    i = 0;
                }
            
            if (txtChk == 0)
            {
                FirstClass db1 = new FirstClass();
                db1.cmd.Parameters.Add("@GoodsID", SqlDbType.NVarChar).Value = textBox1.Text.Trim();
                db1.cmd.Parameters.Add("@Title", SqlDbType.NVarChar).Value = textBox2.Text.Trim();
                db1.cmd.Parameters.Add("@TopicID", SqlDbType.Int).Value = int.Parse(comboBox1.SelectedValue.ToString());
                db1.cmd.Parameters.Add("@CopyrightYear", SqlDbType.Float).Value = textBox4.Text.Trim();
                db1.cmd.Parameters.Add("@ISBNNumber", SqlDbType.NVarChar).Value = textBox5.Text.Trim();
                db1.cmd.Parameters.Add("@PublisherName", SqlDbType.NVarChar).Value = textBox9.Text.Trim();
                db1.cmd.Parameters.Add("@WriterN", SqlDbType.NVarChar).Value = textBox8.Text.Trim();
                db1.cmd.Parameters.Add("@PurchasePrice", SqlDbType.Decimal).Value = decimal.Parse(textBox10.Text.Trim());
                db1.cmd.Parameters.Add("@Pages", SqlDbType.SmallInt).Value =  Convert.ToInt16(textBox7.Text.Trim());
                db1.cmd.Parameters.Add("@Notes", SqlDbType.NText).Value = textBox11.Text.Trim();
                db1.cmd.Parameters.Add("@TahYeNo", SqlDbType.Bit).Value = i;
                db1.exeCommand("sp_Goods_Update");
                grdFil();
                cmbFil();
                string facCode = "";
                MessageBox.Show("عملیات با موفقیت انجام شد");
            }
            else
            {
                MessageBox.Show("مقادیر تمامی فیلدها باید وارد شوند");
            }
        }




    }
}