<?php
session_start();
require_once '../includes/db.php';
require_once '../includes/functions.php';
require_once '../includes/notification.php';

// Ensure only admin can access this page
redirectIfNotAdmin();

if (!isset($_GET['user_id']) || !is_numeric($_GET['user_id'])) {
    header('Location: manage-users.php');
    exit();
}

$userId = (int) $_GET['user_id'];

// Handle ID verification status update
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['verification_status'])) {
    $newStatus = $_POST['verification_status'];

    if (in_array($newStatus, ['verified', 'rejected'])) {
        try {
            $pdo->beginTransaction();

            // Update ID verification status
            $stmt = $pdo->prepare("UPDATE id_verifications SET verification_status = ?, verified_at = NOW() WHERE user_id = ?");
            $stmt->execute([$newStatus, $userId]);

            // If ID is verified, also approve the user and create account if needed
            if ($newStatus === 'verified') {
                // Check if user is still pending
                $userStmt = $pdo->prepare("SELECT status FROM users WHERE user_id = ?");
                $userStmt->execute([$userId]);
                $userData = $userStmt->fetch();

                if ($userData['status'] === 'pending') {
                    // Update user status to approved
                    $stmt = $pdo->prepare("UPDATE users SET status = 'approved' WHERE user_id = ?");
                    $stmt->execute([$userId]);

                    // Check if account exists, if not create one
                    $accStmt = $pdo->prepare("SELECT COUNT(*) FROM accounts WHERE user_id = ?");
                    $accStmt->execute([$userId]);
                    $hasAccount = $accStmt->fetchColumn();

                    if (!$hasAccount) {
                        $accountNumber = generateUniqueAccountNumber($pdo);
                        $stmt = $pdo->prepare("INSERT INTO accounts (user_id, account_number, balance) VALUES (?, ?, 0)");
                        $stmt->execute([$userId, $accountNumber]);
                    }

                    // Send approval email
                    $userStmt = $pdo->prepare("SELECT email, full_name FROM users WHERE user_id = ?");
                    $userStmt->execute([$userId]);
                    $user = $userStmt->fetch();

                    if ($user) {
                        $subject = "Your Oakshire Finance Account Has Been Approved";
                        $message = "<p>Hi <strong>{$user['full_name']}</strong>,</p>
                                   <p>Your ID verification has been approved and your Oakshire Finance account is now active.</p>
                                   <p>You can now log in and start using our services.</p>
                                   <p>Thank you for joining Oakshire Finance!</p>";

                        sendNotification($user['email'], $subject, $message);
                    }
                }
            } else {
                // Send rejection email
                $userStmt = $pdo->prepare("SELECT email, full_name FROM users WHERE user_id = ?");
                $userStmt->execute([$userId]);
                $user = $userStmt->fetch();

                if ($user) {
                    $subject = "Your ID Verification Was Not Approved";
                    $message = "<p>Hi <strong>{$user['full_name']}</strong>,</p>
                               <p>We regret to inform you that your ID verification was not approved.</p>
                               <p>Please upload a clear image of your valid ID and try again.</p>
                               <p>If you have any questions, please contact our support team.</p>";

                    sendNotification($user['email'], $subject, $message);
                }
            }

            $pdo->commit();
            header("Location: view-id.php?user_id=$userId&success=1");
            exit();

        } catch (Exception $e) {
            $pdo->rollBack();
            error_log("Verification error: " . $e->getMessage());
            header("Location: view-id.php?user_id=$userId&error=1");
            exit();
        }
    }
}

