<?php 
session_start();

// Log the logout activity before destroying session
if (isset($_SESSION['user_id'])) {
    include 'layouts/dbconn.php';
    
    // Get client information for logging
    $ip_address = $_SERVER['REMOTE_ADDR'];
    if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $ip_address = $_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    $user_agent = $_SERVER['HTTP_USER_AGENT'] ?? '';
    
    try {
        // Log logout activity (matching exact activity_log schema)
        $log_sql = "INSERT INTO activity_log (user_id, action, module, record_id, record_type, new_values, ip_address, user_agent, session_id) 
                   VALUES (?, 'LOGOUT', 'AUTH', ?, 'USER', ?, ?, ?, ?)";
        $log_stmt = $conn->prepare($log_sql);
        $session_id = session_id();
        $logout_data = json_encode(array(
            'logout_time' => date('Y-m-d H:i:s'),
            'session_duration' => isset($_SESSION['login_time']) ? (time() - $_SESSION['login_time']) : 0,
            'ip_address' => $ip_address
        ));
        $log_stmt->bind_param("iissss", $_SESSION['user_id'], $_SESSION['user_id'], $logout_data, $ip_address, $user_agent, $session_id);
        $log_stmt->execute();
        $log_stmt->close();
        
        $conn->close();
    } catch (Exception $e) {
        error_log("Logout logging error: " . $e->getMessage());
    }
}

// Store user name for display before destroying session
$user_name = $_SESSION['full_name'] ?? 'User';

session_unset();
session_destroy();

// Clear session cookie
if (ini_get("session.use_cookies")) {
    $params = session_get_cookie_params();
    setcookie(session_name(), '', time() - 42000,
        $params["path"], $params["domain"],
        $params["secure"], $params["httponly"]
    );
}

