<?php
require_once('source/session.php');
require_once('source/error.php');
require_once('source/dbconfig.php');
require_once('source/function.php');
require_once('source/restrict_user.php');

// At the top of main.php, ensure session is started and variables exist
if(!isset($_SESSION['SESS_HASH_USER'])) {
    redirect('index.php');
}

// School information
$schoolName = "Henzy Royal International School";
$schoolAddress = "No 17 Amadi Street, Amakohia, Uratta Owerri Imo State Nigeria";
$supportPhone = "+234 803 292 9756";
$supportEmail = "support@hris.com.ng";
$schoolMotto = "Excellence, Integrity, Service";

// Get user data
$rows = sql($DBH, "SELECT * FROM tbl_user WHERE hash = ?", array($_SESSION['SESS_HASH_USER']), "rows");
foreach($rows as $row) {    
    $fullname = $row['fullname'];
    $profile_pic = $row['profile_pic'];
    $user_email = $row['email']; // This is the registration number
    $user_phone = $row['phone'];
    $user_class = $row['class'];
    $user_subclass = $row['subclass'];
    $user_age = $row['age'];
    $user_reg = $row['email']; // Using email field as registration number
}

// Handle messages
$msg = '';
if(isset($_GET['msg']) && $_GET['msg'] == "refered"){
    $msg = "<div class='alert alert-success alert-dismissible fade show' role='alert'>
                <i class='fas fa-check-circle me-2'></i>Referred successfully
                <button type='button' class='btn-close' data-bs-dismiss='alert' aria-label='Close'></button>
            </div>";
}else if(isset($_GET['msg']) && $_GET['msg'] == "failed"){
    $msg = "<div class='alert alert-danger alert-dismissible fade show' role='alert'>
                <i class='fas fa-exclamation-circle me-2'></i>Wrong PIN! Try again or buy a valid pin.
                <button type='button' class='btn-close' data-bs-dismiss='alert' aria-label='Close'></button>
            </div>";
}else if(isset($_GET['msg']) && $_GET['msg'] == "sfailed"){
    $msg = "<div class='alert alert-danger alert-dismissible fade show' role='alert'>
                <i class='fas fa-exclamation-triangle me-2'></i>Wrong PIN! Someone else has already used this PIN
                <button type='button' class='btn-close' data-bs-dismiss='alert' aria-label='Close'></button>
            </div>";
}else if(isset($_GET['msg']) && $_GET['msg'] == "PINfailed"){
    $msg = "<div class='alert alert-warning alert-dismissible fade show' role='alert'>
                <i class='fas fa-lock me-2'></i>Wrong PIN for this Result! You may have used this PIN for a different Session/Term
                <button type='button' class='btn-close' data-bs-dismiss='alert' aria-label='Close'></button>
            </div>";
}

if(isset($_SESSION['msg']) && $_SESSION['msg'] == "update") {
    $msg = "<div class='alert alert-success alert-dismissible fade show' role='alert'>
                <i class='fas fa-check-circle me-2'></i>Order Update Successful. Customer will receive email.
                <button type='button' class='btn-close' data-bs-dismiss='alert' aria-label='Close'></button>
            </div>";
    unset($_SESSION['msg']);
}       

// Calculate age
$age = 'N/A'; // Default value if age can't be calculated
if (!empty($user_age)) {
    if (strpos($user_age, '/') !== false) {
        $birthDate = explode("/", $user_age);
        $month = (int)$birthDate[0];
        $day = (int)$birthDate[1];
        $year = (int)$birthDate[2];
    } elseif (strpos($user_age, '-') !== false) {
        $birthDate = explode("-", $user_age);
        $year = (int)$birthDate[0];
        $month = (int)$birthDate[1];
        $day = (int)$birthDate[2];
    }
    
    if (isset($year) && isset($month) && isset($day)) {
        $currentYear = date('Y');
        $currentMonth = date('m');
        $currentDay = date('d');

        $age = $currentYear - $year;
        
        if ($currentMonth < $month || ($currentMonth == $month && $currentDay < $day)) {
            $age--;
        }
    }
}

