<?php

declare(strict_types=1);

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

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

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

// ======================================================
// FETCH MEMBERS
// ======================================================

$query = "
    SELECT

        lm.id,
        lm.card_no,
        lm.user_type,

        s.name AS student_name,

        t.name AS teacher_name

    FROM library_members lm

    LEFT JOIN students s
        ON lm.reference_id = s.id
        AND lm.user_type = 'Student'

    LEFT JOIN teachers t
        ON lm.reference_id = t.id
        AND lm.user_type = 'Teacher'

    WHERE lm.school_id = ?
    AND lm.status = 'Active'

    ORDER BY lm.id DESC
";

$stmt = $pdo->prepare($query);

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

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

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

$stmt = $pdo->prepare("
    SELECT

        id,
        book_title,
        qty

    FROM books

    WHERE school_id = ?
    AND qty > 0

    ORDER BY book_title ASC
");

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

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

$page_title = 'Issue Book';

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-reader text-primary"></i>

                        Issue Book

                    </h1>

                    <small class="text-muted">

                        Assign books to library members

                    </small>

                </div>

                <div>

                    <a
                        href="list_issued_books.php"
                        class="btn btn-info"
                    >

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

                        Issued Books

                    </a>

                </div>

            </div>

        </div>

    </section>

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

    <section class="content">

        <div class="container-fluid">

            <!-- RESPONSE -->

            <div
                id="responseMessage"
                style="display:none;"
            ></div>

            <div class="row justify-content-center">

                <div class="col-12 col-lg-8">

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

                        <div class="card-header">

                            <h3 class="card-title">

                                Book Issue Form

                            </h3>

                        </div>

                        <!-- FORM -->

                        <form
                            id="issueBookForm"
                            autocomplete="off"
                        >

                            <div class="card-body">

                                <!-- MEMBER -->

                                <div class="form-group">

                                    <label>

                                        Select Member *

                                    </label>

                                    <select
                                        id="member_id"
                                        class="form-control"
                                        required
                                    >

                                        <option value="">

                                            Select Member

                                        </option>

                                        <?php foreach ($members as $member) : ?>

                                            <?php

                                            $name =
                                                $member['user_type'] === 'Student'

                                                    ? $member['student_name']

                                                    : $member['teacher_name'];

                                            ?>

                                            <option
                                                value="<?= (int) $member['id'] ?>"
                                            >

                                                <?= htmlspecialchars($name) ?>

                                                -

                                                <?= htmlspecialchars($member['card_no']) ?>

                                            </option>

                                        <?php endforeach; ?>

                                    </select>

                                </div>

                                <!-- BOOK -->

                                <div class="form-group">

                                    <label>

                                        Select Book *

                                    </label>

                                    <select
                                        id="book_id"
                                        class="form-control"
                                        required
                                    >

                                        <option value="">

                                            Select Book

                                        </option>

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

                                            <option
                                                value="<?= (int) $book['id'] ?>"
                                            >

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

                                                (Stock:

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

                                                )

                                            </option>

                                        <?php endforeach; ?>

                                    </select>

                                </div>

                                <!-- DUE DATE -->

                                <div class="form-group">

                                    <label>

                                        Due Date *

                                    </label>

                                    <input
                                        type="date"
                                        id="due_date"
                                        class="form-control"
                                        required
                                    >

                                </div>

                            </div>

                            <!-- FOOTER -->

                            <div class="card-footer text-right">

                                <button
                                    type="submit"
                                    class="btn btn-primary"
                                    id="issueBtn"
                                >

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

                                    Issue Book

                                </button>

                            </div>

                        </form>

                    </div>

                </div>

            </div>

        </div>

    </section>

</div>

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

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

<script src="assets/js/issue.js"></script>

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