<?php
session_start();
require_once 'config.php';

// Define role constants
if (!defined('ROLE_SUPER_ADMIN')) define('ROLE_SUPER_ADMIN', 1);
if (!defined('ROLE_ADMIN')) define('ROLE_ADMIN', 2);
if (!defined('ROLE_TEACHER')) define('ROLE_TEACHER', 3);
if (!defined('ROLE_STUDENT')) define('ROLE_STUDENT', 4);
if (!defined('ROLE_PARENT')) define('ROLE_PARENT', 5);
if (!defined('ROLE_STAFF')) define('ROLE_STAFF', 6);

// Language Handling
$available_languages = ['en', 'lug', 'sw'];
$default_language = 'en';

if (isset($_GET['lang']) && in_array($_GET['lang'], $available_languages)) {
    $_SESSION['lang'] = $_GET['lang'];
} elseif (!isset($_SESSION['lang'])) {
    $_SESSION['lang'] = $default_language;
}

$current_lang = $_SESSION['lang'];

function load_language_data($lang) {
    $data = [
        'en' => [
            'dashboard_title_prefix' => 'Student Dashboard',
            'sidebar_dashboard' => 'Dashboard',
            'sidebar_profile' => 'Profile',
            'sidebar_reports' => 'Reports',
            'sidebar_assignments' => 'Assignments',
            'sidebar_messages' => 'Messages',
            'sidebar_settings' => 'Settings',
            'sidebar_logout' => 'Logout',
            'school_name_not_found' => 'School Name Not Found',
            'search_placeholder' => 'Search courses/data',
            'welcome_message_prefix' => 'Welcome',
            'welcome_message_suffix' => '!',
            'logged_in_as' => 'Logged in as:',
            'divine_reports' => 'DIVINE REPORTS',
            'profile_heading' => 'My Profile',
            'personal_information' => 'Personal Information',
            'admission_number' => 'Admission Number',
            'username_label' => 'Username',
            'email_label' => 'Email',
            'phone_number_label' => 'Phone Number',
            'status_label' => 'Account Status',
            'school_information' => 'School Information',
            'current_class' => 'Current Class',
            'current_stream' => 'Current Stream/Section',
            'subject_combination' => 'Subject Combination',
            'parent_guardian_info' => 'Parent/Guardian Information',
            'parent_name_label' => 'Parent/Guardian Name',
            'parent_phone_label' => 'Parent/Guardian Phone',
            'parent_email_label' => 'Parent/Guardian Email',
            'no_profile_data' => 'No profile data found for this student.',
            'update_profile_button' => 'Update Profile'
        ],
        'lug' => [
            'dashboard_title_prefix' => 'Diisiboodi y\'Omuyizi',
            'sidebar_dashboard' => 'Diisiboodi',
            'sidebar_profile' => 'Purofayiro',
            'sidebar_reports' => 'Lipooti',
            'sidebar_assignments' => 'Ebitongole',
            'sidebar_messages' => 'Obubaka',
            'sidebar_settings' => 'Enteekateeka',
            'sidebar_logout' => 'Log Out',
            'school_name_not_found' => 'Erinnya ly\'Essomero Terinnaba Kulabika',
            'search_placeholder' => 'Noonya amasomo/data',
            'welcome_message_prefix' => 'Mpulira Olamusiddwa',
            'welcome_message_suffix' => '!',
            'logged_in_as' => 'Oyinze okukakasa nga:',
            'divine_reports' => 'RIPOTA ZA MUNGU',
            'profile_heading' => 'Purofayiro Yange',
            'personal_information' => 'Ebikwata ku Gwe',
            'admission_number' => 'Namba y\'okuayingizibwa',
            'username_label' => 'Erinnya',
            'email_label' => 'E-mail',
            'phone_number_label' => 'Namba y\'essimu',
            'status_label' => 'Embeera y\'Akawunti',
            'school_information' => 'Ebikwata ku Ssomero',
            'current_class' => 'Ekibiina Ekisangamu',
            'current_stream' => 'Omudda/Ekitundu Ekisangamu',
            'subject_combination' => 'Ebisomesebwa Ebirabiddwa',
            'parent_guardian_info' => 'Ebikwata ku Muzadde/Mulabirira',
            'parent_name_label' => 'Erinnya ly\'Omukadde/Omulabirira',
            'parent_phone_label' => 'Namba y\'essimu y\'Omukadde/Omulabirira',
            'parent_email_label' => 'E-mail y\'Omukadde/Omulabirira',
            'no_profile_data' => 'Tewali byafayo bya pulofayiro bisangiddwa ku muyizi ono.',
            'update_profile_button' => 'Ffula Purofayiro'
        ],
        'sw' => [
            'dashboard_title_prefix' => 'Dashibodi ya Mwanafunzi',
            'sidebar_dashboard' => 'Dashibodi',
            'sidebar_profile' => 'Wasifu',
            'sidebar_reports' => 'Ripoti',
            'sidebar_assignments' => 'Kazi',
            'sidebar_messages' => 'Ujumbe',
            'sidebar_settings' => 'Mipangilio',
            'sidebar_logout' => 'Ondoka',
            'school_name_not_found' => 'Jina la Shule Halikupatikana',
            'search_placeholder' => 'Tafuta kozi/data',
            'welcome_message_prefix' => 'Karibu',
            'welcome_message_suffix' => '!',
            'logged_in_as' => 'Umeingia kama:',
            'divine_reports' => 'RIPOTI ZA MUNGU',
            'profile_heading' => 'Wasifu Wangu',
            'personal_information' => 'Taarifa Binafsi',
            'admission_number' => 'Namba ya Uandikishaji',
            'username_label' => 'Jina la Mtumiaji',
            'email_label' => 'Barua pepe',
            'phone_number_label' => 'Namba ya Simu',
            'status_label' => 'Hali ya Akaunti',
            'school_information' => 'Taarifa za Shule',
            'current_class' => 'Darasa la Sasa',
            'current_stream' => 'Mkondo/Sehemu ya Sasa',
            'subject_combination' => 'Mchanganyiko wa Masomo',
            'parent_guardian_info' => 'Taarifa za Mzazi/Mlezi',
            'parent_name_label' => 'Jina la Mzazi/Mlezi',
            'parent_phone_label' => 'Simu ya Mzazi/Mlezi',
            'parent_email_label' => 'Barua Pepe ya Mzazi/Mlezi',
            'no_profile_data' => 'Hakuna data ya wasifu iliyopatikana kwa mwanafunzi huyu.',
            'update_profile_button' => 'Sasisha Wasifu'
        ]
    ];
    return $data[$lang] ?? $data['en'];
}

