using System;
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 frmTahvileketab : Form
    {
        public frmTahvileketab()
        {
            InitializeComponent();
        }

        public void dbConnection()
        {
            clsGrideview dbCon1 = new clsGrideview();
            dataGridView1.DataSource = dbCon1.dbConnection(@"SELECT     dbo.Members.MemBerID, dbo.Members.Name + N'  ' + dbo.Members.Sname AS EXPR1, dbo.Books.Title, dbo.BookH.DateDeli, dbo.Subjects.Subject, 
                      dbo.BookH.zemanat, dbo.BookH.Mablagh
                        FROM         dbo.BookH INNER JOIN
                      dbo.Books ON dbo.BookH.BookID = dbo.Books.BookID INNER JOIN
                      dbo.Members ON dbo.BookH.PersonelID = dbo.Members.MemBerID INNER JOIN
                      dbo.Subjects ON dbo.Books.TopicID = dbo.Subjects.SubId
                        WHERE     (dbo.BookH.DatBaz IS NULL)");
        }

        public void dbConnection2()
        {
            clsForCombo cmbP2 = new clsForCombo();
            comboBox1.DataSource = cmbP2.dbConnection2(@"SELECT     MemBerID FROM    dbo.Members WHERE     (memberState = 1)");
            comboBox2.DataSource = cmbP2.dbConnection2(@"SELECT     BookID, Title FROM   dbo.Books WHERE     (TahYeNo = 0)");
            
            comboBox1.ValueMember = "MemBerID";
            comboBox1.DisplayMember = "MemBerID";

            comboBox2.ValueMember = "BookID";
            comboBox2.DisplayMember = "Title";
        }

        public void adin()
        {
            FirstClass dbEx = new FirstClass();
            dbEx.cmd.Parameters.Add("@PersonelID", SqlDbType.NVarChar).Value = comboBox1.SelectedValue.ToString();
            dbEx.cmd.Parameters.Add("@BookID", SqlDbType.NVarChar).Value = comboBox2.SelectedValue.ToString();
            dbEx.cmd.Parameters.Add("@DateDeli", SqlDbType.NVarChar).Value = textBox2.Text.Trim();
            dbEx.cmd.Parameters.Add("@zemanat", SqlDbType.NVarChar).Value = textBox1.Text.Trim();
            dbEx.cmd.Parameters.Add("@Mablagh", SqlDbType.NVarChar).Value = textBox3.Text.Trim();
            dbEx.exeCommand("sp_BookH_Insert");

            FirstClass dbex1 = new FirstClass();
            dbex1.cmd.Parameters.Add("@BookID", SqlDbType.NVarChar).Value = comboBox2.SelectedValue.ToString();
            dbex1.cmd.Parameters.Add("@TahYeNo", SqlDbType.Bit).Value = 1;
            dbex1.exeCommand("sp_Books_Update");

        }


        private void frmTahvileketab_Load(object sender, EventArgs e)
        {
            dbConnection();
            dbConnection2();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (comboBox1.Text == "" || comboBox2.Text == "" || textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "")
            {
                MessageBox.Show("مقادیر تمامی فیلدها بایستی وارد شوند");
            }
            else
            {
                adin();
                dbConnection();
                dbConnection2();
            }
        }
    }
}