<?php

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

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

$school_id = $_SESSION['school_id'];

$page_title = 'Student Profile';

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

/*
|--------------------------------------------------------------------------
| VALIDATE STUDENT ID
|--------------------------------------------------------------------------
*/

$student_id =
    (int) ($_GET['id'] ?? 0);

if (!$student_id) {

    die('Invalid student ID.');

}

/*
|--------------------------------------------------------------------------
| CURRENT SESSION
|--------------------------------------------------------------------------
*/

$current_session_id =
    get_current_session_id($pdo);

/*
|--------------------------------------------------------------------------
| STUDENT DETAILS
|--------------------------------------------------------------------------
*/

$stmt = $pdo->prepare("

    SELECT

        s.*,

        ss.id AS student_session_id,
        ss.session_id,
        ss.class_id,

        ss.roll_no,
        ss.admission_no,
        ss.section,
        ss.status,

        c.class_name,

        ac.session_name

    FROM students s

    LEFT JOIN student_sessions ss
        ON ss.student_id = s.id
        AND ss.school_id = s.school_id
        AND ss.session_id = ?

    LEFT JOIN classes c
        ON c.id = ss.class_id

    LEFT JOIN academic_sessions ac
        ON ac.id = ss.session_id

    WHERE s.id = ?
    AND s.school_id = ?

    LIMIT 1

");

$stmt->execute([

    $current_session_id,
    $student_id,
    $school_id

]);

$student =
    $stmt->fetch(PDO::FETCH_ASSOC);

if (!$student) {

    die('Student not found.');

}

/*
|--------------------------------------------------------------------------
| SESSION HISTORY
|--------------------------------------------------------------------------
*/

$historyStmt = $pdo->prepare("

    SELECT

        ss.*,

        c.class_name,
        ac.session_name

    FROM student_sessions ss

    LEFT JOIN classes c
        ON c.id = ss.class_id

    LEFT JOIN academic_sessions ac
        ON ac.id = ss.session_id

    WHERE ss.student_id = ?
    AND ss.school_id = ?

    ORDER BY ss.id DESC

");

$historyStmt->execute([

    $student_id,
    $school_id

]);

$session_history =
    $historyStmt->fetchAll(PDO::FETCH_ASSOC);

/*
|--------------------------------------------------------------------------
| PARENT DETAILS
|--------------------------------------------------------------------------
*/

$parentStmt = $pdo->prepare("

    SELECT *
    FROM student_parents

    WHERE student_id = ?
    AND school_id = ?

    LIMIT 1

");

$parentStmt->execute([

    $student_id,
    $school_id

]);

$parentData =
    $parentStmt->fetch(PDO::FETCH_ASSOC);

/*
|--------------------------------------------------------------------------
| ADDRESS DETAILS
|--------------------------------------------------------------------------
*/

$addressStmt = $pdo->prepare("

    SELECT *
    FROM student_address

    WHERE student_id = ?
    AND school_id = ?

    LIMIT 1

");

$addressStmt->execute([

    $student_id,
    $school_id

]);

$addressData =
    $addressStmt->fetch(PDO::FETCH_ASSOC);

/*
|--------------------------------------------------------------------------
| DOCUMENT DETAILS
|--------------------------------------------------------------------------
*/

$documentStmt = $pdo->prepare("

    SELECT *
    FROM student_documents

    WHERE student_id = ?
    AND school_id = ?

    LIMIT 1

");

$documentStmt->execute([

    $student_id,
    $school_id

]);

$documentData =
    $documentStmt->fetch(PDO::FETCH_ASSOC);

/*
|--------------------------------------------------------------------------
| ACADEMIC DETAILS
|--------------------------------------------------------------------------
*/

$academicStmt = $pdo->prepare("

    SELECT *
    FROM student_academic_info

    WHERE student_id = ?
    AND school_id = ?

    LIMIT 1

");

$academicStmt->execute([

    $student_id,
    $school_id

]);

$academicData =
    $academicStmt->fetch(PDO::FETCH_ASSOC);

/*
|--------------------------------------------------------------------------
| CATEGORY DETAILS
|--------------------------------------------------------------------------
*/

$categoryStmt = $pdo->prepare("

    SELECT *
    FROM student_category_info

    WHERE student_id = ?
    AND school_id = ?

    LIMIT 1

");

$categoryStmt->execute([

    $student_id,
    $school_id

]);

$categoryData =
    $categoryStmt->fetch(PDO::FETCH_ASSOC);

/*
|--------------------------------------------------------------------------
| BANK DETAILS
|--------------------------------------------------------------------------
*/

$bankStmt = $pdo->prepare("

    SELECT *
    FROM student_bank_details

    WHERE student_id = ?
    AND school_id = ?

    LIMIT 1

");

$bankStmt->execute([

    $student_id,
    $school_id

]);

$bankData =
    $bankStmt->fetch(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 Profile

                    </h1>

                </div>

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

                    <a
                        href="index.php"
                        class="btn btn-secondary"
                    >

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

                        Back to Students

                    </a>

                </div>

            </div>

        </div>

    </section>

    <!-- MAIN CONTENT -->

    <section class="content">

        <div class="container-fluid">

            <div class="row">

                <!-- LEFT SIDEBAR -->

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

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

                        <div
                            class="
                                card-body
                                box-profile
                            "
                        >

                            <!-- PHOTO -->

                            <div class="text-center mb-3">

                                <?php

                                $photoPath =
                                    !empty($student['photo'])
                                    ? 'uploads/' .
                                        $student['photo']
                                    : 'https://via.placeholder.com/150';

                                ?>

                                <img
                                    src="<?= htmlspecialchars($photoPath) ?>"

                                    alt="Student Photo"

                                    class="
                                        profile-user-img
                                        img-fluid
                                        img-circle
                                    "

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

                            </div>

                            <!-- NAME -->

                            <h3
                                class="
                                    profile-username
                                    text-center
                                "
                            >

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

                            </h3>

                            <!-- CLASS -->

                            <p
                                class="
                                    text-muted
                                    text-center
                                    mb-4
                                "
                            >

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

                            </p>

                            <!-- QUICK INFO -->

                            <ul
                                class="
                                    list-group
                                    list-group-unbordered
                                    mb-3
                                "
                            >

                                <li class="list-group-item">

                                    <b>

                                        Roll No

                                    </b>

                                    <span class="float-right">

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

                                    </span>

                                </li>

                                <li class="list-group-item">

                                    <b>

                                        Scholar No

                                    </b>

                                    <span class="float-right">

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

                                    </span>

                                </li>

                                <li class="list-group-item">

                                    <b>

                                        Admission No

                                    </b>

                                    <span class="float-right">

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

                                    </span>

                                </li>

                                <li class="list-group-item">

                                    <b>

                                        Section

                                    </b>

                                    <span class="float-right">

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

                                    </span>

                                </li>

                                <li class="list-group-item">

                                    <b>

                                        Session

                                    </b>

                                    <span class="float-right">

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

                                    </span>

                                </li>

                                <li class="list-group-item">

                                    <b>

                                        Status

                                    </b>

                                    <span class="float-right">

                                        <?php
                                        $statusClass =
                                            $student['status']
                                            == 'Active'
                                            ? 'success'
                                            : 'secondary';
                                        ?>

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

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

                                        </span>

                                    </span>

                                </li>

                            </ul>

                            <!-- ACTION BUTTON -->

                            <a
                                href="edit.php?id=<?= $student['id'] ?>"
                                class="
                                    btn
                                    btn-primary
                                    btn-block
                                "
                            >

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

                                Edit Student

                            </a>

                        </div>

                    </div>

                </div>

                <!-- RIGHT CONTENT -->

                <div class="col-lg-8 col-md-7 col-12">
				                    <div class="card">

                        <!-- TAB HEADER -->

                        <div class="card-header p-2">

                            <ul
                                class="
                                    nav
                                    nav-pills
                                "
                            >

                                <li class="nav-item">

                                    <a
                                        class="
                                            nav-link
                                            active
                                        "
                                        href="#personal"
                                        data-toggle="tab"
                                    >

                                        Personal

                                    </a>

                                </li>

                                <li class="nav-item">

                                    <a
                                        class="nav-link"
                                        href="#parents"
                                        data-toggle="tab"
                                    >

                                        Parents

                                    </a>

                                </li>

                                <li class="nav-item">

                                    <a
                                        class="nav-link"
                                        href="#academic"
                                        data-toggle="tab"
                                    >

                                        Academic

                                    </a>

                                </li>

                                <li class="nav-item">

                                    <a
                                        class="nav-link"
                                        href="#documents"
                                        data-toggle="tab"
                                    >

                                        Documents

                                    </a>

                                </li>

                                <li class="nav-item">

                                    <a
                                        class="nav-link"
                                        href="#bank"
                                        data-toggle="tab"
                                    >

                                        Bank

                                    </a>

                                </li>

                            </ul>

                        </div>

                        <!-- TAB BODY -->

                        <div class="card-body">

                            <div class="tab-content">

                                <!-- =====================================================
                                PERSONAL TAB
                                ====================================================== -->

                                <div
                                    class="
                                        active
                                        tab-pane
                                    "
                                    id="personal"
                                >

                                    <div class="table-responsive">

                                        <table
                                            class="
                                                table
                                                table-bordered
                                                table-striped
                                            "
                                        >

                                            <tr>

                                                <th
                                                    style="
                                                        width:35%;
                                                    "
                                                >

                                                    Full Name

                                                </th>

                                                <td>

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

                                                </td>

                                            </tr>

                                            <tr>

                                                <th>

                                                    Gender

                                                </th>

                                                <td>

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

                                                </td>

                                            </tr>

                                            <tr>

                                                <th>

                                                    Date Of Birth

                                                </th>

                                                <td>

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

                                                </td>

                                            </tr>

                                            <tr>

                                                <th>

                                                    Mobile Number

                                                </th>

                                                <td>

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

                                                </td>

                                            </tr>

                                            <tr>

                                                <th>

                                                    Email Address

                                                </th>

                                                <td>

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

                                                </td>

                                            </tr>

                                            <tr>

                                                <th>

                                                    Blood Group

                                                </th>

                                                <td>

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

                                                </td>

                                            </tr>

                                            <tr>

                                                <th>

                                                    Current Address

                                                </th>

                                                <td>

                                                    <?= nl2br(
                                                        htmlspecialchars(
                                                            $addressData['address']
                                                            ?? '-'
                                                        )
                                                    ) ?>

                                                </td>

                                            </tr>

                                            <tr>

                                                <th>

                                                    Permanent Address

                                                </th>

                                                <td>

                                                    <?= nl2br(
                                                        htmlspecialchars(
                                                            $addressData['permanent_address']
                                                            ?? '-'
                                                        )
                                                    ) ?>

                                                </td>

                                            </tr>

                                            <tr>

                                                <th>

                                                    State

                                                </th>

                                                <td>

                                                    <?= htmlspecialchars(
                                                        $addressData['state']
                                                        ?? '-'
                                                    ) ?>

                                                </td>

                                            </tr>

                                            <tr>

                                                <th>

                                                    City

                                                </th>

                                                <td>

                                                    <?= htmlspecialchars(
                                                        $addressData['city']
                                                        ?? '-'
                                                    ) ?>

                                                </td>

                                            </tr>

                                            <tr>

                                                <th>

                                                    Village

                                                </th>

                                                <td>

                                                    <?= htmlspecialchars(
                                                        $addressData['village']
                                                        ?? '-'
                                                    ) ?>

                                                </td>

                                            </tr>

                                            <tr>

                                                <th>

                                                    Pincode

                                                </th>

                                                <td>

                                                    <?= htmlspecialchars(
                                                        $addressData['pincode']
                                                        ?? '-'
                                                    ) ?>

                                                </td>

                                            </tr>

                                        </table>

                                    </div>

                                </div>
								                                <!-- =====================================================
                                PARENTS TAB
                                ====================================================== -->

                                <div
                                    class="tab-pane"
                                    id="parents"
                                >

                                    <div class="table-responsive">

                                        <table
                                            class="
                                                table
                                                table-bordered
                                                table-striped
                                            "
                                        >

                                            <tr>
                                                <th style="width:35%;">
                                                    Father Name
                                                </th>

                                                <td>
                                                    <?= htmlspecialchars(
                                                        $parentData['father_name']
                                                        ?? '-'
                                                    ) ?>
                                                </td>
                                            </tr>

                                            <tr>
                                                <th>
                                                    Father Phone
                                                </th>

                                                <td>
                                                    <?= htmlspecialchars(
                                                        $parentData['father_phone']
                                                        ?? '-'
                                                    ) ?>
                                                </td>
                                            </tr>

                                            <tr>
                                                <th>
                                                    Father Occupation
                                                </th>

                                                <td>
                                                    <?= htmlspecialchars(
                                                        $parentData['father_occupation']
                                                        ?? '-'
                                                    ) ?>
                                                </td>
                                            </tr>

                                            <tr>
                                                <th>
                                                    Father Income
                                                </th>

                                                <td>
                                                    ₹ <?= htmlspecialchars(
                                                        $parentData['father_income']
                                                        ?? '0'
                                                    ) ?>
                                                </td>
                                            </tr>

                                            <tr>
                                                <th>
                                                    Mother Name
                                                </th>

                                                <td>
                                                    <?= htmlspecialchars(
                                                        $parentData['mother_name']
                                                        ?? '-'
                                                    ) ?>
                                                </td>
                                            </tr>

                                            <tr>
                                                <th>
                                                    Mother Phone
                                                </th>

                                                <td>
                                                    <?= htmlspecialchars(
                                                        $parentData['mother_phone']
                                                        ?? '-'
                                                    ) ?>
                                                </td>
                                            </tr>

                                            <tr>
                                                <th>
                                                    Mother Occupation
                                                </th>

                                                <td>
                                                    <?= htmlspecialchars(
                                                        $parentData['mother_occupation']
                                                        ?? '-'
                                                    ) ?>
                                                </td>
                                            </tr>

                                            <tr>
                                                <th>
                                                    Mother Income
                                                </th>

                                                <td>
                                                    ₹ <?= htmlspecialchars(
                                                        $parentData['mother_income']
                                                        ?? '0'
                                                    ) ?>
                                                </td>
                                            </tr>

                                            <tr>
                                                <th>
                                                    Guardian Name
                                                </th>

                                                <td>
                                                    <?= htmlspecialchars(
                                                        $parentData['guardian_name']
                                                        ?? '-'
                                                    ) ?>
                                                </td>
                                            </tr>

                                            <tr>
                                                <th>
                                                    Guardian Relation
                                                </th>

                                                <td>
                                                    <?= htmlspecialchars(
                                                        $parentData['guardian_relation']
                                                        ?? '-'
                                                    ) ?>
                                                </td>
                                            </tr>

                                            <tr>
                                                <th>
                                                    Guardian Phone
                                                </th>

                                                <td>
                                                    <?= htmlspecialchars(
                                                        $parentData['guardian_phone']
                                                        ?? '-'
                                                    ) ?>
                                                </td>
                                            </tr>

                                        </table>

                                    </div>

                                </div>

                                <!-- =====================================================
                                ACADEMIC TAB
                                ====================================================== -->

                                <div
                                    class="tab-pane"
                                    id="academic"
                                >

                                    <div class="table-responsive mb-4">

                                        <table
                                            class="
                                                table
                                                table-bordered
                                                table-striped
                                            "
                                        >

                                            <thead>

                                                <tr>

                                                    <th>
                                                        Session
                                                    </th>

                                                    <th>
                                                        Class
                                                    </th>

                                                    <th>
                                                        Section
                                                    </th>

                                                    <th>
                                                        Roll No
                                                    </th>

                                                    <th>
                                                        Status
                                                    </th>

                                                </tr>

                                            </thead>

                                            <tbody>

                                                <?php foreach (
                                                    $session_history
                                                    as $history
                                                ) { ?>

                                                <tr>

                                                    <td>

                                                        <?= htmlspecialchars(
                                                            $history['session_name']
                                                            ?? '-'
                                                        ) ?>

                                                    </td>

                                                    <td>

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

                                                    </td>

                                                    <td>

                                                        <?= htmlspecialchars(
                                                            $history['section']
                                                            ?? '-'
                                                        ) ?>

                                                    </td>

                                                    <td>

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

                                                    </td>

                                                    <td>

                                                        <?php

                                                        $badge =
                                                            'secondary';

                                                        if (
                                                            $history['status']
                                                            == 'Active'
                                                        ) {
                                                            $badge =
                                                                'success';
                                                        } elseif (
                                                            $history['status']
                                                            == 'Promoted'
                                                        ) {
                                                            $badge =
                                                                'info';
                                                        }

                                                        ?>

                                                        <span
                                                            class="
                                                                badge
                                                                badge-<?= $badge ?>
                                                            "
                                                        >

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

                                                        </span>

                                                    </td>

                                                </tr>

                                                <?php } ?>

                                            </tbody>

                                        </table>

                                    </div>

                                </div>

                                <!-- =====================================================
                                DOCUMENTS TAB
                                ====================================================== -->

                                <div
                                    class="tab-pane"
                                    id="documents"
                                >

                                    <div class="table-responsive">

                                        <table
                                            class="
                                                table
                                                table-bordered
                                                table-striped
                                            "
                                        >

                                            <tr>
                                                <th style="width:35%;">
                                                    Aadhaar Number
                                                </th>

                                                <td>
                                                    <?= htmlspecialchars(
                                                        $documentData['aadhaar_no']
                                                        ?? '-'
                                                    ) ?>
                                                </td>
                                            </tr>

                                            <tr>
                                                <th>
                                                    APAAR ID
                                                </th>

                                                <td>
                                                    <?= htmlspecialchars(
                                                        $documentData['apaar_id']
                                                        ?? '-'
                                                    ) ?>
                                                </td>
                                            </tr>

                                            <tr>
                                                <th>
                                                    PEN Number
                                                </th>

                                                <td>
                                                    <?= htmlspecialchars(
                                                        $documentData['pen_no']
                                                        ?? '-'
                                                    ) ?>
                                                </td>
                                            </tr>

                                            <tr>
                                                <th>
                                                    Samagra ID
                                                </th>

                                                <td>
                                                    <?= htmlspecialchars(
                                                        $documentData['samagra_id']
                                                        ?? '-'
                                                    ) ?>
                                                </td>
                                            </tr>

                                            <tr>
                                                <th>
                                                    Family SSSMID
                                                </th>

                                                <td>
                                                    <?= htmlspecialchars(
                                                        $documentData['family_sssmid']
                                                        ?? '-'
                                                    ) ?>
                                                </td>
                                            </tr>

                                        </table>

                                    </div>

                                </div>

                                <!-- =====================================================
                                BANK TAB
                                ====================================================== -->

                                <div
                                    class="tab-pane"
                                    id="bank"
                                >

                                    <div class="table-responsive">

                                        <table
                                            class="
                                                table
                                                table-bordered
                                                table-striped
                                            "
                                        >

                                            <tr>

                                                <th style="width:35%;">

                                                    Bank Name

                                                </th>

                                                <td>

                                                    <?= htmlspecialchars(
                                                        $bankData['bank_name']
                                                        ?? '-'
                                                    ) ?>

                                                </td>

                                            </tr>

                                            <tr>

                                                <th>

                                                    Account Number

                                                </th>

                                                <td>

                                                    <?= htmlspecialchars(
                                                        $bankData['bank_acc_no']
                                                        ?? '-'
                                                    ) ?>

                                                </td>

                                            </tr>

                                            <tr>

                                                <th>

                                                    IFSC Code

                                                </th>

                                                <td>

                                                    <?= htmlspecialchars(
                                                        $bankData['ifsc_code']
                                                        ?? '-'
                                                    ) ?>

                                                </td>

                                            </tr>

                                            <tr>

                                                <th>

                                                    Opening Balance

                                                </th>

                                                <td>

                                                    ₹ <?= htmlspecialchars(
                                                        $bankData['opening_balance']
                                                        ?? '0'
                                                    ) ?>

                                                </td>

                                            </tr>

                                        </table>

                                    </div>

                                </div>

                            </div>

                        </div>

                    </div>

                </div>

            </div>

        </div>

    </section>

</div>

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