$_L = load_language_data($current_lang);

function check_role_access($required_role_code) {
    if (!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true || !isset($_SESSION["school_id"])) {
        header("location: login.php");
        exit;
    }

    if ($_SESSION["role"] !== $required_role_code) {
        switch ($_SESSION["role"]) {
            case ROLE_SUPER_ADMIN:
                header("location: super_admin_dashboard.php");
                break;
            case ROLE_ADMIN:
                header("location: admin_dashboard.php");
                break;
            case ROLE_TEACHER:
                header("location: teacher_dashboard.php");
                break;
            default:
                header("location: login.php");
                break;
        }
        exit;
    }
}

check_role_access(ROLE_STUDENT);

$currentPage = 'profile';

$student_user_id = $_SESSION["id"];
$school_id = $_SESSION["school_id"];
$username = htmlspecialchars($_SESSION["username"]);
$user_role_code = $_SESSION["role"];

$student_photo = "images/default_student_photo.png";
$school_name = $_L['school_name_not_found'];
$school_logo_path = "images/default_school_logo.png";

$role_name = "Unknown";
switch ($user_role_code) {
    case ROLE_SUPER_ADMIN: $role_name = "Super Admin"; break;
    case ROLE_ADMIN:        $role_name = "Admin";       break;
    case ROLE_TEACHER:      $role_name = "Teacher";     break;
    case ROLE_STUDENT:      $role_name = "Student";     break;
    case ROLE_PARENT:       $role_name = "Parent";      break;
    case ROLE_STAFF:        $role_name = "Staff";       break;
}