?>
<?php include 'layouts/main.php'; ?>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Logged Out - Prodigy ENT Hospital Management System')); ?>
    <?php include 'layouts/head-css.php'; ?>
    <script src="https://cdn.lordicon.com/lordicon.js"></script>
    
    <style>
        :root {
            --primary-green: #2d7a2d;
            --primary-green-light: #4caf50;
            --primary-green-dark: #1b5e20;
            --accent-green: #81c784;
            --success-green: #66bb6a;
            --light-green: #e8f5e8;
            --very-light-green: #f1f8f1;
            --medical-teal: #26a69a;
            --medical-teal-light: #4db6ac;
            --text-dark: #2e2e2e;
            --text-muted: #6c757d;
            --white: #ffffff;
            --light-gray: #f8f9fa;
            --border-color: #dee2e6;
        }

        body {
            background: linear-gradient(135deg, var(--very-light-green) 0%, var(--light-green) 100%);
            min-height: 100vh;
        }

        .auth-page-wrapper {
            background: linear-gradient(135deg, var(--very-light-green) 0%, var(--light-green) 100%);
        }

        .card {
            border: none;
            box-shadow: 0 10px 30px rgba(45, 122, 45, 0.1);
            border-radius: 16px;
            overflow: hidden;
        }

        /* Left side - White background with green accents matching login */
        .bg-primary {
            background: linear-gradient(135deg, #ffffff 0%, #f8fffe 30%, #f0fdf4 100%) !important;
            border-right: 4px solid var(--primary-green);
        }

        /* Button styling matching login */
        .btn-primary {
            background: linear-gradient(135deg, var(--primary-green) 0%, var(--primary-green-light) 100%);
            border: none;
            border-radius: 8px;
            padding: 12px 24px;
            font-weight: 600;
            transition: all 0.3s ease;
        }

        .btn-primary:hover {
            background: linear-gradient(135deg, var(--primary-green-dark) 0%, var(--primary-green) 100%);
            transform: translateY(-2px);
            box-shadow: 0 8px 25px rgba(45, 122, 45, 0.3);
        }

        .btn-outline-secondary {
            border: 2px solid var(--primary-green);
            color: var(--primary-green);
            background: transparent;
            border-radius: 8px;
            padding: 10px 24px;
            font-weight: 600;
            transition: all 0.3s ease;
        }

        .btn-outline-secondary:hover {
            background-color: var(--light-green);
            border-color: var(--primary-green);
            color: var(--primary-green);
            transform: translateY(-2px);
        }

        /* Text colors */
        .text-primary {
            color: var(--primary-green) !important;
        }

        .text-info {
            color: var(--medical-teal) !important;
        }

        /* Logo container enhancements */
        .logo-accent {
            position: absolute;
            top: -10px;
            left: -10px;
            right: -10px;
            bottom: -10px;
            background: linear-gradient(135deg, var(--light-green), var(--very-light-green));
            border-radius: 50%;
            z-index: -1;
            opacity: 0.3;
        }

        /* Security info box matching login's info boxes */
        .security-info {
            background: var(--light-green);
            border: 1px solid rgba(45, 122, 45, 0.2);
            border-radius: 12px;
            padding: 20px;
        }

        .security-info h6 {
            color: var(--primary-green);
            font-weight: 600;
        }

        /* Time display */
        .time-display {
            font-family: 'Courier New', monospace;
            font-size: 0.9rem;
            color: var(--text-dark);
            background: var(--very-light-green);
            padding: 10px 16px;
            border-radius: 8px;
            display: inline-block;
            border: 1px solid var(--border-color);
        }

        /* Feature items in left panel */
        .feature-item {
            background: rgba(45, 122, 45, 0.08);
            border: 1px solid rgba(45, 122, 45, 0.15);
            border-radius: 12px;
            padding: 20px;
            margin-bottom: 15px;
            transition: all 0.3s ease;
        }

        .feature-item:hover {
            transform: translateY(-3px);
            background: rgba(45, 122, 45, 0.12);
            box-shadow: 0 4px 12px rgba(45, 122, 45, 0.15);
        }

        .feature-item i {
            color: var(--primary-green);
        }

        .feature-item small {
            color: var(--text-dark);
            font-weight: 500;
        }

        .feature-item .small {
            color: var(--text-muted);
        }

        /* Animation */
        .logout-animation {
            animation: bounceIn 1s ease-out;
        }

        @keyframes bounceIn {
            0% {
                opacity: 0;
                transform: scale(0.3);
            }
            50% {
                opacity: 1;
                transform: scale(1.05);
            }
            70% {
                transform: scale(0.9);
            }
            100% {
                opacity: 1;
                transform: scale(1);
            }
        }

        /* Quick access links */
        .quick-link {
            transition: all 0.3s ease;
            padding: 10px;
            border-radius: 8px;
        }

        .quick-link:hover {
            background: var(--light-green);
        }

        .quick-link i {
            color: var(--primary-green);
        }

        /* Footer styling */
        .footer {
            background: transparent;
        }

        .footer .text-primary {
            color: var(--primary-green) !important;
        }

        /* Enhanced visual elements */
        .card::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 4px;
            background: linear-gradient(90deg, var(--primary-green) 0%, var(--medical-teal) 100%);
            z-index: 1;
        }

        /* Responsive adjustments */
        @media (max-width: 768px) {
            .card {
                margin: 15px;
            }
            
            .p-lg-5 {
                padding: 2rem !important;
            }
            
            .btn-primary, .btn-outline-secondary {
                width: 100%;
                margin-bottom: 10px;
            }
        }
    </style>
</head>

