<?php
// ==========================
// layout.php for DemoBank
// Professional dashboard layout
// ==========================

if (session_status() === PHP_SESSION_NONE) session_start();

require_once __DIR__ . '/../includes/db.php';
require_once __DIR__ . '/../includes/functions.php';

// Database connection
$conn = $conn ?? db();
if (!$conn) die("Database connection failed");

// Redirect if not logged in
if (!isset($_SESSION['user_id'])) {
    header("Location: /auth/login.php");
    exit;
}

$user_id = $_SESSION['user_id'];

// Fetch user info
$stmt = $conn->prepare("SELECT id, first_name, last_name, email, profile_photo FROM users WHERE id=?");
if (!$stmt) die("Prepare failed: ".$conn->error);
$stmt->bind_param("i", $user_id);
$stmt->execute();
$user = $stmt->get_result()->fetch_assoc();
$stmt->close();

// Fetch user accounts
$stmt = $conn->prepare("SELECT id, account_number, balance, currency FROM accounts WHERE user_id=?");
if (!$stmt) die("Prepare failed: ".$conn->error);
$stmt->bind_param("i", $user_id);
$stmt->execute();
$accounts = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
$stmt->close();
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title><?= htmlspecialchars($title ?? 'DemoBank') ?></title>
<link rel="stylesheet" href="/assets/css/style.css">
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

<style>
* { box-sizing:border-box; margin:0; padding:0; }

/* ✅ FLEX LAYOUT FIX */
body {
    font-family: 'Roboto', sans-serif;
    background:#f4f6f9;
    display: flex;
    flex-direction: column;
    min-height: 100vh;
}

/* Sidebar */
.sidebar {
    width: 250px;
    position: fixed;
    top: 0;
    left: 0;
    height: 100%;
    background: #003366;
    color: #fff;
    padding: 20px;
    overflow-y: auto;
    box-shadow: 2px 0 5px rgba(0, 0, 0, 0.1);
    z-index: 2;
}

.sidebar.collapsed {
    width: 80px;
}

/* Sidebar Header */
.sidebar-header {
    margin-bottom: 30px;
    text-align: center;
}

.sidebar-header h2 {
    font-size: 24px;
    margin-bottom: 10px;
}

.sidebar-header p {
    font-size: 16px;
    margin-bottom: 20px;
    color: #ccc;
}

/* Sidebar Sections */
.sidebar-section h3 {
    font-size: 18px;
    margin-bottom: 15px;
    color: #f0f0f0;
    border-bottom: 1px solid #444;
    padding-bottom: 5px;
}

/* Account Boxes */
.account-box {
    background: #3a4656;
    padding: 15px;
    margin-bottom: 10px;
    border-radius: 8px;
    font-size: 14px;
    color: #f1f1f1;
}

/* Sidebar Links */
.sidebar a {
    display: block;
    padding: 12px;
    text-decoration: none;
    color: #fff;
    font-size: 16px;
    border-radius: 4px;
    margin-bottom: 8px;
}

.sidebar a:hover {
    background: #444;
}

/* Navbar */
.navbar {
    height: 60px;
    background: #003366;
    padding: 0 20px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    position: fixed;
    top: 0; left: 250px; right: 0;
    z-index: 3;
}

.navbar img {
    width: 40px;
    height: 40px;
    border-radius: 50%;
}

.navbar .profile-section {
    display: flex;
    align-items: center;
}

/* ✅ MAIN CONTENT FIX */
.main-content {
    margin-left: 250px;
    padding: 80px 30px 30px 30px;
    flex: 1; /* pushes footer down */
}

/* Cards */
.card {
    background: #fff;
    padding: 20px;
    margin-bottom: 20px;
    border-radius: 8px;
}

/* Table */
table {
    width: 100%;
    border-collapse: collapse;
}

table th, table td {
    padding: 10px;
    border-bottom: 1px solid #ddd;
}

/* ✅ FIXED FOOTER */
footer {
    background: #003366;
    color: #fff;
    padding: 20px;
    text-align: center;
    margin-top: auto; /* key fix */
    width: 100%;
}

/* Responsive */
@media (max-width: 768px) {
    .sidebar {
        width: 200px;
    }

    .navbar {
        left: 200px;
    }

    .main-content {
        margin-left: 200px;
        padding: 70px 15px 15px 15px;
    }
}

@media (max-width: 480px) {
    .main-content {
        margin-left: 0;
    }
}
</style>
</head>

<body>

<!-- Sidebar -->
<?php include __DIR__ . '/sidebar.php'; ?>

<!-- Navbar -->
<?php include __DIR__ . '/navbar.php'; ?>

<!-- Main content -->
<div class="main-content">
    <?= $content ?? "<p>No content to display</p>" ?>
</div>

<!-- Footer -->
<footer>
    <p>&copy; <?= date('Y') ?> Lombard Odier Financial Bank. All rights reserved.</p>
    <p>Thanks for banking with us!</p>
</footer>

<script src="/assets/js/main.js"></script>
</body>
</html>