<?php 
session_start();

// Log the logout activity before destroying session
if (isset($_SESSION['user_id'])) {
    include 'layouts/dbconn.php';
    
    $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 {
        // Use correct activity_logs table structure
        $log_sql = "INSERT INTO activity_logs (user_id, action_type, module, reference_id, new_values, ip_address, user_agent) 
                   VALUES (?, 'LOGOUT', 'AUTHENTICATION', ?, ?, ?, ?)";
        $log_stmt = $conn->prepare($log_sql);
        $session_id = session_id();
        $logout_data = json_encode([
            '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("iisss", $_SESSION['user_id'], $_SESSION['user_id'], $logout_data, $ip_address, substr($user_agent, 0, 255));
        $log_stmt->execute();
        $log_stmt->close();
        
        $conn->close();
    } catch (Exception $e) {
        error_log("Logout logging error: " . $e->getMessage());
    }
}

$user_name = $_SESSION['full_name'] ?? 'User';

session_unset();
session_destroy();

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 - Property Management System')); ?>
    <?php include 'layouts/head-css.php'; ?>
    <script src="https://cdn.lordicon.com/lordicon.js"></script>
    
    <style>
        :root {
            --primary-blue: #1e3a8a;
            --primary-blue-light: #3b82f6;
            --primary-blue-dark: #1e40af;
            --accent-blue: #60a5fa;
            --light-blue: #dbeafe;
            --very-light-blue: #eff6ff;
            --property-teal: #0891b2;
            --text-dark: #1f2937;
            --text-muted: #6b7280;
            --white: #ffffff;
            --border-color: #e5e7eb;
        }

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

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

        .bg-primary {
            background: linear-gradient(135deg, #ffffff 0%, #f8faff 30%, #eff6ff 100%) !important;
            border-right: 4px solid var(--primary-blue);
        }

        .btn-primary {
            background: linear-gradient(135deg, var(--primary-blue) 0%, var(--primary-blue-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-blue-dark) 0%, var(--primary-blue) 100%);
            transform: translateY(-2px);
            box-shadow: 0 8px 25px rgba(30, 58, 138, 0.3);
        }

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

        .btn-outline-secondary:hover {
            background-color: var(--light-blue);
            border-color: var(--primary-blue);
            color: var(--primary-blue);
        }

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

        .security-info {
            background: var(--light-blue);
            border: 1px solid rgba(30, 58, 138, 0.2);
            border-radius: 12px;
            padding: 20px;
        }

        .feature-item {
            background: rgba(30, 58, 138, 0.08);
            border: 1px solid rgba(30, 58, 138, 0.15);
            border-radius: 12px;
            padding: 20px;
            margin-bottom: 15px;
        }

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

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

        .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-link {
            transition: all 0.3s ease;
            padding: 10px;
            border-radius: 8px;
        }

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

        .card::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 4px;
            background: linear-gradient(90deg, var(--primary-blue) 0%, var(--property-teal) 100%);
            z-index: 1;
        }
    </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">
                                                    <img src="assets/images/logo.png" alt="Property Management" height="80" class="mb-3">
                                                </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 our Property 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 text-dark">Secure Logout</small>
                                                        <div class="small text-muted 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 text-dark">Data Protected</small>
                                                        <div class="small text-muted 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 text-dark">Activity Logged</small>
                                                        <div class="small text-muted 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:#1e3a8a,secondary:#3b82f6"
                                                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">Property Management System</span>
                                                </p>
                                            </div>
                                        </div>
                                        
                                        <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>
                                        
                                        <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>
                                        
                                        <div class="mt-4">
                                            <div class="security-info">
                                                <h6 class="mb-3 text-primary">
                                                    <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>
                                        
                                        <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@property.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">Property Management System</span>
                            </p>
                        </div>
                    </div>
                </div>
            </div>
        </footer>
    </div>

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

    <script>
    window.history.pushState(null, null, window.location.href);
    window.onpopstate = function(event) {
        window.history.pushState(null, null, window.location.href);
    };

    if ('caches' in window) {
        caches.keys().then(function(names) {
            names.forEach(function(name) {
                caches.delete(name);
            });
        });
    }

    window.addEventListener('beforeunload', function() {
        sessionStorage.clear();
    });
    </script>
</body>
</html>