<?php
session_start();

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

require_once 'php.php';

if (empty($_SESSION['user_id'])) {
    header("Location: SS_Main_Page_.php");
    exit();
}

$user_id = (int)$_SESSION['user_id'];
$user_name = $_SESSION['name'] ?? 'User';

$income_frequency = '';
$annual_salary = 0.00;

$stmt = $conn->prepare("SELECT income_frequency, annual_salary FROM users WHERE id = ?");
if (!$stmt) {
    die("Prepare failed: " . htmlspecialchars($conn->error));
}
$stmt->bind_param("i", $user_id);
$stmt->execute();
$result = $stmt->get_result();

if ($row = $result->fetch_assoc()) {
    $income_frequency = $row['income_frequency'] ?? '';
    $annual_salary = (float)($row['annual_salary'] ?? 0);
}
$stmt->close();

$monthly_income = $annual_salary > 0 ? $annual_salary / 12 : 0;
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <title>Dashboard - Smart Spender</title>
  <link rel="stylesheet" href="styles.css" />
</head>
<body>
  <!-- Logo top-left -->
  <header class="signup-header">
    <div class="brand">
    <span class="logo">&#128178;</span>
    <span class="name"><strong>Smart</strong> <br> 
    <strong>Spender</strong> </span>
    </div>
  </header>

  <!--navigation buttons to there individual pages -->
<nav style="padding: 12px;">
  <a class="plan-btn" href="SS_basic_plan.php">Plan 1: Budget</a> |
  <a class="plan-btn" href="SS_detailed_plan.php">Plan 2: Expenses</a> |
  <a class="plan-btn" href="SS_Summary.php">Summary</a> |
<a class="plan-btn" href="SS_logout.php">Logout</a>
</nav>


<main style="padding: 16px;">
    <section class="dashboard-section">
  <h1>Welcome, <?php echo htmlspecialchars($user_name); ?>!</h1>

  <section style="margin-top: 16px;">
    <h2>Your Income Summary</h2>
    <p><strong>Income Frequency:</strong> <?php echo htmlspecialchars($income_frequency ?: 'Not set'); ?></p>
    <p><strong>Annual Salary:</strong> $<?php echo number_format($annual_salary, 2); ?></p>
    <p><strong>Estimated Monthly Income:</strong> $<?php echo number_format($monthly_income, 2); ?></p>
  </section>

  <section style="margin-top: 16px;">
    <h2>Choose a Plan</h2>
    <p><a class="plan-btn" href="SS_basic_plan.php">➡ Plan 1: Simple Monthly Budget</a></p>
    <p><a class="plan-btn" href="SS_detailed_plan.php">➡ Plan 2: Detailed Expense Tracker</a></p>
  </section>
</main>

</body>
</html>
