<?php
require_once __DIR__ . '/../../app/helpers/functions.php';
require_once __DIR__ . '/../../app/helpers/csrf.php';
require_once __DIR__ . '/../../app/middleware/auth.php';

session_boot();
require_login();

$conn = require __DIR__ . '/../../app/config/database.php';
require_admin($conn);

/* ===============================
   LOAD ASSETS
================================ */
$assets = [];
$res = $conn->query("
  SELECT id, symbol, name
  FROM crypto_assets
  WHERE enabled = 1
  ORDER BY symbol ASC
");
while ($r = $res->fetch_assoc()) {
  $assets[] = $r;
}

/* ===============================
   LOAD USER (FROM QUERY STRING)
================================ */
$userId = (int)($_GET['user_id'] ?? 0);
if ($userId < 1) {
  flash('err', 'Invalid user.');
  redirect('customers.php');
}

$st = $conn->prepare("SELECT id, full_name, email FROM users WHERE id=? LIMIT 1");
$st->bind_param("i", $userId);
$st->execute();
$user = $st->get_result()->fetch_assoc();

if (!$user) {
  flash('err', 'User not found.');
  redirect('customers.php');
}

$title = 'Credit Crypto Wallet';
include __DIR__ . '/../../views/partials/head.php';
include __DIR__ . '/../../views/partials/admin_sidebar.php';
?>

<main class="flex-grow-1" style="margin-left:240px; min-height:100vh;">
<div class="container-fluid py-4">

  <div class="d-flex justify-content-between align-items-center mb-3">
    <div>
      <h3 class="fw-bold mb-0">Credit Crypto Wallet</h3>
      <div class="ab-muted">
        Admin credit for <?= e($user['full_name'] ?? $user['email']) ?>
      </div>
    </div>
    <a href="customer_view.php?id=<?= (int)$user['id'] ?>" class="btn btn-outline-secondary">
      ← Back to User
    </a>
  </div>

  <?php if ($m = flash('msg')): ?>
    <div class="alert alert-success"><?= e($m) ?></div>
  <?php endif; ?>
  <?php if ($m = flash('err')): ?>
    <div class="alert alert-danger"><?= e($m) ?></div>
  <?php endif; ?>

  <!-- CREDIT FORM -->
  <div class="card ab-card p-4" style="max-width:520px">
    <form method="post" action="crypto_wallet_adjust.php">
      <?= csrf_field() ?>

      <input type="hidden" name="user_id" value="<?= (int)$user['id'] ?>">
      <input type="hidden" name="mode" value="credit">
      <input type="hidden" name="source" value="platform">

      <label class="form-label">Asset</label>
      <select name="asset_id" class="form-select mb-3" required>
        <?php foreach ($assets as $a): ?>
          <option value="<?= (int)$a['id'] ?>">
            <?= e($a['symbol']) ?> — <?= e($a['name']) ?>
          </option>
        <?php endforeach; ?>
      </select>

      <label class="form-label">Amount</label>
      <input
        type="number"
        step="0.00000001"
        name="amount"
        class="form-control mb-3"
        placeholder="0.00000000"
        required
      >

      <label class="form-label">Note (optional)</label>
      <input
        type="text"
        name="note"
        class="form-control mb-4"
        placeholder="Admin top-up / demo credit"
      >

      <button class="btn btn-success w-100">
        <i class="bi bi-plus-circle me-1"></i> Credit Wallet
      </button>
    </form>
  </div>

</div>
</main>

<?php include __DIR__ . '/../../views/partials/foot.php'; ?>
