<?php
session_start();
include 'layouts/dbconn.php';

// Set timezone to East Africa Time
date_default_timezone_set('Africa/Nairobi');

// Get user details if logged in
$user_details = null;
if (isset($_SESSION['user_id'])) {
    $user_id = $_SESSION['user_id'];
    $sql_user_details = "SELECT u.*, ur.role_name FROM users u 
                         INNER JOIN user_roles ur ON u.role_id = ur.role_id 
                         WHERE u.user_id = ?";
    $stmt_user = $conn->prepare($sql_user_details);
    $stmt_user->bind_param("i", $user_id);
    $stmt_user->execute();
    $user_details = $stmt_user->get_result()->fetch_assoc();
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Access Denied - Healthcare EMR System</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta content="Healthcare EMR System" name="description">
    <meta content="Themesbrand" name="author">
    
    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    
    <style>
        body {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
            height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            margin: 0;
        }
        
        .error-container {
            background: white;
            border-radius: 20px;
            padding: 3rem;
            box-shadow: 0 20px 60px rgba(0, 0, 0, 0.15);
            text-align: center;
            max-width: 500px;
            width: 90%;
        }
        
        .error-icon {
            font-size: 5rem;
            color: #ef4444;
            margin-bottom: 1.5rem;
            display: block;
        }
        
        .error-title {
            color: #1f2937;
            font-size: 2rem;
            font-weight: 700;
            margin-bottom: 1rem;
        }
        
        .error-message {
            color: #6b7280;
            font-size: 1.1rem;
            margin-bottom: 2rem;
            line-height: 1.6;
        }
        
        .user-info {
            background: #f8fafc;
            border-radius: 10px;
            padding: 1rem;
            margin-bottom: 2rem;
            border-left: 4px solid #ef4444;
        }
        
        .user-info-title {
            font-weight: 600;
            color: #374151;
            margin-bottom: 0.5rem;
        }
        
        .user-info-detail {
            color: #6b7280;
            font-size: 0.9rem;
        }
        
        .btn {
            padding: 0.875rem 2rem;
            border-radius: 10px;
            font-weight: 600;
            text-decoration: none;
            display: inline-block;
            margin: 0.5rem;
            transition: all 0.3s ease;
            border: none;
            cursor: pointer;
        }
        
        .btn-primary {
            background: #3b82f6;
            color: white;
        }
        
        .btn-primary:hover {
            background: #2563eb;
            color: white;
            transform: translateY(-2px);
            box-shadow: 0 10px 30px rgba(59, 130, 246, 0.3);
        }
        
        .btn-secondary {
            background: #6b7280;
            color: white;
        }
        
        .btn-secondary:hover {
            background: #4b5563;
            color: white;
            transform: translateY(-2px);
            box-shadow: 0 10px 30px rgba(107, 114, 128, 0.3);
        }
        
        .actions {
            margin-top: 1.5rem;
        }
        
        .contact-info {
            margin-top: 2rem;
            padding-top: 2rem;
            border-top: 1px solid #e5e7eb;
            color: #6b7280;
            font-size: 0.9rem;
        }
    </style>
</head>

<body>
    <div class="error-container">
        <div class="error-icon">🚫</div>
        
        <h1 class="error-title">Access Denied</h1>
        
        <p class="error-message">
            You don't have permission to access this page. Your current role doesn't include access to the requested resource.
        </p>
        
        <?php if ($user_details): ?>
        <div class="user-info">
            <div class="user-info-title">Current User Information:</div>
            <div class="user-info-detail">
                <strong>Name:</strong> <?php echo htmlspecialchars($user_details['full_name']); ?><br>
                <strong>Role:</strong> <?php echo htmlspecialchars($user_details['role_name']); ?><br>
                <strong>Username:</strong> <?php echo htmlspecialchars($user_details['username']); ?>
            </div>
        </div>
        <?php endif; ?>
        
        <div class="actions">
            <a href="index.php" class="btn btn-primary">
                <i class="ph-house"></i> Return to Dashboard
            </a>
            
            <?php if (isset($_SERVER['HTTP_REFERER'])): ?>
                <a href="<?php echo htmlspecialchars($_SERVER['HTTP_REFERER']); ?>" class="btn btn-secondary">
                    <i class="ph-arrow-left"></i> Go Back
                </a>
            <?php endif; ?>
        </div>
        
        <div class="contact-info">
            <p>
                <strong>Need access to this page?</strong><br>
                Contact your system administrator to request the necessary permissions for your role.
            </p>
        </div>
    </div>

    <!-- Phosphor Icons -->
    <script src="https://unpkg.com/@phosphor-icons/web"></script>
</body>
</html>

<?php $conn->close(); ?>