// Get last exam class
$last_exam_class = 'N/A';
$rows = sql($DBH, "SELECT class FROM tbl_student_results 
                  WHERE registration_no = ? 
                  ORDER BY id DESC LIMIT 1", 
          array($user_reg), "rows");

if (!empty($rows)) {
    $last_exam_class = $rows[0]['class'];
} elseif (!empty($user_class)) {
    $last_exam_class = $user_class;
}

// Profile picture handling
if($profile_pic == "default" || empty($profile_pic)) {
    $profile_image = "<img class='img-fluid rounded-circle profile-pic' width='120' src='profile_pic/profile.jpg' alt='Profile'>";
} else {
    $profile_image = "<img class='img-fluid rounded-circle profile-pic' width='120' src='../admin/profile_pic/$profile_pic' alt='Profile'>";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Student Dashboard - <?= $schoolName ?></title>
    <link rel="icon" type="image/png" href="../admin/coll_logo.png">
    
    <!-- Bootstrap 5 CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    
    <!-- Font Awesome -->
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.2/css/all.min.css" rel="stylesheet">
    
    <!-- Google Fonts -->
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&family=Playfair+Display:wght@700&display=swap" rel="stylesheet">
    
    <style>
        :root {
            --primary-blue: #1a4b8c;    /* Royal Blue */
            --secondary-gold: #FFD700;    /* Gold */
            --dark-blue: #0f2b4f;         /* Dark Blue */
            --light-blue: #4a7db5;         /* Medium Blue */
            --light-gold: #FFF1B8;          /* Light Gold */
            --bg-light: #f5f9ff;
            --success-green: #28a745;
            --danger-red: #dc3545;
            --warning-orange: #fd7e14;
        }
        
        body {
            font-family: 'Poppins', sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #e9ecef 100%);
            color: #333;
            min-height: 100vh;
            display: flex;
            flex-direction: column;
        }
        
        /* Header Styles */
        .header {
            background: linear-gradient(135deg, var(--primary-blue) 0%, var(--dark-blue) 100%);
            color: white;
            padding: 2rem 0 1.5rem;
            margin-bottom: 2rem;
            box-shadow: 0 4px 20px rgba(26, 75, 140, 0.3);
            border-bottom: 3px solid var(--secondary-gold);
            position: relative;
            overflow: hidden;
        }
        
        .header::before {
            content: '';
            position: absolute;
            top: -50%;
            right: -50%;
            width: 200%;
            height: 200%;
            background: radial-gradient(circle, rgba(255,215,0,0.1) 0%, transparent 70%);
            animation: rotate 20s linear infinite;
        }
        
        @keyframes rotate {
            from { transform: rotate(0deg); }
            to { transform: rotate(360deg); }
        }
        
        .school-name {
            font-family: 'Playfair Display', serif;
            font-weight: 700;
            font-size: 2.2rem;
            margin-bottom: 0.25rem;
            text-shadow: 2px 2px 4px rgba(0,0,0,0.2);
        }
        
        .school-motto {
            font-style: italic;
            color: var(--secondary-gold);
            font-size: 1rem;
            letter-spacing: 1px;
        }
        
        .header-logo {
    width: 70px;
    height: 70px;
    object-fit: contain;
    background: white;
    padding: 8px;
    border-radius: 50%;
    box-shadow: 0 4px 15px rgba(0,0,0,0.2);
    border: 3px solid var(--secondary-gold);
    transition: all 0.3s ease;
        }
        
        .header-logo:hover {
            transform: rotate(360deg) scale(1.1);
            box-shadow: 0 6px 20px rgba(255, 215, 0, 0.4);
        }
        
        @media (max-width: 768px) {
            .header-logo {
                width: 50px;
                height: 50px;
                padding: 5px;
            }
            
            .d-flex.align-items-center.justify-content-center {
                flex-direction: column;
                text-align: center;
            }
            
            .header-logo.me-3 {
                margin-right: 0 !important;
                margin-bottom: 1rem;
            }
        }
        
        /* Navigation */
        .navbar {
            background: linear-gradient(135deg, var(--dark-blue), var(--primary-blue)) !important;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            margin-top: -1rem;
            margin-bottom: 2rem;
            border-radius: 0;
        }
        
        .navbar-nav .nav-link {
            color: white !important;
            font-weight: 500;
            padding: 0.75rem 1.5rem;
            border-radius: 50px;
            transition: all 0.3s ease;
        }
        
        .navbar-nav .nav-link:hover {
            background: rgba(255, 215, 0, 0.2);
            color: var(--secondary-gold) !important;
            transform: translateY(-2px);
        }
        
        .navbar-nav .nav-link i {
            margin-right: 0.5rem;
            color: var(--secondary-gold);
        }
        
        /* Cards */
        .card {
            border: none;
            border-radius: 15px;
            box-shadow: 0 10px 30px rgba(26, 75, 140, 0.1);
            margin-bottom: 1.5rem;
            transition: all 0.3s ease;
            border: 1px solid rgba(255, 215, 0, 0.2);
            overflow: hidden;
        }
        
        .card:hover {
            transform: translateY(-5px);
            box-shadow: 0 15px 40px rgba(26, 75, 140, 0.2);
            border-color: var(--secondary-gold);
        }
        
        .card-header {
            background: linear-gradient(135deg, var(--primary-blue), var(--dark-blue));
            color: white;
            font-weight: 600;
            border-radius: 15px 15px 0 0 !important;
            padding: 1rem 1.5rem;
            border-bottom: 2px solid var(--secondary-gold);
        }
        
        .card-header i {
            color: var(--secondary-gold);
            margin-right: 0.5rem;
        }
        
        .card-body {
            padding: 2rem;
        }
        
        /* Profile Section */
        .profile-pic {
            border: 4px solid var(--secondary-gold);
            box-shadow: 0 4px 15px rgba(26, 75, 140, 0.2);
            transition: all 0.3s ease;
        }
        
        .profile-pic:hover {
            transform: scale(1.05) rotate(5deg);
        }
        
        .student-name {
            font-size: 2rem;
            font-weight: 700;
            color: var(--primary-blue);
            margin-bottom: 1rem;
            position: relative;
            display: inline-block;
        }
        
        .student-name::after {
            content: '';
            position: absolute;
            bottom: -5px;
            left: 0;
            width: 50px;
            height: 3px;
            background: linear-gradient(90deg, var(--secondary-gold), var(--primary-blue));
            border-radius: 3px;
        }
        
        .student-info-label {
            font-weight: 600;
            color: var(--dark-blue);
            font-size: 0.95rem;
        }
        
        .student-info-label i {
            color: var(--secondary-gold);
            width: 20px;
        }
        
        .student-info-value {
            color: #555;
            font-weight: 500;
            padding: 0.25rem 0;
            display: inline-block;
        }
        
        /* Form Styles */
        .form-label {
            font-weight: 600;
            color: var(--dark-blue);
            margin-bottom: 0.5rem;
        }
        
        .form-label i {
            color: var(--secondary-gold);
            margin-right: 0.5rem;
        }
        
        .form-control, .form-select {
            border: 2px solid #e0e0e0;
            border-radius: 10px;
            padding: 0.75rem 1rem;
            font-size: 1rem;
            transition: all 0.3s ease;
        }
        
        .form-control:focus, .form-select:focus {
            border-color: var(--secondary-gold);
            box-shadow: 0 0 0 0.25rem rgba(255, 215, 0, 0.25);
            outline: none;
        }
        
        .form-control[readonly] {
            background: linear-gradient(135deg, #e8f0fe, #d4e4fc);
            border-color: var(--light-blue);
        }
        
        /* Buttons */
        .btn-check-result {
            background: linear-gradient(135deg, var(--success-green), #1e7e34);
            border: none;
            color: white;
            font-weight: 600;
            padding: 1rem 2rem;
            font-size: 1.1rem;
            border-radius: 50px;
            transition: all 0.3s ease;
            position: relative;
            overflow: hidden;
        }
        
        .btn-check-result::before {
            content: '';
            position: absolute;
            top: 50%;
            left: 50%;
            width: 0;
            height: 0;
            border-radius: 50%;
            background: rgba(255, 255, 255, 0.2);
            transform: translate(-50%, -50%);
            transition: width 0.5s, height 0.5s;
        }
        
        .btn-check-result:hover::before {
            width: 300px;
            height: 300px;
        }
        
        .btn-check-result:hover {
            transform: translateY(-3px);
            box-shadow: 0 10px 25px rgba(40, 167, 69, 0.3);
        }
        
        .btn-check-result i {
            margin-right: 0.5rem;
            color: var(--secondary-gold);
        }
        
        /* Alert Styles */
        .alert {
            border-radius: 10px;
            border: none;
            padding: 1rem 1.5rem;
            margin-bottom: 1.5rem;
            border-left: 4px solid transparent;
            animation: slideIn 0.5s ease;
        }
        
        @keyframes slideIn {
            from {
                opacity: 0;
                transform: translateY(-20px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        
        .alert-success {
            background: linear-gradient(135deg, #d4edda, #c3e6cb);
            color: #155724;
            border-left-color: #28a745;
        }
        
        .alert-danger {
            background: linear-gradient(135deg, #f8d7da, #f5c6cb);
            color: #721c24;
            border-left-color: #dc3545;
        }
        
        .alert-warning {
            background: linear-gradient(135deg, #fff3cd, #ffeaa7);
            color: #856404;
            border-left-color: #ffc107;
        }
        
        .alert i {
            margin-right: 0.5rem;
            font-size: 1.1rem;
        }
        
        /* Breadcrumb */
        .breadcrumb {
            background: white;
            border-radius: 50px;
            padding: 0.75rem 1.5rem;
            box-shadow: 0 2px 10px rgba(0,0,0,0.05);
            margin-bottom: 2rem;
            border: 1px solid rgba(255, 215, 0, 0.2);
        }
        
        .breadcrumb-item a {
            color: var(--primary-blue);
            text-decoration: none;
            font-weight: 500;
        }
        
        .breadcrumb-item a:hover {
            color: var(--secondary-gold);
        }
        
        .breadcrumb-item.active {
            color: var(--dark-blue);
        }
        
        /* Result Form Card */
        .result-form-card {
            border-left: 4px solid var(--secondary-gold);
        }
        
        /* Footer */
        .footer {
            background: linear-gradient(135deg, var(--dark-blue), var(--primary-blue));
            color: white;
            padding: 2rem 0;
            margin-top: 3rem;
            border-top: 3px solid var(--secondary-gold);
        }
        
        .footer a {
            color: var(--secondary-gold);
            text-decoration: none;
            font-weight: 500;
            transition: all 0.3s ease;
        }
        
        .footer a:hover {
            color: white;
            text-decoration: underline;
        }
        
        .footer-logo {
            height: 40px;
            margin-bottom: 0.5rem;
            filter: brightness(0) invert(1);
        }
        
        /* Responsive */
        @media (max-width: 768px) {
            .school-name {
                font-size: 1.5rem;
            }
            
            .student-name {
                font-size: 1.5rem;
            }
            
            .card-body {
                padding: 1.5rem;
            }
            
            .btn-check-result {
                width: 100%;
            }
            
            .profile-pic {
                width: 100px;
            }
        }
        
        /* Loading Animation */
        .loading {
            display: inline-block;
            width: 20px;
            height: 20px;
            border: 3px solid rgba(255,255,255,.3);
            border-radius: 50%;
            border-top-color: var(--secondary-gold);
            animation: spin 1s ease-in-out infinite;
        }
        
        @keyframes spin {
            to { transform: rotate(360deg); }
        }
        
        /* Print Styles */
        @media print {
            .no-print {
                display: none !important;
            }
        }
    </style>
</head>
<body>
        <!-- Header -->
        <div class="header text-center no-print">
            <div class="container position-relative">
                <div class="d-flex align-items-center justify-content-center mb-3">
                    <img src="../admin/coll_logo.png" alt="<?= $schoolName ?>" class="header-logo me-3">
                    <div>
                        <h1 class="school-name mb-0"><?= $schoolName ?></h1>
                        <p class="school-motto mb-0">"<?= $schoolMotto ?>"</p>
                    </div>
                </div>
                <h3 class="mb-0">Student Result Dashboard</h3>
            </div>
        </div>

    <!-- Navigation -->
    <nav class="navbar navbar-expand-lg navbar-dark no-print">
        <div class="container">
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav me-auto">
                    <li class="nav-item">
                        <a class="nav-link" href="main.php">
                            <i class="fas fa-home"></i> Dashboard
                        </a>
                    </li>
                </ul>
                <ul class="navbar-nav ms-auto">
                    <li class="nav-item">
                        <a class="nav-link" href="logout.php">
                            <i class="fas fa-sign-out-alt"></i> Logout
                        </a>
                    </li>
                </ul>
            </div>
        </div>
    </nav>

    <!-- Main Content -->
    <div class="container">
        <!-- Breadcrumb -->
        <nav aria-label="breadcrumb" class="no-print">
            <ol class="breadcrumb">
                <li class="breadcrumb-item"><a href="main.php"><i class="fas fa-home me-1"></i>Home</a></li>
                <li class="breadcrumb-item active" aria-current="page">Dashboard</li>
            </ol>
        </nav>

        <?php if (!empty($msg)) echo $msg; ?>
        
        <!-- Student Profile Card -->
        <div class="card">
            <div class="card-header">
                <i class="fas fa-user-graduate"></i> Student Profile
            </div>
            <div class="card-body">
                <div class="row align-items-center">
                    <div class="col-md-2 text-center mb-3 mb-md-0">
                        <?= $profile_image ?>
                    </div>
                    <div class="col-md-10">
                        <h2 class="student-name"><?= htmlspecialchars($fullname) ?></h2>
                        <div class="row">
                            <div class="col-md-4 mb-3">
                                <div class="student-info-label"><i class="fas fa-id-card"></i> Admission No:</div>
                                <div class="student-info-value"><?= htmlspecialchars($user_reg) ?></div>
                            </div>
                            <div class="col-md-4 mb-3">
                                <div class="student-info-label"><i class="fas fa-child"></i> Age:</div>
                                <div class="student-info-value"><?= $age ?> years</div>
                            </div>
                            <div class="col-md-4 mb-3">
                                <div class="student-info-label"><i class="fas fa-graduation-cap"></i> Last Exam Class:</div>
                                <div class="student-info-value"><?= htmlspecialchars($last_exam_class) ?></div>
                            </div>
                            <?php if(!empty($user_phone)): ?>
                            <div class="col-md-4 mb-3">
                                <div class="student-info-label"><i class="fas fa-phone"></i> Phone:</div>
                                <div class="student-info-value"><?= htmlspecialchars($user_phone) ?></div>
                            </div>
                            <?php endif; ?>
                            <?php if(!empty($user_email)): ?>
                            <div class="col-md-4 mb-3">
                                <div class="student-info-label"><i class="fas fa-envelope"></i> Email:</div>
                                <div class="student-info-value"><?= htmlspecialchars($user_email) ?></div>
                            </div>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Result Checker Form -->
        <div class="card result-form-card">
            <div class="card-header">
                <i class="fas fa-poll"></i> Result Checker
            </div>
            <div class="card-body">
                <form action="check_results.php" method="post" enctype="multipart/form-data" id="importFrm">
                    <div class="row g-3">
                        <div class="col-md-6">
                            <label for="registration_no" class="form-label">
                                <i class="fas fa-id-card"></i> Admission Number
                            </label>
                            <input type="text" class="form-control form-control-lg" id="registration_no" 
                                   name="registration_no" value="<?= htmlspecialchars($user_reg) ?>" 
                                   required readonly>
                        </div>
                        
                        <div class="col-md-6">
                            <label for="pin" class="form-label">
                                <i class="fas fa-lock"></i> Enter Student PIN
                            </label>
                            <input type="password" class="form-control form-control-lg" id="pin" 
                                   name="pin" placeholder="Enter your result PIN" required>
                        </div>
                        
                        <div class="col-md-4">
                            <label for="term" class="form-label">
                                <i class="fas fa-calendar-alt"></i> Choose Term
                            </label>
                            <select class="form-select form-select-lg" id="term" name="term" required>
                                <option value="">Select Term</option>
                                <option value="1st Term">1st Term</option>
                                <option value="2nd Term">2nd Term</option>
                                <option value="3rd Term">3rd Term</option>
                            </select>
                        </div>
                        
                        <div class="col-md-4">
                            <label for="mclass" class="form-label">
                                <i class="fas fa-layer-group"></i> Select Class
                            </label>
                            <select class="form-select form-select-lg" id="mclass" name="mclass" required>
                                <option value="">Select Class</option>
                                <?php
                                $rows1 = sql($DBH, "SELECT * FROM tbl_class WHERE id >= ? ORDER BY class", array(1), "rows");                            
                                foreach($rows1 as $row1) {    
                                    $sch_class = $row1['class'];
                                    echo "<option value='$sch_class'>$sch_class</option>";
                                }
                                ?>
                            </select>
                        </div>
                        
                        <div class="col-md-4">
                            <label for="year" class="form-label">
                                <i class="fas fa-calendar"></i> Academic Session
                            </label>
                            <select class="form-select form-select-lg" id="year" name="year" required>
                                <option value="">Select Session</option>
                                <?php
                                $rows1 = sql($DBH, "SELECT * FROM tbl_session WHERE id >= ? ORDER BY session DESC", array(1), "rows");                            
                                foreach($rows1 as $row1) {    
                                    $sch_session = $row1['session'];
                                    echo "<option value='$sch_session'>$sch_session</option>";
                                }
                                ?>
                            </select>
                        </div>
                        
                        <div class="col-12 mt-4">
                            <button type="submit" class="btn btn-check-result btn-lg" name="check">
                                <i class="fas fa-search"></i> CHECK THIS RESULT
                                <span class="loading" style="display: none;"></span>
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </div>

        <!-- Quick Info Card -->
        <div class="card no-print">
            <div class="card-body">
                <div class="row align-items-center">
                    <div class="col-md-8">
                        <h5><i class="fas fa-info-circle" style="color: var(--secondary-gold);"></i> Need Help?</h5>
                        <p class="mb-md-0">Contact the school office or support team for assistance with your results or PIN issues.</p>
                    </div>
                    <div class="col-md-4 text-md-end">
                        <a href="tel:<?= $supportPhone ?>" class="btn btn-outline-primary me-2">
                            <i class="fas fa-phone-alt"></i> Call
                        </a>
                        <a href="mailto:<?= $supportEmail ?>" class="btn btn-outline-primary">
                            <i class="fas fa-envelope"></i> Email
                        </a>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Footer -->
    <footer class="footer text-center no-print">
        <div class="container">
            <div class="mb-3">
                <img src="../admin/coll_logo.png" alt="<?= $schoolName ?>" class="footer-logo">
            </div>
            <p class="mb-2">
                &copy; <?= date('Y'); ?> <?= $schoolName ?>. All Rights Reserved.<br>
                <small><?= $schoolAddress ?></small>
            </p>
            <p class="mb-0">
                <small>Powered by <a href="https://dataswift.com.ng" target="_blank">DataSwift Coders</a></small>
            </p>
        </div>
    </footer>

    <!-- Bootstrap JS -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
    
    <!-- Custom Scripts -->
    <script>
        // Add loading animation on form submit
        document.getElementById('importFrm')?.addEventListener('submit', function(e) {
            const submitBtn = this.querySelector('button[type="submit"]');
            const loadingSpinner = submitBtn.querySelector('.loading');
            const btnText = submitBtn.querySelector('i');
            
            submitBtn.disabled = true;
            if(loadingSpinner) loadingSpinner.style.display = 'inline-block';
            if(btnText) btnText.style.marginRight = '0.5rem';
        });

        // Auto-hide alerts after 5 seconds
        document.addEventListener('DOMContentLoaded', function() {
            const alerts = document.querySelectorAll('.alert');
            alerts.forEach(alert => {
                setTimeout(() => {
                    alert.style.transition = 'all 0.5s ease';
                    alert.style.opacity = '0';
                    alert.style.transform = 'translateY(-20px)';
                    setTimeout(() => alert.remove(), 500);
                }, 5000);
            });
        });

        // Add active class to current nav item
        document.addEventListener('DOMContentLoaded', function() {
            const currentLocation = window.location.pathname;
            const navLinks = document.querySelectorAll('.navbar-nav .nav-link');
            
            navLinks.forEach(link => {
                if(link.getAttribute('href') === 'main.php') {
                    link.classList.add('active');
                }
            });
        });
    </script>
</body>
</html>