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

namespace Library
{
    public partial class frmBookList : Form
    {
        public frmBookList()
        {
            InitializeComponent();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        
        private void frmBookList_Load(object sender, EventArgs e)
        {
            dataGridView1.AutoGenerateColumns = false;

            category cat = new category();
            DataTable dt = cat.GetList();
            cbcategory.DisplayMember = "catname";
            cbcategory.ValueMember = "catid";
            cbcategory.DataSource = dt;

          
        }

        private void cbcategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            int catid = Int32.Parse(cbcategory.SelectedValue.ToString());
            Book b = new Book();
            b.CategoryID = catid;
            DataTable dt = b.GetListByCategory();
            dataGridView1.DataSource = dt;   
        }

        private void btnshow_Click(object sender, EventArgs e)
        {
         
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //1-
            string val = dataGridView1[0, dataGridView1.CurrentRow.Index].Value.ToString();
            //2- 
            Book b = new Book();
            b.ID = val;
            b.GetOne();
            //3- 
            frmBookEdit fe = new frmBookEdit();
            //4-
            fe.txtid.Text = b.ID;
            fe.txtname.Text = b.Name;
            fe.txtauthor.Text = b.Author;
            fe.txtyear.Text = b.Year.ToString();
            fe.txttranslator.Text = b.Translator;
            fe.txtisbn.Text = b.ISBN;
            fe.txtcount.Text = b.Count.ToString() ;

            category cat = new category();
            DataTable dtcat = cat.GetList();
            fe.cbcategory.DisplayMember = "catname";
            fe.cbcategory.ValueMember = "catid";
            fe.cbcategory.DataSource = dtcat;
            fe.cbcategory.SelectedValue = b.CategoryID;

            Publisher pub = new Publisher();
            DataTable dtpub = pub.GetList();
            fe.cbpublisher.DisplayMember = "pname";
            fe.cbpublisher.ValueMember = "pid";
            fe.cbpublisher.DataSource = dtpub;
            fe.cbpublisher.SelectedValue = b.PublisherID;
            
            
            fe.ShowDialog();

            cbcategory_SelectedIndexChanged(null, null);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            new frmBookReport().ShowDialog();
        }
    }
}