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

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

$user_id = $_SESSION['user_id'];
$message = "";
$message_color = "#0ff"; // default cyan

// Handle form submission
if ($_SERVER["REQUEST_METHOD"] === "POST") {
  $amount = floatval($_POST['amount']);
  $sms = trim($_POST['sms']);

  // Check for required values
  if ($amount <= 0 || empty($sms)) {
    $message = "❌ Please fill in all fields correctly.";
    $message_color = "red";
  } else {
    // Check for duplicate SMS
    $check = $conn->prepare("SELECT id FROM mpesa_recharges WHERE sms = ?");
    $check->bind_param("s", $sms);
    $check->execute();
    $check->store_result();

    if ($check->num_rows > 0) {
      $message = "⚠ This SMS has already been submitted!";
      $message_color = "#ffc107";
    } else {
      // Insert into database
      $stmt = $conn->prepare("INSERT INTO mpesa_recharges (user_id, amount, sms) VALUES (?, ?, ?)");
      $stmt->bind_param("ids", $user_id, $amount, $sms);
      if ($stmt->execute()) {
        $message = "✅ Recharge submitted successfully! Please wait for admin approval.";
      } else {
        $message = "❌ Failed to submit. Please try again later.";
        $message_color = "red";
      }
    }
  }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <title>Recharge</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 {
      background: #0a1624;
      color: #fff;
      font-family: 'Segoe UI', sans-serif;
      padding: 20px;
    }
    .recharge-box {
      background: #111a2e;
      border-radius: 15px;
      padding: 20px;
      max-width: 450px;
      margin: auto;
      box-shadow: 0 0 15px rgba(0,180,255,0.3);
    }
    .recharge-box h2 {
      color: #4dd0ff;
      margin-bottom: 20px;
      text-align: center;
    }
    .info-box {
      background: #1b2b44;
      padding: 15px;
      border-radius: 10px;
      margin-bottom: 20px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      color: #cce7ff;
    }
    .info-box span {
      font-size: 16px;
    }
    .copy-btn {
      background: linear-gradient(to right, #4dd0ff, #00ffaa);
      color: #000;
      border: none;
      padding: 6px 14px;
      border-radius: 8px;
      cursor: pointer;
      font-weight: bold;
    }
    .input-group {
      margin-bottom: 15px;
    }
    .input-group label {
      display: block;
      margin-bottom: 5px;
      color: #b0cde6;
    }
    .input-group input, .input-group textarea {
      width: 93%;
      padding: 10px;
      border: none;
      border-radius: 8px;
      background: #22334d;
      color: #fff;
    }
    .input-group input:focus, .input-group textarea:focus {
      outline: none;
      box-shadow: 0 0 8px #00b4ff;
    }
    .submit-btn {
      width: 100%;
      padding: 12px;
      background: linear-gradient(to right, #4dd0ff, #00ffaa);
      color: #000;
      border: none;
      border-radius: 20px;
      font-weight: bold;
      cursor: pointer;
      margin-top: 10px;
      transition: 0.3s;
    }
    .submit-btn:hover {
      opacity: 0.9;
    }
    .steps {
      font-size: 14px;
      background: #1b2b44;
      padding: 12px;
      border-radius: 10px;
      margin-top: 20px;
      color: #d4e9ff;
    }
    .steps li {
      margin-bottom: 8px;
    }
    .msg {
      margin-bottom: 20px;
      background: #1b2b44;
      border-left: 5px solid <?= $message_color ?>;
      padding: 10px 15px;
      border-radius: 10px;
      color: <?= $message_color ?>;
      font-weight: 500;
      animation: fadein 0.5s ease-in-out;
    }
    @keyframes fadein {
      from {opacity: 0;}
      to {opacity: 1;}
    }
  </style>
</head>
<body>

  <div class="recharge-box">
    <h2>Recharge via MTN/Airtel</h2>

    <?php if (!empty($message)): ?>
      <div class="msg"><?= $message ?></div>
    <?php endif; ?>

    <div class="info-box">
      <span>MTN Number: <strong>‪+260763776043‬</strong></span>
       <button class="copy-btn" onclick="copyMTN()">Copy</button></div>
        <div class="info-box">
       <span>Airtel Zambia: <strong>‪+260972586082‬</strong></span>
     <button class="copy-btn" onclick="copyAirtel()">Copy</button>
    </div>

    <form method="post">
      <div class="input-group">
        <label for="amount">Amount (ZMW)</label>
        <input type="number" name="amount" id="amount" required />
      </div>

      <div class="input-group">
        <label for="sms">Paste your confirmation SMS</label>
        <textarea name="sms" id="sms" rows="5" placeholder="E.g. QFD3GH1Q Confirmed. ZMW 1,500.00 sent to..." required></textarea>
      </div>

      <button type="submit" class="submit-btn">Submit Recharge</button>
    </form>

    <ul class="steps">
      <li>✅ Go to your MTN/Airtel menu</li>
      <li>✅ Select Send Money</li>
      <li>✅ Enter the MTN/Airtel number above and your amount</li>
      <li>✅ After payment, paste the SMS below and click Submit</li>
    </ul>
  </div>

  <script>
  function copyMTN() {
    const number = "‪+260763776043‬";
    navigator.clipboard.writeText(number).then(() => {
      alert("MTN number copied!");
    });
  }

  function copyAirtel() {
    const number = "‪+260972586082‬";
    navigator.clipboard.writeText(number).then(() => {
      alert("Airtel number copied!");
    });
}
</script>
</body>
</html>
