private void btnReturn_Click(object sender, EventArgs e)
        {
            //update book status
            book.BookStatus = 1;
            int i = EntityBroker.updateBook(book);

            if (i == 1)
            {
                //transaction
                transaction.ReturnDate   = DateTime.Today.Date;
                transaction.ChargeAmount = (decimal)charge;
                int j = EntityBroker.updateTransaction(transaction);
                if (j != 1)
                {
                    MessageBox.Show("Error Saving in Book Transaction Update. Please Contact to Programmer");
                }
            }
            else
            {
                MessageBox.Show("Error Saving in Book Status Change. Please Contact to Programmer");
            }

            ucListBooks booklist = new ucListBooks();

            booklist.setMainWindowRefrence(MainWindowObject);
            MainWindowObject.RequestContentChange(booklist);

            this.Close();
        }
        private void btnAddNew_Click(object sender, EventArgs e)
        {
            Book book = new Book();

            book.BookModelID = book_model_id;
            book.BookStatus  = 1;
            int i = EntityBroker.createNewBook(book);

            if (i == 0)
            {
                return;
            }
            //refreshing book list
            //MessageBox.Show(book_model_id.ToString());
            ucDetailsBook ucdb = new ucDetailsBook(book_model_id);

            ucdb.setMainWindowRefrence(MainWindowObject);
            ucdb.setParentWindowRefrence(ParentWindowObject);
            ParentWindowObject.RequestContentChange(ucdb);

            //refreshing min window list
            ucListBooks booklist = new ucListBooks();

            booklist.setMainWindowRefrence(MainWindowObject);
            MainWindowObject.RequestContentChange(booklist);
        }
        public ucDetialsMember(int _member_id)
        {
            InitializeComponent();

            member_id = _member_id;
            entity    = new LibraryDBEntities();

            member = EntityBroker.getMemberByMemberID(_member_id);
        }
        public ucDetailsBook(int _book_model_id)
        {
            InitializeComponent();

            book_model_id = _book_model_id;
            entity        = new LibraryDBEntities();
            dt            = new DataTable();

            dt.Columns.Add("Book ID", typeof(int));
            dt.Columns.Add("Status", typeof(string));
            dt.Columns.Add("Lend Date", typeof(string));
            dt.Columns.Add("Due Date", typeof(string));

            bookmodel = EntityBroker.getBookModelByID(_book_model_id);
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            int i = 0;

            if (txtMemberName.Text == "")
            {
                MessageBox.Show("Member Name shouldn't be black.");
                return;
            }

            if (member_id > 0)
            {
                member.MemberName  = txtMemberName.Text;
                member.Address     = txtAddress.Text;
                member.PhoneNumber = txtPhoneNumber.Text;
                member.NIRC        = txtNIRCNumber.Text;
                member.Email       = txtEmail.Text;

                i = EntityBroker.updateMember(member);
            }
            else
            {
                Member mb = new Member();
                mb.MemberName  = txtMemberName.Text;
                mb.Address     = txtAddress.Text;
                mb.PhoneNumber = txtPhoneNumber.Text;
                mb.NIRC        = txtNIRCNumber.Text;
                mb.Email       = txtEmail.Text;

                i = EntityBroker.createMember(mb);
            }


            if (i == 1)
            {
                MessageBox.Show("Member Information Saved");
                //#TODO::
                //ucListMember ml = new ucListMember();
                //ml.setMainWindowRefrence(MainWindowObject);
                //MainWindowObject.RequestContentChange(ml);

                ParentWindowObject.Close();
            }
            else
            {
                MessageBox.Show("Error in updating. Error code :: {0}", i.ToString());
            }
        }
        public void fillBookList()
        {
            try
            {
                lblBookTitlePageTitle.Text = bookmodel.BookTitle;
                txtBookTitle.Text          = bookmodel.BookTitle;
                txtBookDescription.Text    = bookmodel.BookDescription;
                txtAuthor.Text             = bookmodel.Author;
                txtBookCategory.Text       = bookmodel.BookCategory;
                txtPublisherName.Text      = bookmodel.PublisherName;
                dtpPublishDate.Value       = bookmodel.PublishDate.Value;
                txtRentalPricePerDay.Text  = bookmodel.RentalPricePerDay.ToString();
                maxAvaiableDaysToRent.Text = bookmodel.MaxAvailableDayToRent.ToString();
            }
            catch { }
            dt.Rows.Clear();

            var books = EntityBroker.getBooksByBookModelID(bookmodel.BookModelId);

            dgvListOfCopies.DataSource = null;
            foreach (Book book in books)
            {
                string lend_date_str = "-";
                string due_date_str  = "-";

                string status = (book.BookStatus == 0) ? "Rented" : (book.BookStatus == 1) ? "Avaiable" : "Not Avaiable";

                LibTran t = EntityBroker.getLastTransationByBookID(book.BookID);

                if (book.BookStatus == 0)
                {
                    lend_date_str = t.LendDate.ToString("dd MM yyyy");
                    DateTime DueDate = t.LendDate.AddDays(Convert.ToDouble(bookmodel.MaxAvailableDayToRent));
                    due_date_str = DueDate.ToString("dd MM yyyy");
                }

                dt.Rows.Add(
                    book.BookID,
                    status,
                    lend_date_str,
                    due_date_str
                    );
            }
        }
        private void btnSearchByBookTitle_Click(object sender, EventArgs e)
        {
            var bookmodels = EntityBroker.getBookModelByTitle(txtSearchByBookTitle.Text);

            dt.Rows.Clear();
            foreach (BooksModel bookmodel in bookmodels)
            {
                dt.Rows.Add(
                    bookmodel.BookTitle,
                    EntityBroker.getNumberOfBooksInLibraryByBookModelID(bookmodel.BookModelId),
                    EntityBroker.getNumberOfAvaiableBooksToRentInLibraryByBookModelID(bookmodel.BookModelId),
                    bookmodel.Author,
                    bookmodel.BookCategory,
                    bookmodel.MaxAvailableDayToRent,
                    bookmodel.BookModelId
                    );
            }

            dgvBooksList.DataSource = dt;
        }
        public Window_Popup_ReturnBook(int _book_id)
        {
            InitializeComponent();
            this.FormBorderStyle = FormBorderStyle.FixedSingle; //make unresizable

            transaction = EntityBroker.getLastTransationByBookID(_book_id);
            book        = EntityBroker.getBookByBookID(_book_id);
            member      = EntityBroker.getMemberByMemberID(transaction.MemberID);
            bookmodel   = EntityBroker.getBookModelByID(book.BookModelID);

            calculateCharge();

            lblBookTitle.Text = bookmodel.BookTitle;

            lblBookIDValue.Text     = book.BookID.ToString();
            lblMemberNameValue.Text = member.MemberName;
            lblMemberIDValue.Text   = member.MemberID.ToString();
            lblRentDateValue.Text   = transaction.LendDate.ToString("dd MM, yyyy");
            lblChargeValue.Text     = charge.ToString();
        }
        private void ucListBooks_Load(object sender, EventArgs e)
        {
            var bookmodels = EntityBroker.getBooksModels();

            dt.Rows.Clear();
            foreach (BooksModel bookmodel in bookmodels)
            {
                dt.Rows.Add(
                    bookmodel.BookTitle,
                    EntityBroker.getNumberOfBooksInLibraryByBookModelID(bookmodel.BookModelId),
                    EntityBroker.getNumberOfAvaiableBooksToRentInLibraryByBookModelID(bookmodel.BookModelId),
                    bookmodel.Author,
                    bookmodel.BookCategory,
                    bookmodel.MaxAvailableDayToRent,
                    bookmodel.BookModelId
                    );
            }

            dgvBooksList.DataSource = dt;
            dgvBooksList.Refresh();
        }
        private void dgvListOfCopies_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridView dgv = sender as DataGridView;

            if (dgv == null)
            {
                return;
            }

            if (dgv.CurrentRow.Selected)
            {
                int _book_id = Convert.ToInt32(dgv.SelectedRows[0].Cells["Book ID"].Value);

                Book _book = EntityBroker.getBookByBookID(_book_id);

                if (_book.BookStatus == 1)
                {
                    try
                    {
                        Window_Popup_LendBook w = new Window_Popup_LendBook(_book_id);
                        w.setMainWindowRefrence(MainWindowObject);
                        w.ShowDialog();
                        winObj.Close();
                    }
                    catch {
                        MessageBox.Show("Please close the window manually");
                    }
                }
                else
                {
                    Window_Popup_ReturnBook w = new Window_Popup_ReturnBook(_book_id);
                    w.setMainWindowRefrence(MainWindowObject);
                    w.ShowDialog();
                    winObj.Close();
                }
            }
        }
        private void btnSearchByBookID_Click(object sender, EventArgs e)
        {
            int _book_id = 0;

            try
            {
                _book_id = Convert.ToInt32(txtSearchByBookID.Text);
            }
            catch {
                MessageBox.Show("Book ID you entered is not valid type.");
                return;
            }

            Book _book = new Book();

            _book = EntityBroker.getBookByBookID(_book_id);

            if (_book == null)
            {
                MessageBox.Show("Book ID you entered is not in the library.");
                return;
            }

            if (_book.BookStatus == 1)
            {
                Window_Popup_LendBook w = new Window_Popup_LendBook(_book_id);
                w.setMainWindowRefrence(MainWindowObject);
                w.ShowDialog();
            }
            else if (_book.BookStatus == 0)
            {
                Window_Popup_ReturnBook w = new Window_Popup_ReturnBook(_book_id);
                w.setMainWindowRefrence(MainWindowObject);
                w.ShowDialog();
            }
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            int i = 0;

            if (txtBookTitle.Text == "")
            {
                MessageBox.Show("Book Title shouldn't be empty.");
                return;
            }
            if (txtRentalPricePerDay.Text == "")
            {
                MessageBox.Show("Rental Price shouldn't be empty.");
                return;
            }
            if (maxAvaiableDaysToRent.Text == "")
            {
                MessageBox.Show("Max Avaiable Days To Rent shouldn't be empty.");
                return;
            }

            if (book_model_id > 0)
            {
                bookmodel.BookTitle             = lblBookTitlePageTitle.Text;
                bookmodel.BookTitle             = txtBookTitle.Text;
                bookmodel.BookDescription       = (string)txtBookDescription.Text;
                bookmodel.Author                = txtAuthor.Text;
                bookmodel.BookCategory          = txtBookCategory.Text;
                bookmodel.PublisherName         = txtPublisherName.Text;
                bookmodel.PublishDate           = dtpPublishDate.Value;
                bookmodel.RentalPricePerDay     = (decimal)Convert.ToDouble(txtRentalPricePerDay.Text);
                bookmodel.MaxAvailableDayToRent = (short)Convert.ToInt32(maxAvaiableDaysToRent.Text);

                i = EntityBroker.updateBookModel(bookmodel);
            }
            else
            {
                BooksModel bm = new BooksModel();

                bm.BookTitle             = (string)txtBookTitle.Text;
                bm.BookDescription       = (string)txtBookDescription.Text;
                bm.Author                = (string)txtAuthor.Text;
                bm.BookCategory          = (string)txtBookCategory.Text;
                bm.PublisherName         = (string)txtPublisherName.Text;
                bm.PublishDate           = dtpPublishDate.Value;
                bm.RentalPricePerDay     = (decimal)Convert.ToDouble(txtRentalPricePerDay.Text);
                bm.MaxAvailableDayToRent = (short)Convert.ToDouble(maxAvaiableDaysToRent.Text);

                i = EntityBroker.createNewBookModel(bm);
            }


            if (i == 1)
            {
                MessageBox.Show("Book Information Saved");

                ucListBooks booklist = new ucListBooks();
                booklist.setMainWindowRefrence(MainWindowObject);
                MainWindowObject.RequestContentChange(booklist);

                ParentWindowObject.Close();
            }
            else
            {
                MessageBox.Show("Error in updating. Error code :: {0}", i.ToString());
            }
        }
        private void btnRent_Click(object sender, EventArgs e)
        {
            //#TODO::ExceptionHandle
            LibraryDBEntities entity = new LibraryDBEntities();
            LibTran           t      = new LibTran();

            int member_id = 0;
            int book_id   = 0;

            try
            {
                book_id = Convert.ToInt32(txtBookID.Text);
            }
            catch
            {
                MessageBox.Show("BookID you entered is not valid type.");
                return;
            }

            try
            {
                member_id = Convert.ToInt32(txtMemberID.Text);
            }
            catch
            {
                MessageBox.Show("MemberID you entered is not valid type.");
                return;
            }

            Book book = EntityBroker.getBookByBookID(book_id);

            if (book == null)
            {
                MessageBox.Show("BookID doesn't exit.");
                return;
            }
            if (book.BookStatus != 1)
            {
                MessageBox.Show("Book is not avaiable to rent.");
                return;
            }

            Member member;

            member = EntityBroker.getMemberByMemberID(member_id);
            if (member == null)
            {
                MessageBox.Show("MemberID doesn't exit.");
                return;
            }

            t.MemberID = member_id;
            t.BookID   = book_id;
            t.LendDate = DateTime.Now;

            entity.AddToLibTrans(t);
            try
            {
                entity.SaveChanges();
            }catch {
                MessageBox.Show("User not exit");
            }
            Book b = entity.Books.Where(x => x.BookID == t.BookID).SingleOrDefault();

            b.BookStatus = 0;

            int i = entity.SaveChanges();

            if (i == 1)
            {
                try
                {
                    ucListBooks booklist = new ucListBooks();
                    booklist.setMainWindowRefrence(MainWindowObject);
                    MainWindowObject.RequestContentChange(booklist);
                }
                catch {
                    MessageBox.Show("Exception. Please close the window manually. Sorry for inconvenience");
                }
            }
            this.Close();
        }