<body>
    <div class="auth-page-wrapper py-5 d-flex justify-content-center align-items-center min-vh-100">
        <div class="auth-page-content overflow-hidden pt-lg-5">
            <div class="container">
                <div class="row">
                    <div class="col-lg-12">
                        <div class="card overflow-hidden border-0 shadow-lg">
                            <div class="row g-0">
                                <div class="col-lg-6">
                                    <div class="p-lg-5 p-4 h-100 bg-primary bg-gradient">
                                        <div class="position-relative h-100 d-flex flex-column align-items-center justify-content-center text-center">
                                            <div class="mb-4">
                                                <a href="index.php" class="d-block position-relative">
                                                    <div class="logo-accent"></div>
                                                    <img src="assets/images/prodigy.png" alt="ENT Hospital" height="80" class="mb-3 position-relative">
                                                </a>
                                                <h4 class="text-dark mb-3">Session Ended Securely</h4>
                                                <p class="text-muted mb-4">Your session has been safely terminated. Thank you for using Prodigy ENT Hospital Management System.</p>
                                            </div>
                                            
                                            <div class="row text-center">
                                                <div class="col-12 mb-3">
                                                    <div class="feature-item">
                                                        <i class="ri-shield-check-line h4 mb-2 d-block"></i>
                                                        <small class="d-block">Secure Logout</small>
                                                        <div class="small mt-1">All session data cleared</div>
                                                    </div>
                                                </div>
                                                <div class="col-12 mb-3">
                                                    <div class="feature-item">
                                                        <i class="ri-lock-line h4 mb-2 d-block"></i>
                                                        <small class="d-block">Data Protected</small>
                                                        <div class="small mt-1">Your information is safe</div>
                                                    </div>
                                                </div>
                                                <div class="col-12">
                                                    <div class="feature-item">
                                                        <i class="ri-time-line h4 mb-2 d-block"></i>
                                                        <small class="d-block">Activity Logged</small>
                                                        <div class="small mt-1">Session recorded securely</div>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>

                                <div class="col-lg-6">
                                    <div class="p-lg-5 p-4">
                                        <div class="text-center logout-animation mb-4">
                                            <lord-icon
                                                src="https://cdn.lordicon.com/hzomhqxz.json"
                                                trigger="loop"
                                                colors="primary:#2d7a2d,secondary:#4caf50"
                                                style="width:120px;height:120px">
                                            </lord-icon>
                                            
                                            <div class="mt-4">
                                                <h5 class="text-primary mb-2">Successfully Logged Out</h5>
                                                <p class="text-muted mb-1">
                                                    Goodbye, <strong><?php echo htmlspecialchars($user_name); ?></strong>!
                                                </p>
                                                <p class="text-muted mb-4">
                                                    Thank you for using <span class="fw-semibold text-primary">Prodigy ENT Hospital</span>
                                                </p>
                                            </div>
                                        </div>
                                        
                                        <!-- Current time display -->
                                        <div class="text-center mb-4">
                                            <div class="time-display">
                                                <i class="ri-time-line me-2"></i>
                                                <?php echo date('F j, Y \a\t g:i A'); ?>
                                            </div>
                                        </div>
                                        
                                        <!-- Action buttons -->
                                        <div class="mt-4">
                                            <button class="btn btn-primary w-100 mb-3" onclick="window.location.href='login.php'">
                                                <i class="ri-login-box-line me-2"></i>Sign In Again
                                            </button>
                                            <button class="btn btn-outline-secondary w-100" onclick="window.location.href='index.php'">
                                                <i class="ri-home-line me-2"></i>Go to Homepage
                                            </button>
                                        </div>
                                        
                                        <!-- Security information -->
                                        <div class="mt-4">
                                            <div class="security-info">
                                                <h6 class="mb-3">
                                                    <i class="ri-information-line me-2"></i>Security Notice
                                                </h6>
                                                <ul class="text-muted small mb-0 ps-3">
                                                    <li>Your session has been completely terminated</li>
                                                    <li>All temporary data has been cleared</li>
                                                    <li>For security, close all browser windows</li>
                                                    <li>Use private mode on shared computers</li>
                                                </ul>
                                            </div>
                                        </div>
                                        
                                        <!-- Quick access links -->
                                        <div class="mt-4">
                                            <div class="row text-center">
                                                <div class="col-4">
                                                    <a href="login.php" class="text-decoration-none text-muted quick-link d-block">
                                                        <i class="ri-login-circle-line d-block mb-1 h5"></i>
                                                        <small>Login</small>
                                                    </a>
                                                </div>
                                                <div class="col-4">
                                                    <a href="forgot_password.php" class="text-decoration-none text-muted quick-link d-block">
                                                        <i class="ri-lock-unlock-line d-block mb-1 h5"></i>
                                                        <small>Reset Password</small>
                                                    </a>
                                                </div>
                                                <div class="col-4">
                                                    <a href="mailto:support@enthospital.co.ke" class="text-decoration-none text-muted quick-link d-block">
                                                        <i class="ri-customer-service-line d-block mb-1 h5"></i>
                                                        <small>Support</small>
                                                    </a>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <footer class="footer">
            <div class="container">
                <div class="row">
                    <div class="col-lg-12">
                        <div class="text-center">
                            <p class="mb-0 text-muted">
                                &copy; <script>document.write(new Date().getFullYear())</script> 
                                <span class="text-primary fw-semibold">Prodigy ENT Hospital</span> - 
                                Professional Healthcare Management System
                            </p>
                        </div>
                    </div>
                </div>
            </div>
        </footer>
    </div>

    <?php include 'layouts/vendor-scripts.php'; ?>

    <script>
    // Prevent back button after logout
    window.history.pushState(null, null, window.location.href);
    window.onpopstate = function(event) {
        window.history.pushState(null, null, window.location.href);
    };

    // Clear any cached data
    if ('caches' in window) {
        caches.keys().then(function(names) {
            names.forEach(function(name) {
                caches.delete(name);
            });
        });
    }

    // Security: Clear any remaining sensitive data
    window.addEventListener('beforeunload', function() {
        sessionStorage.clear();
    });
    </script>
</body>
</html>