$student_profile_data = null;

if (isset($conn) && $conn instanceof mysqli) {
    // CORRECTED QUERY: Get data from users table and JOIN with classes and streams
    $sql_profile = "
        SELECT
            u.username, u.email, u.phone_number, u.photo, u.status, u.ADM,
            u.parent_name, u.parent_phone, u.parent_email,
            u.subject_combination,
            COALESCE(c.class_name, 'N/A') AS class_name,
            COALESCE(s.stream_name, 'N/A') AS stream_name
        FROM
            users u
        LEFT JOIN
            classes c ON u.class_id = c.class_id AND u.school_id = c.school_id
        LEFT JOIN
            streams s ON u.stream_id = s.stream_id AND u.school_id = s.school_id
        WHERE
            u.id = ? AND u.school_id = ? AND u.role = ?;
    ";
    
    if ($stmt_profile = $conn->prepare($sql_profile)) {
        $student_role_const = ROLE_STUDENT;
        $stmt_profile->bind_param("iii", $student_user_id, $school_id, $student_role_const);
        
        if ($stmt_profile->execute()) {
            $result_profile = $stmt_profile->get_result();
            if ($result_profile->num_rows > 0) {
                $student_profile_data = $result_profile->fetch_assoc();
                $student_photo = !empty($student_profile_data['photo']) ? htmlspecialchars($student_profile_data['photo']) : 'images/default_student_photo.png';
            } else {
                error_log("No profile data found for student user_id: $student_user_id, school_id: $school_id");
            }
        } else {
            error_log("Execute failed for student profile query: " . $stmt_profile->error);
        }
        $stmt_profile->close();
    } else {
        error_log("Prepare failed for student profile query: " . $conn->error);
    }

    // Fetch school details
    $sql_school_details = "SELECT school_name, logo FROM schools WHERE school_id = ?";
    if ($stmt_school = $conn->prepare($sql_school_details)) {
        $stmt_school->bind_param("i", $school_id);
        if ($stmt_school->execute()) {
            $stmt_school->bind_result($school_name_db, $school_logo_db);
            if ($stmt_school->fetch()) {
                $school_name = htmlspecialchars($school_name_db);
                $school_logo_path = !empty($school_logo_db) ? htmlspecialchars($school_logo_db) : 'images/default_school_logo.png';
            }
        }
        $stmt_school->close();
    }

    $conn->close();
} else {
    error_log("Database connection \$conn not available in student_profile.php");
}

