<?php
session_start();
require_once __DIR__ . '/core/db_connect.php'; // Connects to both RBE and master DBs

// Check if user is logged in
if (isset($_SESSION['user_id'])) {
    $user_id = $_SESSION['user_id'];
    // Fetch RBE role
    $stmt = $conn_rbe->prepare("SELECT role FROM rbe_roles WHERE user_id = ? LIMIT 1");
    $stmt->execute([$user_id]);
    $role = $stmt->fetchColumn();

    if ($role) {
        // Redirect based on role
        switch ($role) {
            case 'admin':
                header("Location: admin-dashboard.php");
                exit;
            case 'landlord':
                header("Location: landlord-dashboard.php");
                exit;
            case 'tenant':
                header("Location: tenant-dashboard.php");
                exit;
            case 'agent':
                header("Location: agent-dashboard.php");
                exit;
            default:
                echo "Unknown role detected. Contact support.";
                exit;
        }
    } else {
        echo "You are logged in but no RBE role assigned. Contact support.";
        exit;
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>RBE Utilities Client Portal</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style>
        body {
            font-family: 'Segoe UI', sans-serif;
            background: #0a1a2f;
            color: #ffffff;
            text-align: center;
            padding: 50px;
            margin: 0;
        }
        .container {
            max-width: 600px;
            margin: auto;
            background: #122c4c;
            padding: 30px 20px;
            border-radius: 16px;
            box-shadow: 0 0 25px rgba(0,0,0,0.5);
        }
        img.logo {
            width: 250px;
            height: auto;
            margin-bottom: 30px;
        }
        h1 {
            color: #7ed957;
            margin-bottom: 10px;
        }
        p {
            font-size: 16px;
            line-height: 1.5;
        }
        a.button {
            display: inline-block;
            background: #7ed957;
            color: #000;
            padding: 12px 24px;
            margin-top: 25px;
            text-decoration: none;
            border-radius: 10px;
            font-weight: 600;
            font-size: 16px;
            transition: background 0.3s ease;
        }
        a.button:hover {
            background: #69c349;
        }
        a.email-link {
            color: #7ed957;
            text-decoration: underline;
        }
    </style>
</head>
<body>
    <nav style="background:#061423; padding: 15px 0; box-shadow: 0 4px 10px rgba(0,0,0,0.3);">
    <div style="max-width: 800px; margin: 0 auto; display: flex; justify-content: center; gap: 30px;">
        <a href="//rbelectrical.enterprises/" style="color: #7ed957; font-weight: bold; text-decoration: none; font-size: 16px;">Home</a>
        <a href="//rbelectrical.enterprises/#about" style="color: #7ed957; font-weight: bold; text-decoration: none; font-size: 16px;">About</a>
        <a href="//rbelectrical.enterprises/#contact" style="color: #7ed957; font-weight: bold; text-decoration: none; font-size: 16px;">Contact</a>
    </div>
</nav>
    <div class="container">
        <img src="https://rbelectrical.enterprises/wp-content/uploads/2024/07/RBE-Electrical-Logo-clear2-250x125.png" alt="RBE Logo" class="logo">
        <h1>Welcome to RBE Client Portal</h1>
        <p>Your RBE utilities management platform</p>
        <a href="login.php" class="button">Login to Your Account</a>
        <p style="margin-top: 25px;">Need tech help? Email us at <a href="mailto:support@rbelectrical.enterprises" class="email-link">support@rbelectrical.enterprises</a></p>
    </div>
</body>
</html>
