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

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

$user_id = $_SESSION['user_id'];
?>

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <title>Recharge Records</title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css"/>
  <style>
    body {
      margin: 0;
      font-family: 'Segoe UI', sans-serif;
      background-color: #0a1624;
      color: #fff;
    }

    .tabs {
      display: flex;
      justify-content: space-around;
      background: #111a2e;
      border-radius: 25px;
      margin: 15px;
      overflow: hidden;
      box-shadow: 0 0 10px rgba(0,180,255,0.3);
    }

    .tab {
      flex: 1;
      text-align: center;
      padding: 15px 0;
      font-weight: bold;
      cursor: pointer;
      color: #a0c4e4;
      transition: all 0.3s ease;
    }

    .tab.active {
      color: #000;
      background: linear-gradient(to right, #4dd0ff, #00ffaa);
      border-radius: 20px;
      margin: 5px;
    }

    .record-section {
      padding: 20px;
    }

    .record {
      background: #1b2b44;
      border-radius: 12px;
      padding: 15px;
      margin-bottom: 15px;
      box-shadow: 0 2px 8px rgba(0,180,255,0.2);
    }

    .record h4 {
      margin: 0;
      color: #4dd0ff;
    }

    .record p {
      margin: 5px 0 0;
      font-size: 14px;
      color: #d0e6ff;
    }

    .amount {
      color: #00ffaa;
      font-weight: bold;
      font-size: 16px;
    }

    .status {
      display: inline-block;
      padding: 4px 10px;
      border-radius: 12px;
      font-size: 13px;
      font-weight: bold;
      margin-top: 8px;
    }

    .approved {
      background-color: #0f5132;
      color: #00ffaa;
    }

    .pending {
      background-color: #1e3a5f;
      color: #ffc107;
    }

    .rejected {
      background-color: #5c1a1a;
      color: #ff6b6b;
    }
  </style>
</head>
<body>

<div class="tabs">
  <div class="tab active">Recharge Account</div>
  <div class="tab" onclick="window.location.href='withdraw_records.php'">Withdraw Account</div>
</div>

<div class="record-section">
  <?php
  $stmt = $conn->prepare("SELECT amount, created_at, status FROM mpesa_recharges WHERE user_id = ? ORDER BY created_at DESC");
  $stmt->bind_param("i", $user_id);
  $stmt->execute();
  $recharges = $stmt->get_result();

  if ($recharges->num_rows > 0) {
    while ($row = $recharges->fetch_assoc()) {
      $statusClass = '';
      $statusText = ucfirst($row['status']);
      if ($row['status'] == 'approved') {
        $statusClass = 'approved';
      } elseif ($row['status'] == 'pending') {
        $statusClass = 'pending';
      } else {
        $statusClass = 'rejected';
      }

      echo "<div class='record'>
              <h4>Recharge</h4>
              <p class='amount'>+ KES " . number_format($row['amount']) . "</p>
              <p>" . $row['created_at'] . "</p>
              <span class='status $statusClass'>$statusText</span>
            </div>";
    }
  } else {
    echo "<p style='color: #88aacc;'>No recharge records found.</p>";
  }
  ?>
</div>

</body>
</html>
