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 LibraryMembers
{
    public partial class frmBooks : Form
    {
        public frmBooks()
        {
            InitializeComponent();
        }

        public void grdFil()
        {
            clsGrideview grd = new clsGrideview();
            dataGridView1.DataSource = grd.dbConnection(@"SELECT     dbo.Books.BookID, dbo.Books.Title, dbo.Subjects.Subject, dbo.Books.CopyrightYear, dbo.Books.ISBNNumber, dbo.Books.PublisherName, 
                      dbo.Books.WriterN, dbo.Books.PurchasePrice, dbo.Books.Pages, dbo.Books.Notes, dbo.Books.TahYeNo FROM  dbo.Books INNER JOIN dbo.Subjects ON dbo.Books.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()
        {

                int i = 0;
                if (checkBox1.Checked)
                {
                    i = 1;
                }
                else
                {
                    i = 0;
                }
            
                clsAdding insTodb = new clsAdding();
                SqlParameter param1 = new SqlParameter("@geimat", SqlDbType.Money);
                param1.Value = textBox10.Text;
                insTodb.insSql("insert into Books (BookID, 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 + "')");
                grdFil();
                cmbFil();

        }

        private void frmBooks_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'libraryMembersDataSet3.Books' table. You can move, or remove it, as needed.
            //this.booksTableAdapter.Fill(this.libraryMembersDataSet3.Books);
            grdFil();
            cmbFil();
            
        }

        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();
                txtClear();
            }
        }

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

            FirstClass db = new FirstClass();
            db.cmd.Parameters.Add("@BookID", SqlDbType.NVarChar).Value = textBox1.Text.Trim();
            db.cmd.Parameters.Add("@Title", SqlDbType.NVarChar).Value = textBox2.Text.Trim();
            db.cmd.Parameters.Add("@TopicID", SqlDbType.Int).Value = comboBox1.SelectedValue;
            db.cmd.Parameters.Add("@CopyrightYear", SqlDbType.Float).Value = float.Parse(textBox4.Text.Trim());
            db.cmd.Parameters.Add("@ISBNNumber", SqlDbType.NVarChar).Value = textBox5.Text.Trim();
            db.cmd.Parameters.Add("@PublisherName", SqlDbType.NVarChar).Value = textBox9.Text.Trim();
            db.cmd.Parameters.Add("@WriterN", SqlDbType.NVarChar).Value = textBox8.Text.Trim();
            db.cmd.Parameters.Add("@PurchasePrice", SqlDbType.Decimal).Value = decimal.Parse(textBox10.Text.Trim());
            db.cmd.Parameters.Add("@Pages", SqlDbType.SmallInt).Value = int.Parse(textBox7.Text.Trim());
            db.cmd.Parameters.Add("@Notes", SqlDbType.NText).Value = textBox11.Text.Trim();

            db.exeCommand("sp_Books_Update2");
            txtClear();

        }

        protected void txtClear()
        {
            foreach (Control c in this.Controls)
            {
                if (c is TextBox)
                {

                    ((TextBox)c).Clear();

                }
            }
        }

        private void button2_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
            {
                dbUpdate();
                grdFil();
                cmbFil();
                txtClear();
            }
        }

        protected void dbSearch()
        {
            clsGrideview grd = new clsGrideview();
            dataGridView1.DataSource = grd.dbConnection(@"SELECT     dbo.Books.BookID, dbo.Books.Title, dbo.Subjects.Subject, dbo.Books.CopyrightYear, dbo.Books.ISBNNumber, dbo.Books.PublisherName, 
                      dbo.Books.WriterN, dbo.Books.PurchasePrice, dbo.Books.Pages, dbo.Books.Notes, dbo.Books.TahYeNo FROM  dbo.Books INNER JOIN dbo.Subjects ON dbo.Books.TopicID = dbo.Subjects.SubId WHERE     (dbo.Books.Title LIKE N'%" + textBox3.Text.Trim() + "%')");

        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (textBox3.Text == "")
            {
                MessageBox.Show("کد کتاب وارد نشده است");
            }
            else
            {
                dbSearch();
                cmbFil();
                txtClear();
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("کد کتاب وارد نشده است");
            }
            else
            {
                FirstClass db = new FirstClass();
                db.cmd.Parameters.Add("@BookID", SqlDbType.NVarChar).Value = textBox1.Text.Trim();
                db.exeCommand("sp_Books_DeleteRow");
                grdFil();
                cmbFil();
                txtClear();
            }
        }
    }
}