<?php

declare(strict_types=1);

require_once __DIR__ . '/../library/auth.php';

$school_id = (int) $_SESSION['school_id'];

allow_roles(['admin', 'teacher']);

// ======================================================
// FETCH BOOKS
// ======================================================

$stmt = $pdo->prepare("
    SELECT *

    FROM books

    WHERE school_id = ?

    ORDER BY id DESC
");

$stmt->execute([$school_id]);

$books = $stmt->fetchAll(PDO::FETCH_ASSOC);

$page_title = 'Library Books';

require_once __DIR__ . '/../include/header.php';
require_once __DIR__ . '/../include/sidebar.php';

?>

<div class="content-wrapper">

    <!-- ============================================== -->
    <!-- PAGE HEADER -->
    <!-- ============================================== -->

    <section class="content-header">

        <div class="container-fluid">

            <div class="d-flex flex-column flex-md-row justify-content-between align-items-md-center">

                <div class="mb-2 mb-md-0">

                    <h1>

                        <i class="fas fa-book text-primary"></i>

                        Library Books

                    </h1>

                    <small class="text-muted">

                        Manage library inventory

                    </small>

                </div>

                <div>

                    <a
                        href="add_book.php"
                        class="btn btn-primary"
                    >

                        <i class="fas fa-plus"></i>

                        Add Book

                    </a>

                </div>

            </div>

        </div>

    </section>

    <!-- ============================================== -->
    <!-- MAIN CONTENT -->
    <!-- ============================================== -->

    <section class="content">

        <div class="container-fluid">

            <!-- ALERT -->

            <?php if (isset($_GET['msg'])) : ?>

                <div class="alert alert-success alert-dismissible fade show">

                    <?= htmlspecialchars($_GET['msg']) ?>

                    <button
                        type="button"
                        class="close"
                        data-dismiss="alert"
                    >

                        <span>&times;</span>

                    </button>

                </div>

            <?php endif; ?>

            <!-- ========================================== -->
            <!-- STATS -->
            <!-- ========================================== -->

            <div class="row mb-3">

                <!-- TOTAL BOOKS -->

                <div class="col-12 col-md-4">

                    <div class="card shadow-sm border-0">

                        <div class="card-body">

                            <div class="d-flex justify-content-between align-items-center">

                                <div>

                                    <h6 class="text-muted">

                                        Total Books

                                    </h6>

                                    <h3>

                                        <?= count($books) ?>

                                    </h3>

                                </div>

                                <div class="text-primary">

                                    <i class="fas fa-book fa-2x"></i>

                                </div>

                            </div>

                        </div>

                    </div>

                </div>

                <!-- TOTAL STOCK -->

                <div class="col-12 col-md-4">

                    <div class="card shadow-sm border-0">

                        <div class="card-body">

                            <div class="d-flex justify-content-between align-items-center">

                                <div>

                                    <h6 class="text-muted">

                                        Total Quantity

                                    </h6>

                                    <h3>

                                        <?= array_sum(array_column($books, 'qty')) ?>

                                    </h3>

                                </div>

                                <div class="text-success">

                                    <i class="fas fa-layer-group fa-2x"></i>

                                </div>

                            </div>

                        </div>

                    </div>

                </div>

                <!-- OUT OF STOCK -->

                <div class="col-12 col-md-4">

                    <div class="card shadow-sm border-0">

                        <div class="card-body">

                            <div class="d-flex justify-content-between align-items-center">

                                <div>

                                    <h6 class="text-muted">

                                        Out Of Stock

                                    </h6>

                                    <h3>

                                        <?= count(array_filter($books, fn($b) => $b['qty'] <= 0)) ?>

                                    </h3>

                                </div>

                                <div class="text-danger">

                                    <i class="fas fa-exclamation-triangle fa-2x"></i>

                                </div>

                            </div>

                        </div>

                    </div>

                </div>

            </div>

            <!-- ========================================== -->
            <!-- TABLE -->
            <!-- ========================================== -->

            <div class="card card-outline card-primary shadow-sm">

                <div class="card-header">

                    <div class="d-flex flex-column flex-md-row justify-content-between align-items-md-center">

                        <h3 class="card-title mb-2 mb-md-0">

                            <i class="fas fa-table"></i>

                            Books Records

                        </h3>

                        <!-- SEARCH -->

                        <div style="max-width:300px; width:100%;">

                            <input
                                type="text"
                                id="searchInput"
                                class="form-control"
                                placeholder="Search books..."
                            >

                        </div>

                    </div>

                </div>

                <div class="card-body p-0">

                    <!-- DESKTOP TABLE -->

                    <div class="table-responsive d-none d-md-block">

                        <table class="table table-hover mb-0">

                            <thead class="thead-light">

                                <tr>

                                    <th width="70">

                                        #

                                    </th>

                                    <th>

                                        Book Title

                                    </th>

                                    <th>

                                        Author

                                    </th>

                                    <th>

                                        Subject

                                    </th>

                                    <th>

                                        Rack

                                    </th>

                                    <th>

                                        Stock

                                    </th>

                                    <th width="120">

                                        Actions

                                    </th>

                                </tr>

                            </thead>

                            <tbody id="booksTable">

                                <?php if (!empty($books)) : ?>

                                    <?php foreach ($books as $index => $book) : ?>

                                        <tr>

                                            <td>

                                                <?= $index + 1 ?>

                                            </td>

                                            <td>

                                                <strong>

                                                    <?= htmlspecialchars($book['book_title']) ?>

                                                </strong>

                                                <br>

                                                <small class="text-muted">

                                                    <?= htmlspecialchars($book['book_no'] ?: '-') ?>

                                                </small>

                                            </td>

                                            <td>

                                                <?= htmlspecialchars($book['author'] ?: '-') ?>

                                            </td>

                                            <td>

                                                <?= htmlspecialchars($book['subject'] ?: '-') ?>

                                            </td>

                                            <td>

                                                <?= htmlspecialchars($book['rack_no'] ?: '-') ?>

                                            </td>

                                            <td>

                                                <?php if ($book['qty'] > 0) : ?>

                                                    <span class="badge badge-success p-2">

                                                        <?= (int) $book['qty'] ?>

                                                    </span>

                                                <?php else : ?>

                                                    <span class="badge badge-danger p-2">

                                                        Out

                                                    </span>

                                                <?php endif; ?>

                                            </td>

                                            <td>

                                                <a
                                                    href="edit_book.php?id=<?= (int) $book['id'] ?>"
                                                    class="btn btn-sm btn-success"
                                                >

                                                    <i class="fas fa-edit"></i>

                                                </a>

                                            </td>

                                        </tr>

                                    <?php endforeach; ?>

                                <?php else : ?>

                                    <tr>

                                        <td
                                            colspan="7"
                                            class="text-center text-muted p-5"
                                        >

                                            No books found.

                                        </td>

                                    </tr>

                                <?php endif; ?>

                            </tbody>

                        </table>

                    </div>

                    <!-- MOBILE CARDS -->

                    <div class="d-block d-md-none p-3">

                        <?php if (!empty($books)) : ?>

                            <?php foreach ($books as $book) : ?>

                                <div class="card shadow-sm border-0 mb-3">

                                    <div class="card-body">

                                        <div class="d-flex justify-content-between align-items-start">

                                            <div>

                                                <h5 class="mb-1">

                                                    <?= htmlspecialchars($book['book_title']) ?>

                                                </h5>

                                                <small class="text-muted">

                                                    <?= htmlspecialchars($book['author'] ?: '-') ?>

                                                </small>

                                            </div>

                                            <div>

                                                <?php if ($book['qty'] > 0) : ?>

                                                    <span class="badge badge-success">

                                                        <?= (int) $book['qty'] ?>

                                                    </span>

                                                <?php else : ?>

                                                    <span class="badge badge-danger">

                                                        Out

                                                    </span>

                                                <?php endif; ?>

                                            </div>

                                        </div>

                                        <hr>

                                        <div class="mb-2">

                                            <small class="text-muted">

                                                Subject:

                                            </small>

                                            <?= htmlspecialchars($book['subject'] ?: '-') ?>

                                        </div>

                                        <div class="mb-3">

                                            <small class="text-muted">

                                                Rack:

                                            </small>

                                            <?= htmlspecialchars($book['rack_no'] ?: '-') ?>

                                        </div>

                                        <a
                                            href="edit_book.php?id=<?= (int) $book['id'] ?>"
                                            class="btn btn-success btn-sm btn-block"
                                        >

                                            <i class="fas fa-edit"></i>

                                            Edit Book

                                        </a>

                                    </div>

                                </div>

                            <?php endforeach; ?>

                        <?php else : ?>

                            <div class="alert alert-warning text-center">

                                No books found.

                            </div>

                        <?php endif; ?>

                    </div>

                </div>

            </div>

        </div>

    </section>

</div>

<!-- ============================================== -->
<!-- SCRIPTS -->
<!-- ============================================== -->

<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>

<script>

'use strict';

$(document).ready(function () {

    $('#searchInput').on('keyup', function () {

        let value =
            $(this).val().toLowerCase();

        $('#booksTable tr').filter(function () {

            $(this).toggle(

                $(this)
                    .text()
                    .toLowerCase()
                    .indexOf(value) > -1
            );
        });
    });
});

</script>

<?php require_once __DIR__ . '/../include/footer.php'; ?>