<?php include ('header.php'); ?>

<div class="page-title">
    <div class="title_left">
        <h3>
            <small>Home /</small> Books
        </h3>
    </div>
</div>
<div class="clearfix"></div>

<div class="row">
    <div class="col-md-12 col-sm-12 col-xs-12">
        <div class="x_panel">
            <a href="book_print.php" target="_blank" style="background:none;">
                <button class="btn btn-danger pull-right"><i class="fa fa-print"></i> Print Books List</button>
            </a>
            <a href="print_barcode1.php" target="_blank" style="background:none;">
                <button class="btn btn-danger pull-right"><i class="fa fa-print"></i> Print Books Barcode</button>
            </a>
            <br />
            <br />
            <div class="x_title">
                <h2><i class="fa fa-book"></i> Book List</h2>
                <ul class="nav navbar-right panel_toolbox">
                    <li>
                        <a href="add_book.php" style="background:none;">
                            <button class="btn btn-primary"><i class="fa fa-plus"></i> Add Book</button>
                        </a>
                    </li>
                    <li><a class="collapse-link"><i class="fa fa-chevron-up"></i></a></li>
                    <li><a class="close-link"><i class="fa fa-close"></i></a></li>
                </ul>
                <div class="clearfix"></div>
                <ul class="nav nav-pills">
                    <li role="presentation" class="active"><a href="book.php">All</a></li>
                    <li role="presentation"><a href="new_books.php">New Books</a></li>
                    <li role="presentation"><a href="old_books.php">Old Books</a></li>
                    <li role="presentation"><a href="lost_books.php">Lost Books</a></li>
                    <li role="presentation"><a href="damage_books.php">Damaged Books</a></li>
                    <li role="presentation"><a href="sub_rep.php">Subject for Replacement Books</a></li>
                    <li role="presentation"><a href="hard_bound.php">Hardbound Books</a></li>
                </ul>
                <div class="clearfix"></div>
            </div>

            <div class="x_content">
                <!-- content starts here -->

                <div class="table-responsive">

                    <!-- BULK DELETE FORM -->
                    <form method="post" action="delete_books.php" id="booksForm">

                        <div style="margin-bottom:10px;">
                            <button type="submit"
                                    class="btn btn-danger"
                                    onclick="return confirm('Are you sure you want to delete selected books?');">
                                <i class="fa fa-trash"></i> Delete Selected
                            </button>
                        </div>

                        <table cellpadding="0" cellspacing="0" border="0" class="table table-striped table-bordered" id="example">

                            <thead>
                                <tr>
                                    <th style="width:30px; text-align:center;">
                                        <input type="checkbox" id="select-all">
                                    </th>
                                    <th style="width:100px;">Book Image</th>
                                    <th>Barcode</th>
                                    <th>Title</th>
                                    <th>ISBN</th>
                                    <th>Author/s</th>
                                    <th>Copies</th>
                                    <th>Category</th>
                                    <th>Status</th>
                                    <th>Remarks</th>
                                    <th>Action</th>
                                </tr>
                            </thead>

                            <tbody>
                            <?php
                            include('include/dbcon.php');
                            $result = mysqli_query($con,"SELECT * FROM book ORDER BY book_id DESC") or die (mysqli_error($con));
                            while ($row = mysqli_fetch_array($result)) {
                                $id = $row['book_id'];
                                $category_id = $row['category_id'];

                                $cat_query = mysqli_query($con,"SELECT * FROM category WHERE category_id = '$category_id'") or die(mysqli_error($con));
                                $cat_row = mysqli_fetch_array($cat_query);
                            ?>
                                <tr>
                                    <!-- CHECKBOX FOR BULK DELETE -->
                                    <td style="text-align:center;">
                                        <input type="checkbox" class="book-checkbox" name="book_ids[]" value="<?php echo $id; ?>">
                                    </td>

                                    <td>
                                        <?php if($row['book_image'] != ""): ?>
                                            <img src="upload/<?php echo $row['book_image']; ?>" class="img-thumbnail" width="75px" height="50px">
                                        <?php else: ?>
                                            <img src="images/book_image.jpg" class="img-thumbnail" width="75px" height="50px">
                                        <?php endif; ?>
                                    </td>

                                    <td>
                                        <a target="_blank" href="print_barcode_individual1.php?code=<?php echo $row['book_barcode']; ?>">
                                            <?php echo $row['book_barcode']; ?>
                                        </a>
                                    </td>

                                    <td style="word-wrap: break-word; width: 10em;"><?php echo $row['book_title']; ?></td>
                                    <td style="word-wrap: break-word; width: 10em;"><?php echo $row['isbn']; ?></td>
                                    <td style="word-wrap: break-word; width: 10em;">
                                        <?php
                                            echo $row['author']."<br />"
                                                .$row['author_2']."<br />"
                                                .$row['author_3']."<br />"
                                                .$row['author_4']."<br />"
                                                .$row['author_5'];
                                        ?>
                                    </td>
                                    <td><?php echo $row['book_copies']; ?></td>
                                    <td><?php echo $cat_row['classname']; ?></td>
                                    <td><?php echo $row['status']; ?></td>
                                    <td><?php echo $row['remarks']; ?></td>
                                    <td>
                                        <a class="btn btn-primary" href="view_book.php?book_id=<?php echo $id; ?>">
                                            <i class="fa fa-search"></i>
                                        </a>
                                        <a class="btn btn-warning" href="edit_book.php?book_id=<?php echo $id; ?>">
                                            <i class="fa fa-edit"></i>
                                        </a>
                                        <!-- SINGLE DELETE BUTTON -->
                                        <a class="btn btn-danger"
                                           href="delete_books.php?book_id=<?php echo $id; ?>"
                                           onclick="return confirm('Are you sure you want to delete this book?');">
                                            <i class="glyphicon glyphicon-trash icon-white"></i>
                                        </a>
                                    </td>
                                </tr>
                            <?php } ?>
                            </tbody>

                        </table>
                    </form>
                </div>

                <!-- content ends here -->
            </div>
        </div>
    </div>
</div>

<script>
// Select/Deselect all checkboxes
document.addEventListener('DOMContentLoaded', function () {
    var selectAll = document.getElementById('select-all');
    if (selectAll) {
        selectAll.addEventListener('change', function () {
            var checked = this.checked;
            var boxes = document.querySelectorAll('.book-checkbox');
            boxes.forEach(function (cb) {
                cb.checked = checked;
            });
        });
    }
});
</script>

<?php include ('footer.php'); ?>