try {
    // Get user and ID verification details
    $stmt = $pdo->prepare("
        SELECT u.*, iv.* 
        FROM users u 
        LEFT JOIN id_verifications iv ON u.user_id = iv.user_id 
        WHERE u.user_id = ?
    ");
    $stmt->execute([$userId]);
    $user = $stmt->fetch();

    if (!$user) {
        header('Location: manage-users.php');
        exit();
    }
} catch (PDOException $e) {
    error_log("Error: " . $e->getMessage());
    header('Location: manage-users.php');
    exit();
}

// Get current admin info
$adminId = $_SESSION['user_id'];
$adminInfo = $pdo->prepare("SELECT full_name, email FROM users WHERE user_id = ?");
$adminInfo->execute([$adminId]);
$admin = $adminInfo->fetch();
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover">
    <title>Oakshire Finance - View ID Verification</title>
    <link
        href="https://fonts.googleapis.com/css2?family=Inter:opsz,wght@14..32,300;14..32,400;14..32,500;14..32,600;14..32,700;14..32,800&display=swap"
        rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        :root {
            --primary-navy: #0a2540;
            --primary-dark: #0a1c2f;
            --primary-blue: #1a4d8c;
            --accent-blue: #2c7da0;
            --success-green: #2e7d32;
            --warning-orange: #f39c12;
            --error-red: #dc3545;
            --info-blue: #17a2b8;
            --gray-50: #f8f9fc;
            --gray-100: #f1f3f9;
            --gray-200: #e9ecef;
            --gray-300: #dee2e6;
            --gray-400: #ced4da;
            --gray-500: #adb5bd;
            --gray-600: #6c757d;
            --gray-700: #495057;
            --gray-800: #343a40;
            --gray-900: #212529;
            --white: #ffffff;
            --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.08);
            --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.05);
            --shadow-lg: 0 8px 24px rgba(0, 0, 0, 0.08);
            --shadow-hover: 0 12px 28px rgba(0, 0, 0, 0.12);
            --transition: all 0.3s ease;
        }

        body {
            font-family: 'Inter', system-ui, -apple-system, sans-serif;
            background: var(--gray-50);
            color: var(--gray-800);
            line-height: 1.5;
            overflow-x: hidden;
        }

        /* Layout */
        .dashboard-wrapper {
            display: flex;
            min-height: 100vh;
        }

        /* Sidebar */
        .sidebar {
            width: 280px;
            background: var(--white);
            border-right: 1px solid var(--gray-200);
            position: fixed;
            left: 0;
            top: 0;
            bottom: 0;
            overflow-y: auto;
            transition: transform 0.3s ease;
            z-index: 100;
        }

        .sidebar-header {
            padding: 24px;
            border-bottom: 1px solid var(--gray-200);
            text-align: center;
        }

        .logo {
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 10px;
            text-decoration: none;
        }

        .logo-icon {
            width: 40px;
            height: 40px;
            background: var(--primary-navy);
            border-radius: 10px;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .logo-icon i {
            font-size: 20px;
            color: white;
        }

        .logo-text {
            font-size: 22px;
            font-weight: 700;
            color: var(--primary-navy);
            letter-spacing: -0.3px;
        }

        .nav-menu {
            padding: 24px 16px;
        }

        .nav-item {
            margin-bottom: 8px;
        }

        .nav-link {
            display: flex;
            align-items: center;
            gap: 12px;
            padding: 12px 16px;
            border-radius: 12px;
            text-decoration: none;
            color: var(--gray-700);
            transition: var(--transition);
            font-weight: 500;
            font-size: 14px;
        }

        .nav-link i {
            width: 20px;
            font-size: 18px;
        }

        .nav-link:hover {
            background: var(--gray-100);
            color: var(--primary-blue);
        }

        .nav-link.active {
            background: var(--primary-navy);
            color: white;
        }

        .nav-link.active i {
            color: white;
        }

        .logout-section {
            padding: 24px 16px;
            border-top: 1px solid var(--gray-200);
            margin-top: auto;
            position: absolute;
            bottom: 0;
            left: 0;
            right: 0;
            background: var(--white);
        }

        .logout-btn {
            display: flex;
            align-items: center;
            gap: 12px;
            padding: 12px 16px;
            border-radius: 12px;
            text-decoration: none;
            color: var(--error-red);
            transition: var(--transition);
            font-weight: 500;
        }

        .logout-btn:hover {
            background: rgba(220, 53, 69, 0.1);
        }

        /* Main Content */
        .main-content {
            flex: 1;
            margin-left: 280px;
            min-height: 100vh;
        }

        /* Top Header */
        .top-header {
            background: var(--white);
            border-bottom: 1px solid var(--gray-200);
            padding: 16px 32px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            position: sticky;
            top: 0;
            z-index: 99;
        }

        .page-title h1 {
            font-size: 24px;
            font-weight: 700;
            color: var(--gray-900);
        }

        .user-menu {
            display: flex;
            align-items: center;
            gap: 24px;
        }

        .notification-btn {
            position: relative;
            background: none;
            border: none;
            font-size: 20px;
            color: var(--gray-600);
            cursor: pointer;
            transition: var(--transition);
        }

        .notification-btn:hover {
            color: var(--primary-blue);
        }

        .user-info {
            display: flex;
            align-items: center;
            gap: 12px;
        }

        .user-avatar {
            width: 40px;
            height: 40px;
            background: linear-gradient(135deg, var(--primary-navy), var(--primary-blue));
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .user-avatar i {
            font-size: 18px;
            color: white;
        }

        .user-details h4 {
            font-size: 14px;
            font-weight: 600;
            color: var(--gray-900);
        }

        .user-details p {
            font-size: 12px;
            color: var(--gray-600);
        }

        /* Content Area */
        .content-area {
            padding: 32px;
        }

        /* Back Link */
        .back-link {
            display: inline-flex;
            align-items: center;
            gap: 8px;
            color: var(--primary-blue);
            text-decoration: none;
            font-weight: 500;
            margin-bottom: 24px;
            transition: var(--transition);
        }

        .back-link:hover {
            gap: 12px;
            color: var(--primary-navy);
        }

        /* ID Container */
        .id-container {
            background: var(--white);
            border-radius: 24px;
            border: 1px solid var(--gray-200);
            overflow: hidden;
        }

        /* Info Section */
        .info-section {
            padding: 24px;
            border-bottom: 1px solid var(--gray-200);
        }

        .section-title {
            font-size: 18px;
            font-weight: 600;
            margin-bottom: 20px;
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .info-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
            gap: 20px;
        }

        .info-item {
            display: flex;
            flex-direction: column;
        }

        .info-label {
            font-size: 12px;
            font-weight: 500;
            color: var(--gray-600);
            text-transform: uppercase;
            letter-spacing: 0.5px;
            margin-bottom: 4px;
        }

        .info-value {
            font-size: 15px;
            font-weight: 500;
            color: var(--gray-900);
        }

        /* Status Badge */
        .status-badge {
            display: inline-flex;
            align-items: center;
            gap: 6px;
            padding: 6px 14px;
            border-radius: 30px;
            font-size: 12px;
            font-weight: 600;
        }

        .status-pending {
            background: rgba(243, 156, 18, 0.1);
            color: var(--warning-orange);
        }

        .status-verified {
            background: rgba(46, 125, 50, 0.1);
            color: var(--success-green);
        }

        .status-rejected {
            background: rgba(220, 53, 69, 0.1);
            color: var(--error-red);
        }

        /* Document Section */
        .document-section {
            padding: 24px;
            border-bottom: 1px solid var(--gray-200);
        }

        .document-preview {
            background: var(--gray-50);
            border-radius: 16px;
            padding: 24px;
            text-align: center;
            margin-top: 16px;
        }

        .document-image {
            max-width: 100%;
            max-height: 500px;
            border-radius: 12px;
            box-shadow: var(--shadow-md);
            cursor: pointer;
            transition: var(--transition);
        }

        .document-image:hover {
            transform: scale(1.02);
            box-shadow: var(--shadow-hover);
        }

        .pdf-link {
            display: inline-flex;
            align-items: center;
            gap: 10px;
            padding: 12px 24px;
            background: var(--primary-navy);
            color: white;
            text-decoration: none;
            border-radius: 12px;
            transition: var(--transition);
        }

        .pdf-link:hover {
            background: var(--primary-dark);
            transform: translateY(-2px);
        }

        /* Verification Actions */
        .verification-actions {
            background: var(--white);
            border-radius: 24px;
            border: 1px solid var(--gray-200);
            margin-top: 24px;
            padding: 24px;
            display: flex;
            gap: 16px;
            justify-content: center;
            flex-wrap: wrap;
        }

        .btn {
            display: inline-flex;
            align-items: center;
            gap: 8px;
            padding: 12px 24px;
            border-radius: 12px;
            font-weight: 600;
            font-size: 14px;
            cursor: pointer;
            transition: var(--transition);
            border: none;
            text-decoration: none;
        }

        .btn-success {
            background: rgba(46, 125, 50, 0.1);
            color: var(--success-green);
            border: 1px solid var(--success-green);
        }

        .btn-success:hover {
            background: var(--success-green);
            color: white;
            transform: translateY(-2px);
        }

        .btn-danger {
            background: rgba(220, 53, 69, 0.1);
            color: var(--error-red);
            border: 1px solid var(--error-red);
        }

        .btn-danger:hover {
            background: var(--error-red);
            color: white;
            transform: translateY(-2px);
        }

        /* Alert */
        .alert {
            padding: 14px 16px;
            border-radius: 12px;
            margin-bottom: 24px;
            display: flex;
            align-items: center;
            gap: 12px;
            font-size: 14px;
        }

        .alert-success {
            background: rgba(46, 125, 50, 0.1);
            border-left: 3px solid var(--success-green);
            color: var(--success-green);
        }

        .alert-danger {
            background: rgba(220, 53, 69, 0.1);
            border-left: 3px solid var(--error-red);
            color: var(--error-red);
        }

        /* Modal */
        .modal {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: rgba(0, 0, 0, 0.9);
            z-index: 1000;
            align-items: center;
            justify-content: center;
        }

        .modal.active {
            display: flex;
        }

        .modal-content {
            max-width: 90%;
            max-height: 90%;
            position: relative;
        }

        .modal-content img {
            max-width: 100%;
            max-height: 90vh;
            border-radius: 8px;
        }

        .modal-close {
            position: absolute;
            top: -40px;
            right: 0;
            background: none;
            border: none;
            color: white;
            font-size: 32px;
            cursor: pointer;
            transition: var(--transition);
        }

        .modal-close:hover {
            color: var(--gray-400);
        }

        /* Hamburger Menu */
        .hamburger {
            display: none;
            background: none;
            border: none;
            font-size: 24px;
            cursor: pointer;
            color: var(--gray-700);
        }

        /* Mobile Responsive */
        @media (max-width: 1024px) {
            .sidebar {
                transform: translateX(-100%);
                z-index: 1000;
            }

            .sidebar.mobile-open {
                transform: translateX(0);
            }

            .main-content {
                margin-left: 0;
            }

            .hamburger {
                display: block;
            }
        }

        @media (max-width: 768px) {
            .content-area {
                padding: 20px;
            }

            .top-header {
                padding: 12px 20px;
            }

            .page-title h1 {
                font-size: 20px;
            }

            .user-details {
                display: none;
            }

            .info-grid {
                grid-template-columns: 1fr;
                gap: 16px;
            }

            .info-section {
                padding: 20px;
            }

            .document-section {
                padding: 20px;
            }

            .document-preview {
                padding: 16px;
            }

            .verification-actions {
                flex-direction: column;
            }

            .btn {
                justify-content: center;
            }
        }
    </style>
    <link rel="stylesheet" href="../assets/css/nexus-theme.css">
</head>

<body>
    <div class="dashboard-wrapper">
        <!-- Sidebar Navigation -->
        <aside class="sidebar" id="sidebar">
            <div class="sidebar-header">
                <a href="dashboard.php" class="logo">
                    <div class="logo-icon">
                        <i class="fas fa-building-columns"></i>
                    </div>
                    <span class="logo-text">Oakshire Finance</span>
                </a>
            </div>

            <nav class="nav-menu">
                <div class="nav-item">
                    <a href="dashboard.php" class="nav-link">
                        <i class="fas fa-chart-line"></i>
                        <span>Dashboard</span>
                    </a>
                </div>
                <div class="nav-item">
                    <a href="manage-users.php" class="nav-link active">
                        <i class="fas fa-users"></i>
                        <span>Manage Users</span>
                    </a>
                </div>
                <div class="nav-item">
                    <a href="manage-loans.php" class="nav-link">
                        <i class="fas fa-hand-holding-usd"></i>
                        <span>Manage Loans</span>
                    </a>
                </div>
                <div class="nav-item">
                    <a href="manage-investments.php" class="nav-link">
                        <i class="fas fa-chart-pie"></i>
                        <span>Manage Investments</span>
                    </a>
                </div>
                <div class="nav-item">
                    <a href="track-investments.php" class="nav-link">
                        <i class="fas fa-chart-simple"></i>
                        <span>Users Investments</span>
                    </a>
                </div>
                <div class="nav-item">
                    <a href="role.php" class="nav-link">
                        <i class="fas fa-user-tag"></i>
                        <span>Roles</span>
                    </a>
                </div>
                <div class="nav-item">
                    <a href="recent_transactions.php" class="nav-link">
                        <i class="fas fa-exchange-alt"></i>
                        <span>Transactions</span>
                    </a>
                </div>
                <div class="nav-item">
                    <a href="loan-history.php" class="nav-link">
                        <i class="fas fa-history"></i>
                        <span>Loan History</span>
                    </a>
                </div>
                <div class="nav-item">
                    <a href="login-records.php" class="nav-link">
                        <i class="fas fa-sign-in-alt"></i>
                        <span>Login Records</span>
                    </a>
                </div>
                <div class="nav-item">
                    <a href="manage-messages.php" class="nav-link">
                        <i class="fas fa-envelope"></i>
                        <span>Contact Messages</span>
                    </a>
                </div>
            </nav>

            <div class="logout-section">
                <a href="../logout.php" class="logout-btn">
                    <i class="fas fa-sign-out-alt"></i>
                    <span>Logout</span>
                </a>
            </div>
        </aside>

        <!-- Main Content -->
        <main class="main-content">
            <!-- Top Header -->
            <header class="top-header">
                <button class="hamburger" id="hamburger">
                    <i class="fas fa-bars"></i>
                </button>
                <div class="page-title">
                    <h1>ID Verification</h1>
                </div>
                <div class="user-menu">
                    <button class="notification-btn">
                        <i class="fas fa-bell"></i>
                    </button>
                    <div class="user-info">
                        <div class="user-avatar">
                            <i class="fas fa-user-shield"></i>
                        </div>
                        <div class="user-details">
                            <h4><?= htmlspecialchars($admin['full_name'] ?? 'Admin User') ?></h4>
                            <p>Administrator</p>
                        </div>
                    </div>
                </div>
            </header>

            <!-- Content Area -->
            <div class="content-area">
                <!-- Back Link -->
                <a href="manage-users.php" class="back-link">
                    <i class="fas fa-arrow-left"></i> Back to Users
                </a>

                <!-- Success/Error Alerts -->
                <?php if (isset($_GET['success'])): ?>
                    <div class="alert alert-success">
                        <i class="fas fa-check-circle"></i>
                        <span>ID verification status updated successfully. The user has been notified.</span>
                    </div>
                <?php endif; ?>

                <?php if (isset($_GET['error'])): ?>
                    <div class="alert alert-danger">
                        <i class="fas fa-exclamation-circle"></i>
                        <span>Failed to update verification status. Please try again.</span>
                    </div>
                <?php endif; ?>

                <!-- ID Container -->
                <div class="id-container">
                    <!-- User Information -->
                    <div class="info-section">
                        <div class="section-title">
                            <i class="fas fa-user-circle"></i>
                            <span>User Information</span>
                        </div>
                        <div class="info-grid">
                            <div class="info-item">
                                <span class="info-label">Full Name</span>
                                <span class="info-value"><?= htmlspecialchars($user['full_name']) ?></span>
                            </div>
                            <div class="info-item">
                                <span class="info-label">Email Address</span>
                                <span class="info-value"><?= htmlspecialchars($user['email']) ?></span>
                            </div>
                            <div class="info-item">
                                <span class="info-label">Phone Number</span>
                                <span class="info-value"><?= htmlspecialchars($user['phone']) ?></span>
                            </div>
                            <div class="info-item">
                                <span class="info-label">Address</span>
                                <span class="info-value"><?= htmlspecialchars($user['address']) ?></span>
                            </div>
                            <div class="info-item">
                                <span class="info-label">Occupation</span>
                                <span class="info-value"><?= htmlspecialchars($user['occupation']) ?></span>
                            </div>
                        </div>
                    </div>

                    <!-- ID Verification Details -->
                    <div class="info-section">
                        <div class="section-title">
                            <i class="fas fa-id-card"></i>
                            <span>ID Verification Details</span>
                        </div>
                        <div class="info-grid">
                            <div class="info-item">
                                <span class="info-label">ID Type</span>
                                <span
                                    class="info-value"><?= ucwords(str_replace('_', ' ', $user['id_type'] ?? 'N/A')) ?></span>
                            </div>
                            <div class="info-item">
                                <span class="info-label">Verification Status</span>
                                <span class="info-value">
                                    <?php
                                    $status = $user['verification_status'] ?? 'pending';
                                    $statusClass = '';
                                    if ($status === 'pending')
                                        $statusClass = 'status-pending';
                                    elseif ($status === 'verified')
                                        $statusClass = 'status-verified';
                                    else
                                        $statusClass = 'status-rejected';
                                    ?>
                                    <span class="status-badge <?= $statusClass ?>">
                                        <i
                                            class="fas <?= $status === 'pending' ? 'fa-clock' : ($status === 'verified' ? 'fa-check-circle' : 'fa-times-circle') ?>"></i>
                                        <?= ucfirst($status) ?>
                                    </span>
                                </span>
                            </div>
                            <div class="info-item">
                                <span class="info-label">Uploaded On</span>
                                <span
                                    class="info-value"><?= date('M j, Y g:i A', strtotime($user['created_at'])) ?></span>
                            </div>
                        </div>
                    </div>

                    <!-- ID Document -->
                    <div class="document-section">
                        <div class="section-title">
                            <i class="fas fa-file-alt"></i>
                            <span>ID Document</span>
                        </div>
                        <div class="document-preview">
                            <?php if (!empty($user['id_file_path'])):
                                $fileExtension = strtolower(pathinfo($user['id_file_path'], PATHINFO_EXTENSION));
                                if (in_array($fileExtension, ['jpg', 'jpeg', 'png'])): ?>
                                    <img src="../<?= htmlspecialchars($user['id_file_path']) ?>" alt="ID Document"
                                        class="document-image" id="documentImage" onclick="openModal()">
                                <?php else: ?>
                                    <a href="../<?= htmlspecialchars($user['id_file_path']) ?>" target="_blank"
                                        class="pdf-link">
                                        <i class="fas fa-file-pdf"></i>
                                        View PDF Document
                                        <i class="fas fa-external-link-alt"></i>
                                    </a>
                                <?php endif; ?>
                            <?php else: ?>
                                <div style="text-align: center; padding: 40px; color: var(--gray-500);">
                                    <i class="fas fa-file-image"
                                        style="font-size: 48px; margin-bottom: 16px; opacity: 0.5;"></i>
                                    <p>No ID document uploaded</p>
                                </div>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>

                <!-- Verification Actions (Only show if not already verified) -->
                <?php if (($user['verification_status'] ?? 'pending') !== 'verified'): ?>
                    <div class="verification-actions">
                        <form method="POST" style="display: inline;">
                            <input type="hidden" name="verification_status" value="verified">
                            <button type="submit" class="btn btn-success"
                                onclick="return confirm('Verify this ID and approve the user? This will also create their account if needed.')">
                                <i class="fas fa-check-circle"></i> Verify & Approve User
                            </button>
                        </form>
                        <form method="POST" style="display: inline;">
                            <input type="hidden" name="verification_status" value="rejected">
                            <button type="submit" class="btn btn-danger"
                                onclick="return confirm('Reject this ID? The user will need to re-upload their ID document.')">
                                <i class="fas fa-times-circle"></i> Reject ID
                            </button>
                        </form>
                    </div>
                <?php endif; ?>
            </div>
        </main>
    </div>

    <!-- Image Modal -->
    <div id="imageModal" class="modal">
        <div class="modal-content">
            <button class="modal-close" onclick="closeModal()">&times;</button>
            <img id="modalImage" src="" alt="ID Document">
        </div>
    </div>

    <script>
        // Mobile sidebar toggle
        const hamburger = document.getElementById('hamburger');
        const sidebar = document.getElementById('sidebar');

        if (hamburger) {
            hamburger.addEventListener('click', function () {
                sidebar.classList.toggle('mobile-open');
            });
        }

        // Close sidebar when clicking outside on mobile
        document.addEventListener('click', function (event) {
            if (window.innerWidth <= 1024) {
                const isClickInside = sidebar.contains(event.target) || hamburger.contains(event.target);
                if (!isClickInside && sidebar.classList.contains('mobile-open')) {
                    sidebar.classList.remove('mobile-open');
                }
            }
        });

        // Modal functionality
        const modal = document.getElementById('imageModal');
        const modalImage = document.getElementById('modalImage');
        const documentImage = document.getElementById('documentImage');

        function openModal() {
            if (documentImage) {
                modalImage.src = documentImage.src;
                modal.classList.add('active');
            }
        }

        function closeModal() {
            modal.classList.remove('active');
        }

        // Close modal when clicking outside
        modal.addEventListener('click', function (e) {
            if (e.target === modal) {
                closeModal();
            }
        });

        // Auto-dismiss alerts after 5 seconds
        const alerts = document.querySelectorAll('.alert');
        alerts.forEach(alert => {
            setTimeout(() => {
                alert.style.transition = 'opacity 0.3s ease';
                alert.style.opacity = '0';
                setTimeout(() => alert.remove(), 300);
            }, 5000);
        });
    </script>
</body>

</html>