<?php

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

$school_id = $_SESSION['school_id'];
$session_id = get_current_session_id($pdo);

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

$page_title = 'View Students';

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

/*
|--------------------------------------------------------------------------
| FILTERS
|--------------------------------------------------------------------------
*/

$classid = isset($_POST['classid'])
    ? (int) $_POST['classid']
    : 0;

/*
|--------------------------------------------------------------------------
| FETCH CLASSES
|--------------------------------------------------------------------------
*/


$classStmt = $pdo->prepare("
    SELECT
        id,
        class_name
    FROM classes
    WHERE school_id = ?
    ORDER BY class_name ASC
");

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

$classes =
    $classStmt->fetchAll(PDO::FETCH_ASSOC);

?>

<div class="content-wrapper">

    <!-- PAGE HEADER -->

    <section class="content-header">

        <div class="container-fluid">

            <div
                class="
                    row
                    mb-2
                    align-items-center
                "
            >

                <div class="col-sm-6">

                    <h1>

                        Student Directory

                    </h1>

                </div>

                <div
                    class="
                        col-sm-6
                        text-sm-right
                        mt-2
                        mt-sm-0
                    "
                >

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

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

                        Add New Student

                    </a>

                </div>

            </div>

        </div>

    </section>

    <!-- MAIN CONTENT -->

    <section class="content">

        <div class="container-fluid">

            <!-- FILTER CARD -->

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

                <div class="card-body">

                    <form method="post">

                        <div class="row">

                            <!-- CLASS FILTER -->

                            <div
                                class="
                                    col-lg-4
                                    col-md-6
                                    col-12
                                    form-group
                                    mb-0
                                "
                            >

                                <label>

                                    Select Class

                                </label>

                                <select
                                    name="classid"
                                    class="form-control"
                                    onchange="this.form.submit();"
                                >

                                    <option value="0">

                                        -- Select Class --

                                    </option>

                                    <?php foreach ($classes as $c) { ?>

                                        <option
                                            value="<?= $c['id'] ?>"

                                            <?= $classid == $c['id']
                                                ? 'selected'
                                                : '' ?>
                                        >

                                            <?= htmlspecialchars(
                                                $c['class_name']
                                            ) ?>

                                        </option>

                                    <?php } ?>

                                </select>

                            </div>

                        </div>

                    </form>

                </div>

            </div>

            <!-- STUDENT LIST CARD -->

            <div
                class="
                    card
                    card-outline
                    card-info
                "
            >

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

                    <h3 class="card-title">

                        Student Records

                    </h3>

                    <?php if ($classid > 0) { ?>

                        <span class="badge badge-primary px-3 py-2">

                            Current Session Students

                        </span>

                    <?php } ?>

                </div>

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

                    <div class="table-responsive">

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

                            <thead class="thead-dark">

                                <tr>

                                    <th
                                        style="
                                            min-width:80px;
                                        "
                                    >

                                        Photo

                                    </th>

                                    <th
                                        style="
                                            min-width:220px;
                                        "
                                    >

                                        Student

                                    </th>

                                    <th
                                        style="
                                            min-width:100px;
                                        "
                                    >

                                        Roll No

                                    </th>

                                    <th
                                        style="
                                            min-width:120px;
                                        "
                                    >

                                        Scholar No

                                    </th>

                                    <th
                                        style="
                                            min-width:120px;
                                        "
                                    >

                                        Status

                                    </th>

                                    <th
                                        class="text-center"

                                        style="
                                            min-width:220px;
                                        "
                                    >

                                        Actions

                                    </th>

                                </tr>

                            </thead>

                            <tbody>
							<?php

/*
|--------------------------------------------------------------------------
| FETCH STUDENTS
|--------------------------------------------------------------------------
*/

if ($classid > 0) {
	

    $stmt = $pdo->prepare("

        SELECT

            s.id,
            s.name,
            s.photo,
            s.enrollment_no,

            ss.roll_no,
            ss.status,
            ss.section

        FROM students s

        INNER JOIN student_sessions ss
            ON ss.student_id = s.id

        WHERE ss.class_id = ?
        AND ss.session_id = ?
        AND ss.school_id = ?
        AND ss.status = 'Active'

        ORDER BY s.name ASC

    ");

    $stmt->execute([
        $classid,
        $session_id,
        $school_id
    ]);

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

    if (!empty($students)) {

        foreach ($students as $s) {

            $serverPath =
                __DIR__ .
                '/uploads/' .
                $s['photo'];

            $imageUrl =
                WEB_ROOT .
                'student/uploads/' .
                $s['photo'];

            $displayPhoto =
                (
                    !empty($s['photo']) &&
                    file_exists($serverPath)
                )
                ? $imageUrl
                : 'https://via.placeholder.com/60';

            $statusClass =
                $s['status'] == 'Active'
                ? 'success'
                : 'secondary';

?>

<tr>

    <!-- PHOTO -->

    <td class="text-center align-middle">

        <img
            src="<?= htmlspecialchars($displayPhoto) ?>"
            class="img-thumbnail rounded-circle"

            style="
                width:60px;
                height:60px;
                object-fit:cover;
            "
        >

    </td>

    <!-- STUDENT -->

    <td class="align-middle">

        <strong class="d-block">

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

        </strong>

        <small class="text-muted">

            Section:
            <?= htmlspecialchars(
                $s['section'] ?? '-'
            ) ?>

        </small>

    </td>

    <!-- ROLL NUMBER -->

    <td class="align-middle">

        <?= htmlspecialchars(
            $s['roll_no'] ?? '-'
        ) ?>

    </td>

    <!-- SCHOLAR NUMBER -->

    <td class="align-middle">

        <?= htmlspecialchars(
            $s['enrollment_no'] ?? '-'
        ) ?>

    </td>

    <!-- STATUS -->

    <td class="align-middle">

        <span
            class="
                badge
                badge-<?= $statusClass ?>
                px-3
                py-2
            "
        >

            <?= htmlspecialchars(
                $s['status']
            ) ?>

        </span>

    </td>

    <!-- ACTIONS -->

    <td class="text-center align-middle">

        <div
            class="
                btn-group
                btn-group-sm
            "
        >                                            <a
                                                href="view.php?id=<?= $s['id'] ?>"
                                                class="btn btn-secondary"
                                                title="View Student"
                                            >

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

                                            </a>

                                            <a
                                                href="edit.php?id=<?= $s['id'] ?>"
                                                class="btn btn-info"
                                                title="Edit Student"
                                            >

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

                                            </a>

                                            <a
                                                href="process.php?action=delete&id=<?= $s['id'] ?>"
                                                class="btn btn-danger"

                                                title="Delete Student"

                                                onclick="
                                                    return confirm(
                                                        'Delete this student?'
                                                    )
                                                "
                                            >

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

                                            </a>

                                        </div>

                                    </td>

                                </tr>

<?php

        }

    } else {

?>

<tr>

    <td
        colspan="6"

        class="
            text-center
            py-5
            text-muted
        "
    >

        <div class="py-4">

            <i
                class="
                    fas
                    fa-user-graduate
                    fa-3x
                    mb-3
                    text-secondary
                "
            ></i>

            <h5>

                No Students Found

            </h5>

            <p class="mb-0">

                No active students available
                in selected class.

            </p>

        </div>

    </td>

</tr>

<?php

    }

} else {

?>

<tr>

    <td
        colspan="6"

        class="
            text-center
            py-5
            text-muted
        "
    >

        <div class="py-4">

            <i
                class="
                    fas
                    fa-filter
                    fa-3x
                    mb-3
                    text-secondary
                "
            ></i>

            <h5>

                Select a Class

            </h5>

            <p class="mb-0">

                Please select a class
                to view students.

            </p>

        </div>

    </td>

</tr>

<?php } ?>

                            </tbody>

                        </table>

                    </div>

                </div>

            </div>
			        </div>

    </section>

</div>

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