<?php
session_start();
require_once 'db.php';

if (!isset($_SESSION['user_id'])) {
  header("Location: login.php");
  exit();
}

$user_id = $_SESSION['user_id'];

$query = $conn->prepare("SELECT * FROM investments WHERE user_id = ? AND status = 'active'");
$query->bind_param("i", $user_id);
$query->execute();
$result = $query->get_result();
?>

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>My Investments</title>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css"/>
  <style>
    body {
      font-family: Arial, sans-serif;
      background: #e9f3fb; /* light blue background */
      color: #1a1a1a;
      padding: 20px;
    }

    h1 {
      text-align: center;
      color: #0d47a1;
      margin-bottom: 25px;
    }

    .tabs {
      display: flex;
      justify-content: center;
      margin-bottom: 20px;
    }

    .tab {
      padding: 10px 20px;
      border-radius: 25px;
      margin: 0 6px;
      font-weight: bold;
      transition: 0.3s;
      text-align: center;
    }

    .tab.active {
      background: linear-gradient(to right, #0d47a1, #1976d2);
      color: white;
      width: 160px;
      box-shadow: 0 3px 6px rgba(0,0,0,0.2);
    }

    .tab.inactive {
      background: linear-gradient(to right, #64b5f6, #bbdefb);
      color: #0d47a1;
      width: 160px;
      border: 1px solid #1976d2;
    }

    .investment-card {
      background: #ffffff;
      border-left: 6px solid #1976d2;
      padding: 20px;
      border-radius: 12px;
      margin-bottom: 20px;
      box-shadow: 0 2px 8px rgba(0,0,0,0.1);
      transition: transform 0.2s;
    }

    .investment-card:hover {
      transform: translateY(-3px);
    }

    .investment-card h3 {
      margin: 0 0 10px;
      color: #0d47a1;
    }

    .investment-card p {
      margin: 6px 0;
      color: #333;
    }

    .investment-card i {
      color: #1976d2;
      margin-right: 6px;
    }

    .no-investment {
      background: #bbdefb;
      padding: 20px;
      border-radius: 10px;
      text-align: center;
      color: #0d47a1;
      font-weight: bold;
      box-shadow: 0 2px 6px rgba(0,0,0,0.15);
    }
  </style>
</head>
<body>

  <h1><i class="fa-solid fa-sack-dollar"></i> My Investments</h1>

  <!-- Example Tabs -->
  <div class="tabs">
    <div class="tab active">Active</div>
    <div class="tab inactive">Completed</div>
  </div>

  <!-- Investment List -->
  <?php
  if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
      $amount = $row['amount'];
      $daily = $row['daily_earnings'];
      $total_earned = $row['total_earned'];
      $start_time = strtotime($row['start_time']);
      $daily_percentage = $row['daily_percentage'];
      $cycle_days = $row['cycle_days'];

      $now = time();
      $end_time = strtotime("+$cycle_days days", $start_time);
      $remaining_seconds = $end_time - $now;
      $remaining_days = max(0, ceil($remaining_seconds / 86400));

      echo "<div class='investment-card'>
              <h3><i class='fa-solid fa-coins'></i> Invested: KES " . number_format($amount) . "</h3>
              <p><i class='fa-regular fa-calendar'></i> Started: " . date("d M Y", $start_time) . "</p>
              <p><i class='fa-solid fa-chart-line'></i> Daily Income: KES " . number_format($daily) . " (" . $daily_percentage . "%)</p>
              <p><i class='fa-solid fa-wallet'></i> Total Earned: KES " . number_format($total_earned) . "</p>
              <p><i class='fa-regular fa-clock'></i> Remaining Days: " . $remaining_days . "</p>
            </div>";
    }
  } else {
    echo "<div class='no-investment'><i class='fa-solid fa-circle-info'></i> You have no active investments.</div>";
  }
  ?>

</body>
</html>
