<?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>Withdraw 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: #0a192f; /* Bluish dark background */
      color: #e0e0e0;
    }

    .tabs {
      display: flex;
      justify-content: space-around;
      background-color: #112240; /* Deep blue */
      border-bottom: 2px solid #1f4068;
      border-radius: 20px;
      margin: 10px;
      overflow: hidden;
    }

    .tab {
      flex: 1;
      text-align: center;
      padding: 15px 0;
      font-weight: bold;
      cursor: pointer;
      color: #90a4ae;
      border-bottom: 3px solid transparent;
      transition: all 0.3s ease;
    }

    .tab.active {
      color: #fff;
      border-bottom: 3px solid #00bcd4;
      background: linear-gradient(to left, #2196f3, #00bcd4);
    }

    .record-section {
      padding: 20px;
    }

    .record {
      background-color: #112240;
      border-radius: 12px;
      padding: 15px;
      margin-bottom: 15px;
      box-shadow: 0 4px 10px rgba(0,0,0,0.3);
      border: 1px solid #1f4068;
    }

    .record h4 {
      margin: 0;
      color: #ffffff;
    }

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

    .amount {
      color: #00bcd4;
      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: #c8e6c9;
      color: #1b5e20;
    }

    .pending {
      background-color: #bbdefb;
      color: #0d47a1;
    }

    .rejected {
      background-color: #ffcdd2;
      color: #b71c1c;
    }
  </style>
</head>
<body>

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

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

  if ($withdrawals->num_rows > 0) {
    while ($row = $withdrawals->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>Withdraw</h4>
              <p class='amount'>- ZMW " . number_format($row['amount']) . "</p>
              <p>" . $row['requested_at'] . "</p>
              <span class='status $statusClass'>$statusText</span>
            </div>";
    }
  } else {
    echo "<p style='color: #78909c;'>No withdrawal records found.</p>";
  }

  $stmt->close();
  ?>
</div>

</body>
</html>