?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo $_L['sidebar_profile']; ?> - <?php echo $school_name; ?></title>
    <link rel="stylesheet" href="style.css">
    <link rel="icon" href="<?php echo $school_logo_path; ?>" type="image/png">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
    <script src="https://cdn.tailwindcss.com"></script>
    <style>
        /* Fixed Header and Sidebar Styles */
        .dashboard-container {
            display: flex;
            height: 100vh;
            overflow: hidden;
        }

        .sidebar {
            position: fixed;
            left: 0;
            top: 0;
            height: 100vh;
            width: 250px;
            overflow-y: auto;
            z-index: 1000;
            box-shadow: 2px 0 8px rgba(0, 0, 0, 0.1);
        }

        .main-content {
            margin-left: 250px;
            width: calc(100% - 250px);
            height: 100vh;
            display: flex;
            flex-direction: column;
            overflow: hidden;
        }

        .header {
            position: fixed;
            top: 0;
            left: 250px;
            right: 0;
            z-index: 999;
            background-color: #ffffff;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
        }

        /* Scrollable content area with fade effect */
        .content-wrapper {
            flex: 1;
            overflow-y: auto;
            overflow-x: hidden;
            margin-top: 80px; /* Adjust based on header height */
            position: relative;
        }

        /* Fade overlay at the top */
        .content-wrapper::before {
            content: '';
            position: sticky;
            top: 0;
            left: 0;
            right: 0;
            height: 40px;
            background: linear-gradient(to bottom, rgba(255, 255, 255, 1) 0%, rgba(255, 255, 255, 0) 100%);
            pointer-events: none;
            z-index: 10;
        }

        /* Smooth scrolling */
        .content-wrapper {
            scroll-behavior: smooth;
        }

        /* Custom scrollbar */
        .content-wrapper::-webkit-scrollbar {
            width: 8px;
        }

        .content-wrapper::-webkit-scrollbar-track {
            background: #f1f1f1;
        }

        .content-wrapper::-webkit-scrollbar-thumb {
            background: #888;
            border-radius: 4px;
        }

        .content-wrapper::-webkit-scrollbar-thumb:hover {
            background: #555;
        }

        /* Responsive adjustments */
        @media (max-width: 768px) {
            .sidebar {
                transform: translateX(-100%);
                transition: transform 0.3s ease-in-out;
            }

            .sidebar.active {
                transform: translateX(0);
            }

            .main-content {
                margin-left: 0;
                width: 100%;
            }

            .header {
                left: 0;
            }
        }

        .profile-card {
            background-color: #ffffff;
            border-radius: 0.75rem;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
            padding: 2rem;
        }
        .profile-header {
            display: flex;
            flex-direction: column;
            align-items: center;
            margin-bottom: 2rem;
            text-align: center;
        }
        .profile-photo-container {
            width: 120px;
            height: 120px;
            border-radius: 50%;
            overflow: hidden;
            border: 4px solid #cbd5e0;
            display: flex;
            align-items: center;
            justify-content: center;
            margin-bottom: 1rem;
        }
        .profile-photo {
            width: 100%;
            height: 100%;
            object-fit: cover;
        }
        .info-grid {
            display: grid;
            grid-template-columns: 1fr;
            gap: 1.5rem;
        }
        @media (min-width: 768px) {
            .info-grid {
                grid-template-columns: repeat(2, 1fr);
            }
        }
        .info-item {
            padding: 1rem;
            background-color: #f8fafc;
            border-radius: 0.5rem;
            border: 1px solid #e2e8f0;
        }
        .info-item label {
            display: block;
            font-size: 0.875rem;
            color: #64748b;
            margin-bottom: 0.25rem;
            font-weight: 500;
        }
        .info-item p {
            font-size: 1rem;
            color: #334155;
            font-weight: 600;
        }
    
        /* Hide specific items from sidebar on mobile when footer is active */
        .hide-on-mobile {
            display: list-item;
        }

        /* --- Mobile Footer Navigation --- */
        #mobile-footer-nav {
            position: fixed;
            bottom: 0;
            left: 0;
            right: 0;
            height: 70px;
            background: #ffffff;
            border-top: 1px solid #e0e0e0;
            box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.1);
            z-index: 1000;
            padding-bottom: env(safe-area-inset-bottom, 0px);
            display: none;
        }

        #mobile-footer-nav ul {
            display: flex;
            list-style: none;
            height: 100%;
            margin: 0;
            padding: 0;
        }

        #mobile-footer-nav li {
            flex: 1;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        #mobile-footer-nav a {
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            text-decoration: none;
            color: #666666;
            font-size: 10px;
            font-weight: 500;
            width: 100%;
            height: 100%;
            transition: all 0.3s ease;
            position: relative;
        }

        #mobile-footer-nav a i {
            font-size: 22px;
            margin-bottom: 4px;
            transition: all 0.3s ease;
        }

        #mobile-footer-nav a.active {
            color: #4f46e5;
        }

        #mobile-footer-nav a.active::after {
            content: '';
            position: absolute;
            top: 0;
            left: 50%;
            transform: translateX(-50%);
            width: 30px;
            height: 3px;
            background: #4f46e5;
            border-radius: 2px;
        }
        
        /* --- More Menu Modal --- */
        #more-menu-modal {
            position: fixed;
            bottom: 0;
            left: 0;
            right: 0;
            height: auto;
            max-height: 80vh;
            background-color: #fff;
            border-top-left-radius: 20px;
            border-top-right-radius: 20px;
            box-shadow: 0 -4px 15px rgba(0, 0, 0, 0.15);
            z-index: 2000;
            transform: translateY(100%);
            transition: transform 0.3s ease-in-out;
            padding-bottom: env(safe-area-inset-bottom, 20px);
            overflow-y: auto;
            display: none;
        }

        #more-menu-modal.show {
            transform: translateY(0);
        }
        
        #more-menu-modal-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 15px 20px;
            border-bottom: 1px solid #e0e0e0;
            position: sticky;
            top: 0;
            background-color: #fff;
        }

        #more-menu-close-btn {
            background: none;
            border: none;
            font-size: 1.5em;
            cursor: pointer;
            color: #666;
        }

        #more-menu-list {
            list-style: none;
            padding: 0;
            margin: 0;
        }

        #more-menu-list li a {
            display: flex;
            align-items: center;
            padding: 15px 20px;
            text-decoration: none;
            color: #333;
            transition: background-color 0.2s ease;
        }

        #more-menu-list li a:hover {
            background-color: #f4f4f4;
        }

        #more-menu-list li a i {
            font-size: 1.2em;
            width: 30px;
            color: #555;
            margin-right: 15px;
        }

        /* Overlay for mobile sidebar and modal */
        .overlay {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.5);
            z-index: 1500;
            display: none;
        }
        
        .overlay.show {
            display: block;
        }

        /* Responsive adjustments */
        @media (max-width: 768px) {
            #mobile-footer-nav {
                display: block;
            }
            
            #more-menu-modal {
                display: block;
            }
            
            /* Adjust main content padding to account for fixed footer */
            .content-wrapper {
                padding-bottom: 90px;
            }
            
            /* Hide specific items from sidebar on mobile when footer is active */
            .sidebar nav ul li.hide-on-mobile {
                display: none;
            }
        }

    </style>
