<?php
session_start();
include("conexao.php");

if (!isset($_SESSION['usuario_id'])) {
    header("Location: index.html");
    exit();
}

$usuario_id = $_SESSION['usuario_id'];

// Busca dados pessoais
$sql_pessoais = "SELECT cpf, email, nome, telefone FROM dados_pessoais WHERE usuario_id = ?";
$stmt_pessoais = mysqli_prepare($conexao, $sql_pessoais);
mysqli_stmt_bind_param($stmt_pessoais, "i", $usuario_id);
mysqli_stmt_execute($stmt_pessoais);
$result_pessoais = mysqli_stmt_get_result($stmt_pessoais);
$dados_pessoais = mysqli_fetch_assoc($result_pessoais);

// Busca investimentos
$sql_inv = "SELECT data_compra, rendimento_atual, rendimento_total, valor_investido FROM investimentos WHERE usuario_id = ?";
$stmt_inv = mysqli_prepare($conexao, $sql_inv);
mysqli_stmt_bind_param($stmt_inv, "i", $usuario_id);
mysqli_stmt_execute($stmt_inv);
$result_inv = mysqli_stmt_get_result($stmt_inv);
$investimentos = mysqli_fetch_all($result_inv, MYSQLI_ASSOC);
?>

<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <title>Dashboard - Minha Conta</title>
    <link rel="stylesheet" href="css/style.css">
    <style>
        body {
            background-image: url('img/fundo.png'); /* Corrigido caminho relativo */
            background-size: cover;
            background-repeat: no-repeat;
            background-position: center;
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .dashboard-container {
            max-width: 900px;
            margin: 40px auto;
            padding: 20px;
            background: rgba(255, 255, 255, 0.95);
            border-radius: 10px;
            box-shadow: 0px 4px 12px rgba(0,0,0,0.1);
        }

        h1 {
            color: #333;
            text-align: center;
            margin-bottom: 40px;
        }

        .card {
            background: #fafafa;
            padding: 20px;
            margin-bottom: 30px;
            border-radius: 8px;
            border-left: 5px solid #007BFF;
        }

        .card h2 {
            margin-top: 0;
            color: #007BFF;
        }

        ul {
            list-style: none;
            padding: 0;
        }

        ul li {
            padding: 5px 0;
            border-bottom: 1px solid #eee;
        }

        .tabela-investimentos {
            width: 100%;
            border-collapse: collapse;
            margin-top: 10px;
        }

        .tabela-investimentos th,
        .tabela-investimentos td {
            padding: 10px;
            text-align: left;
            border-bottom: 1px solid #ddd;
        }

        .tabela-investimentos th {
            background: #007BFF;
            color: white;
        }

        .btn-sair {
            display: block;
            width: fit-content;
            margin: 0 auto;
            padding: 10px 25px;
            background-color: #dc3545;
            color: white;
            text-decoration: none;
            border-radius: 5px;
            font-weight: bold;
            text-align: center;
        }

        .btn-sair:hover {
            background-color: #c82333;
        }
    </style>
</head>
<body>
    <div class="dashboard-container">
        <h1>Olá, <?=htmlspecialchars($_SESSION['username'])?> 👋</h1>

        <div class="card">
            <h2>Dados Pessoais</h2>
            <?php if ($dados_pessoais): ?>
                <ul>
                    <li><strong>Nome:</strong> <?=htmlspecialchars($dados_pessoais['nome'])?></li>
                    <li><strong>CPF:</strong> <?=htmlspecialchars($dados_pessoais['cpf'])?></li>
                    <li><strong>E-mail:</strong> <?=htmlspecialchars($dados_pessoais['email'])?></li>
                    <li><strong>Telefone:</strong> <?=htmlspecialchars($dados_pessoais['telefone'])?></li>
                </ul>
            <?php else: ?>
                <p>Dados pessoais não cadastrados.</p>
            <?php endif; ?>
        </div>

        <div class="card">
            <h2>Investimentos</h2>
            <?php if ($investimentos): ?>
                <table class="tabela-investimentos">
                    <thead>
                        <tr>
                            <th>Data da Compra</th>
                            <th>Valor Investido</th>
                            <th>Rendimento Atual</th>
                            <th>Rendimento Total</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($investimentos as $inv): ?>
                        <tr>
                            <td><?=htmlspecialchars($inv['data_compra'])?></td>
                            <td>R$ <?=number_format($inv['valor_investido'], 2, ',', '.')?></td>
                            <td>R$ <?=number_format($inv['rendimento_atual'], 2, ',', '.')?></td>
                            <td>R$ <?=number_format($inv['rendimento_total'], 2, ',', '.')?></td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            <?php else: ?>
                <p>Sem investimentos cadastrados.</p>
            <?php endif; ?>
        </div>

        <a href="logout.php" class="btn-sair">Sair da conta</a>
    </div>
</body>
</html>
