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

// Hakikisha mtu ame-login
if (!isset($_SESSION['user_id'])) {
    die("Access denied");
}

// Approve or Reject Logic
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['recharge_id']) && isset($_POST['action'])) {
    $id = intval($_POST['recharge_id']);
    $action = $_POST['action'];

    $status = ($action === 'approve') ? 'approved' : 'rejected';

    // Pata recharge details
    $stmt = $conn->prepare("SELECT user_id, amount FROM mpesa_recharges WHERE id = ?");
    $stmt->bind_param("i", $id);
    $stmt->execute();
    $stmt->store_result();

    if ($stmt->num_rows > 0) {
        $stmt->bind_result($user_id, $amount);
        $stmt->fetch();

        // Update recharge status
        $update = $conn->prepare("UPDATE mpesa_recharges SET status = ? WHERE id = ?");
        $update->bind_param("si", $status, $id);
        $update->execute();

        if ($status === 'approved') {
            // Ongeza balance ya user
            $updateBalance = $conn->prepare("UPDATE users SET balance = balance + ? WHERE id = ?");
            $updateBalance->bind_param("di", $amount, $user_id);
            $updateBalance->execute();
        }
    }
}

// Fetch all recharges (not just pending)
$results = $conn->query("SELECT r.id, r.user_id, u.phone, r.amount, r.sms, r.created_at, r.status
                         FROM mpesa_recharges r
                         JOIN users u ON r.user_id = u.id
                         ORDER BY r.created_at DESC");
?>

<!DOCTYPE html>
<html>
<head>
  <title>Approve Recharges</title>
  <style>
    body { font-family: Arial, sans-serif; padding: 20px; background: #f2f2f2; }
    h2 { text-align: center; margin-bottom: 20px; }
    .recharge-box {
      background: #fff;
      padding: 15px;
      border-radius: 8px;
      margin-bottom: 20px;
      box-shadow: 0 0 10px rgba(0,0,0,0.05);
    }
    .recharge-box p { margin: 5px 0; }
    form { margin-top: 10px; }
    button {
      padding: 8px 12px;
      margin-right: 10px;
      border: none;
      border-radius: 5px;
      color: #fff;
      cursor: pointer;
    }
    .approve { background: #28a745; }
    .reject { background: #dc3545; }
    .status { font-weight: bold; }
    .approved { color: green; }
    .rejected { color: red; }
    .pending { color: orange; }
  </style>
</head>
<body>

<h2>All M-PESA Recharges</h2>

<?php if ($results->num_rows > 0): ?>
  <?php while ($row = $results->fetch_assoc()): ?>
    <div class="recharge-box">
      <p><strong>User ID:</strong> <?= $row['user_id'] ?> (<?= htmlspecialchars($row['phone']) ?>)</p>
      <p><strong>Amount:</strong> ZMW <?= $row['amount'] ?></p>
      <p><strong>SMS:</strong><br><?= nl2br(htmlspecialchars($row['sms'])) ?></p>
      <p><strong>Time:</strong> <?= $row['created_at'] ?></p>
      <p><strong>Status:</strong> 
        <span class="status <?= $row['status'] ?>">
          <?= ucfirst($row['status']) ?>
        </span>
      </p>
      <?php if ($row['status'] === 'pending'): ?>
        <form method="post">
          <input type="hidden" name="recharge_id" value="<?= $row['id'] ?>">
          <button type="submit" name="action" value="approve" class="approve">Approve</button>
          <button type="submit" name="action" value="reject" class="reject">Reject</button>
        </form>
      <?php endif; ?>
    </div>
  <?php endwhile; ?>
<?php else: ?>
  <p style="text-align:center;">No recharges found.</p>
<?php endif; ?>

</body>
</html>