</head>
<body>
    <div class="dashboard-container" id="dashboardContainer">
        <aside class="sidebar" id="sidebar">
            <div class="logo">
                <img src="images/logo.png" alt="divine Logo">
                <?php echo $_L['divine_reports']; ?>
            </div>
            <nav>
                <ul>
                    <li class="<?php echo ($currentPage == 'dashboard' ? 'active' : ''); ?>"><a href="student_dashboard.php"><i class="fas fa-th-large"></i> <?php echo $_L['sidebar_dashboard']; ?></a></li>
                    <li class="<?php echo ($currentPage == 'profile' ? 'active' : ''); ?>"><a href="student_profile.php"><i class="fas fa-user-circle"></i> <?php echo $_L['sidebar_profile']; ?></a></li>
                    <li class="<?php echo ($currentPage == 'reports' ? 'active' : ''); ?>"><a href="student_reports.php"><i class="fas fa-file-invoice"></i> <?php echo $_L['sidebar_reports']; ?></a></li>
                    <li class="<?php echo ($currentPage == 'assignments' ? 'active' : ''); ?>"><a href="student_assignments.php"><i class="fas fa-file-alt"></i> <?php echo $_L['sidebar_assignments']; ?></a></li>
                    <li class="<?php echo ($currentPage == 'messages' ? 'active' : ''); ?>"><a href="student_messages.php"><i class="fas fa-envelope"></i> <?php echo $_L['sidebar_messages']; ?></a></li>
                    <li class="<?php echo ($currentPage == 'settings' ? 'active' : ''); ?>"><a href="student_settings.php"><i class="fas fa-cog"></i> <?php echo $_L['sidebar_settings']; ?></a></li>
                    <li><a href="logout.php"><i class="fas fa-sign-out-alt"></i> <?php echo $_L['sidebar_logout']; ?></a></li>
                </ul>
            </nav>
        </aside>

        <div class="main-content">
            <header class="header">
                <button class="menu-toggle" id="menuToggle">
                    <i class="fas fa-bars"></i>
                </button>
                <div class="school-info">
                    <img src="<?php echo $school_logo_path; ?>" alt="<?php echo $school_name; ?> Logo">
                    <h1><?php echo $school_name; ?></h1>
                </div>
                <div class="header-right">
                    <div class="language-select">
                        <select id="languageSelector" onchange="changeLanguage(this.value)">
                           <option value="en" <?php if ($current_lang === 'en') echo 'selected'; ?>>🇬🇧 English</option>
                           <option value="lug" <?php if ($current_lang === 'lug') echo 'selected'; ?>>🇺🇬 Luganda</option>
                           <option value="sw" <?php if ($current_lang === 'sw') echo 'selected'; ?>>🇹🇿 Kiswahili</option>
                        </select>
                    </div>
                    <div class="search-bar">
                        <i class="fas fa-search"></i>
                        <input type="text" placeholder="<?php echo $_L['search_placeholder']; ?>">
                    </div>
                    <div class="notifications">
                        <i class="fas fa-bell"></i>
                        <span class="badge">3</span>
                    </div>
                    <div class="user-profile">
                        <img src="<?php echo $student_photo; ?>" alt="User Photo">
                        <div>
                            <div class="user-name"><?php echo $username; ?></div>
                            <div class="user-email"><?php echo $_L['logged_in_as']; ?> <?php echo $role_name; ?></div>
                        </div>
                        <i class="fas fa-chevron-down"></i>
                    </div>
                </div>
            </header>

            <div class="content-wrapper">
                <div class="container mx-auto p-6 lg:p-8">
                <div class="profile-card">
                    <div class="profile-header">
                        <div class="profile-photo-container">
                            <img src="<?php echo $student_photo; ?>" alt="Student Photo" class="profile-photo" onerror="this.onerror=null;this.src='images/default_student_photo.png';">
                        </div>
                        <h1 class="text-3xl font-bold text-blue-700"><?php echo $_L['profile_heading']; ?></h1>
                    </div>

                    <?php if ($student_profile_data): ?>
                        <div class="space-y-6">
                            <!-- Personal Information -->
                            <div>
                                <h2 class="text-xl font-semibold text-gray-800 mb-4"><?php echo $_L['personal_information']; ?></h2>
                                <div class="info-grid">
                                    <div class="info-item">
                                        <label><?php echo $_L['admission_number']; ?>:</label>
                                        <p><?php echo htmlspecialchars($student_profile_data['ADM'] ?: 'N/A'); ?></p>
                                    </div>
                                    <div class="info-item">
                                        <label><?php echo $_L['username_label']; ?>:</label>
                                        <p><?php echo htmlspecialchars($student_profile_data['username'] ?: 'N/A'); ?></p>
                                    </div>
                                    <div class="info-item">
                                        <label><?php echo $_L['email_label']; ?>:</label>
                                        <p><?php echo htmlspecialchars($student_profile_data['email'] ?: 'N/A'); ?></p>
                                    </div>
                                    <div class="info-item">
                                        <label><?php echo $_L['phone_number_label']; ?>:</label>
                                        <p><?php echo htmlspecialchars($student_profile_data['phone_number'] ?: 'N/A'); ?></p>
                                    </div>
                                    <div class="info-item">
                                        <label><?php echo $_L['status_label']; ?>:</label>
                                        <p>
                                            <span class="px-2 py-1 rounded-full text-xs font-semibold
                                                <?php
                                                    if ($student_profile_data['status'] === 'active') {
                                                        echo 'bg-green-100 text-green-800';
                                                    } elseif ($student_profile_data['status'] === 'inactive') {
                                                        echo 'bg-red-100 text-red-800';
                                                    } elseif ($student_profile_data['status'] === 'suspended') {
                                                        echo 'bg-yellow-100 text-yellow-800';
                                                    } else {
                                                        echo 'bg-gray-100 text-gray-800';
                                                    }
                                                ?>
                                            ">
                                                <?php echo htmlspecialchars(ucfirst($student_profile_data['status'])); ?>
                                            </span>
                                        </p>
                                    </div>
                                </div>
                            </div>

                            <!-- School Information -->
                            <div>
                                <h2 class="text-xl font-semibold text-gray-800 mb-4"><?php echo $_L['school_information']; ?></h2>
                                <div class="info-grid">
                                    <div class="info-item">
                                        <label><?php echo $_L['current_class']; ?>:</label>
                                        <p><?php echo htmlspecialchars($student_profile_data['class_name']); ?></p>
                                    </div>
                                    <div class="info-item">
                                        <label><?php echo $_L['current_stream']; ?>:</label>
                                        <p><?php echo htmlspecialchars($student_profile_data['stream_name']); ?></p>
                                    </div>
                                    <?php 
                                    // Display subject combination only for SENIOR 5 and SENIOR 6 students
                                    $class_name_upper = strtoupper(trim($student_profile_data['class_name']));
                                    $is_senior_5_or_6 = (strpos($class_name_upper, 'SENIOR 5') !== false || 
                                                         strpos($class_name_upper, 'SENIOR 6') !== false ||
                                                         strpos($class_name_upper, 'S5') !== false ||
                                                         strpos($class_name_upper, 'S6') !== false);
                                    
                                    if ($is_senior_5_or_6 && !empty($student_profile_data['subject_combination'])): 
                                    ?>
                                    <div class="info-item">
                                        <label><?php echo $_L['subject_combination']; ?>:</label>
                                        <p><?php echo htmlspecialchars($student_profile_data['subject_combination']); ?></p>
                                    </div>
                                    <?php endif; ?>
                                </div>
                            </div>

                            <!-- Parent/Guardian Information -->
                            <div>
                                <h2 class="text-xl font-semibold text-gray-800 mb-4"><?php echo $_L['parent_guardian_info']; ?></h2>
                                <div class="info-grid">
                                    <div class="info-item">
                                        <label><?php echo $_L['parent_name_label']; ?>:</label>
                                        <p><?php echo htmlspecialchars($student_profile_data['parent_name'] ?: 'N/A'); ?></p>
                                    </div>
                                    <div class="info-item">
                                        <label><?php echo $_L['parent_phone_label']; ?>:</label>
                                        <p><?php echo htmlspecialchars($student_profile_data['parent_phone'] ?: 'N/A'); ?></p>
                                    </div>
                                    <div class="info-item">
                                        <label><?php echo $_L['parent_email_label']; ?>:</label>
                                        <p><?php echo htmlspecialchars($student_profile_data['parent_email'] ?: 'N/A'); ?></p>
                                    </div>
                                </div>
                            </div>
                            <div class="text-right mt-6">
                                <button class="bg-blue-600 hover:bg-blue-700 text-white font-bold py-2.5 px-5 rounded-lg transition duration-150 ease-in-out shadow-md">
                                    <?php echo $_L['update_profile_button']; ?>
                                </button>
                            </div>
                        </div>
                    <?php else: ?>
                        <p class="text-gray-500 italic text-center py-4"><?php echo $_L['no_profile_data']; ?></p>
                    <?php endif; ?>
                </div>
            </div>
            </div>
        </div>
    </div>

    <script>
        function changeLanguage(lang) {
            window.location.href = `?lang=${lang}`;
        }

        document.addEventListener('DOMContentLoaded', function() {
            const menuToggle = document.getElementById('menuToggle');
            const sidebar = document.getElementById('sidebar');
            const dashboardContainer = document.getElementById('dashboardContainer');

            if (menuToggle && sidebar && dashboardContainer) {
                menuToggle.addEventListener('click', function() {
                    sidebar.classList.toggle('active');
                    dashboardContainer.classList.toggle('sidebar-open');
                });

                dashboardContainer.addEventListener('click', function(event) {
                    if (sidebar.classList.contains('active') && !sidebar.contains(event.target) && !menuToggle.contains(event.target)) {
                        sidebar.classList.remove('active');
                        dashboardContainer.classList.remove('sidebar-open');
                    }
                });
            } else {
                console.error("Menu toggle, sidebar, or dashboard container elements for responsive behavior not found.");
            }
        });
    
        // --- START: Mobile Footer Modal Logic ---
        document.addEventListener('DOMContentLoaded', function() {
            const moreMenuButton = document.getElementById('moreMenuButton');
            const moreMenuModal = document.getElementById('more-menu-modal');
            const moreMenuCloseBtn = document.getElementById('more-menu-close-btn');
            const overlay = document.getElementById('overlay');

            function toggleMoreMenu(show) {
                if (show) {
                    moreMenuModal.classList.add('show');
                    overlay.classList.add('show');
                    document.body.style.overflow = 'hidden';
                } else {
                    moreMenuModal.classList.remove('show');
                    overlay.classList.remove('show');
                    document.body.style.overflow = '';
                }
            }

            if (moreMenuButton) {
                moreMenuButton.addEventListener('click', (e) => {
                    e.preventDefault();
                    toggleMoreMenu(true);
                });
            }

            if (moreMenuCloseBtn) {
                moreMenuCloseBtn.addEventListener('click', () => {
                    toggleMoreMenu(false);
                });
            }

            if (overlay) {
                overlay.addEventListener('click', () => {
                    toggleMoreMenu(false);
                });
            }

            // Set active state for mobile footer based on current page
            const currentPage = '<?php echo $currentPage; ?>';
            const mobileNavLinks = document.querySelectorAll('#mobile-footer-nav a[data-section]');
            mobileNavLinks.forEach(link => {
                const section = link.getAttribute('data-section');
                if (section === currentPage) {
                    link.classList.add('active');
                }
            });
        });
        // --- END: Mobile Footer Modal Logic ---

    </script>    <!-- Mobile Footer Navigation -->
    <div id="mobile-footer-nav">
        <ul>
            <li>
                <a href="student_dashboard.php" data-section="dashboard" id="mobileNavDashboard">
                    <i class="fas fa-th-large"></i>
                    <span><?php echo $_L['sidebar_dashboard']; ?></span>
                </a>
            </li>
            <li>
                <a href="student_profile.php" data-section="profile" id="mobileNavProfile">
                    <i class="fas fa-user"></i>
                    <span><?php echo $_L['sidebar_profile']; ?></span>
                </a>
            </li>
            <li>
                <a href="student_messages.php" data-section="messages" id="mobileNavMessages">
                    <i class="fas fa-envelope"></i>
                    <span><?php echo $_L['sidebar_messages']; ?></span>
                </a>
            </li>
            <li>
                <a href="#" id="moreMenuButton" data-section="more">
                    <i class="fas fa-ellipsis-h"></i>
                    <span>More</span>
                </a>
            </li>
        </ul>
    </div>

    <!-- More Menu Modal -->
    <div id="more-menu-modal">
        <div id="more-menu-modal-header">
            <h3>More</h3>
            <button id="more-menu-close-btn"><i class="fas fa-times"></i></button>
        </div>
        <ul id="more-menu-list">
            <li><a href="student_reports.php"><i class="fas fa-file-alt"></i> <?php echo $_L['sidebar_reports']; ?></a></li>
            <li><a href="student_assignments.php"><i class="fas fa-tasks"></i> <?php echo $_L['sidebar_assignments']; ?></a></li>
            <li><a href="student_settings.php"><i class="fas fa-cog"></i> <?php echo $_L['sidebar_settings']; ?></a></li>
            <li class="logout"><a href="logout.php"><i class="fas fa-sign-out-alt"></i> <?php echo $_L['sidebar_logout']; ?></a></li>
        </ul>
    </div>

    <div class="overlay" id="overlay"></div>


</body>
</html>