<?php

declare(strict_types=1);

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

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

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

$page_title = 'Add Library Member';

// ======================================================
// FETCH STUDENTS
// ======================================================

$stmt = $pdo->prepare("
    SELECT

        s.id,
        s.name

    FROM students s

    WHERE s.school_id = ?

    ORDER BY s.name ASC
");

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

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

// ======================================================
// FETCH TEACHERS
// ======================================================

$stmt = $pdo->prepare("
    SELECT

        id,
        name

    FROM teachers

    WHERE school_id = ?

    ORDER BY name ASC
");

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

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

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

                        Add Library Member

                    </h1>

                    <small class="text-muted">

                        Create new library membership

                    </small>

                </div>

                <div>

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

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

                        View Members

                    </a>

                </div>

            </div>

        </div>

    </section>

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

    <section class="content">

        <div class="container-fluid">

            <!-- ALERT -->

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

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

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

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

                        <div class="card-header">

                            <h3 class="card-title">

                                Member Registration Form

                            </h3>

                        </div>

                        <!-- FORM -->

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

                            <div class="card-body">

                                <!-- USER TYPE -->

                                <div class="form-group">

                                    <label>

                                        User Type *

                                    </label>

                                    <select
                                        name="user_type"
                                        id="user_type"
                                        class="form-control"
                                        required
                                    >

                                        <option value="">

                                            Select User Type

                                        </option>

                                        <option value="Student">

                                            Student

                                        </option>

                                        <option value="Teacher">

                                            Teacher

                                        </option>

                                    </select>

                                </div>

                                <!-- STUDENT DROPDOWN -->

                                <div
                                    class="form-group"
                                    id="studentBox"
                                    style="display:none;"
                                >

                                    <label>

                                        Select Student *

                                    </label>

                                    <select
                                        id="studentSelect"
                                        class="form-control"
                                    >

                                        <option value="">

                                            Select Student

                                        </option>

                                        <?php foreach ($students as $student) : ?>

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

                                                <?= htmlspecialchars($student['name']) ?>

                                            </option>

                                        <?php endforeach; ?>

                                    </select>

                                </div>

                                <!-- TEACHER DROPDOWN -->

                                <div
                                    class="form-group"
                                    id="teacherBox"
                                    style="display:none;"
                                >

                                    <label>

                                        Select Teacher *

                                    </label>

                                    <select
                                        id="teacherSelect"
                                        class="form-control"
                                    >

                                        <option value="">

                                            Select Teacher

                                        </option>

                                        <?php foreach ($teachers as $teacher) : ?>

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

                                                <?= htmlspecialchars($teacher['name']) ?>

                                            </option>

                                        <?php endforeach; ?>

                                    </select>

                                </div>

                                <!-- HIDDEN REFERENCE -->

                                <input
                                    type="hidden"
                                    name="reference_id"
                                    id="reference_id"
                                >

                                <!-- CARD NUMBER -->

                                <div class="form-group">

                                    <label>

                                        Library Card Number *

                                    </label>

                                    <input
                                        type="text"
                                        name="card_no"
                                        id="card_no"
                                        class="form-control"
                                        maxlength="50"
                                        placeholder="Enter card number"
                                        required
                                    >

                                </div>

                            </div>

                            <!-- FOOTER -->

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

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

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

                                    Save Member

                                </button>

                            </div>

                        </form>

                    </div>

                </div>

            </div>

        </div>

    </section>

</div>

<!-- ============================================== -->
<!-- JS -->
<!-- ============================================== -->

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

<script src="assets/js/member.js"></script>
<?php require_once __DIR__ . '/../include/footer.php'; ?>