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

$conn = require __DIR__ . '/../../app/config/database.php';
require_login();
$admin = current_user(); // assume role checked in middleware/guard

$title = 'Crypto Send Review';
include __DIR__ . '/../../views/partials/head.php';
include __DIR__ . '/../../views/partials/admin_sidebar.php';

$rows = [];
$res = $conn->query("
  SELECT t.ref,t.user_id,t.asset_id,t.amount,t.fee,t.total_debit,t.destination,t.status,t.risk_flag,t.risk_reason,t.created_at,
         a.symbol
  FROM crypto_txn t
  JOIN crypto_assets a ON a.id=t.asset_id
  WHERE t.type='send' AND t.status='awaiting_admin'
  ORDER BY t.id DESC
");
while ($r = $res->fetch_assoc()) $rows[] = $r;
?>

<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">Crypto Sends</h3>
        <div class="ab-muted">Approve or reject. Funds are locked until decision.</div>
      </div>
    </div>

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

    <div class="card ab-card p-3">
      <div class="table-responsive">
        <table class="table align-middle mb-0">
          <thead>
            <tr>
              <th>Ref</th>
              <th>Asset</th>
              <th>Amount</th>
              <th>Fee</th>
              <th>Total</th>
              <th>Destination</th>
              <th>Risk</th>
              <th>Action</th>
            </tr>
          </thead>
          <tbody>
          <?php if (!$rows): ?>
            <tr><td colspan="8" class="text-muted">No awaiting approvals.</td></tr>
          <?php endif; ?>
          <?php foreach ($rows as $t): ?>
            <tr>
              <td class="fw-bold"><?= e($t['ref']) ?></td>
              <td><?= e($t['symbol']) ?></td>
              <td><?= e((string)$t['amount']) ?></td>
              <td><?= e((string)$t['fee']) ?></td>
              <td><?= e((string)$t['total_debit']) ?></td>
              <td style="max-width:280px;word-break:break-all;"><?= e($t['destination']) ?></td>
              <td>
                <?php if ((int)$t['risk_flag']===1): ?>
                  <span class="badge bg-warning text-dark">FLAG</span>
                  <div class="ab-muted" style="font-size:12px;"><?= e($t['risk_reason'] ?? '') ?></div>
                <?php else: ?>
                  <span class="badge bg-success">OK</span>
                <?php endif; ?>
              </td>
              <td>
                <form method="post" action="crypto_txn_action.php" class="d-flex gap-2">
                  <?= csrf_field() ?>
                  <input type="hidden" name="ref" value="<?= e($t['ref']) ?>">
                  <input type="text" name="tx_hash" class="form-control form-control-sm" placeholder="Tx hash (approve)">
                  <button name="do" value="approve" class="btn btn-sm btn-success">Approve</button>
                  <button name="do" value="reject" class="btn btn-sm btn-danger">Reject</button>
                </form>
              </td>
            </tr>
          <?php endforeach; ?>
          </tbody>
        </table>
      </div>
    </div>

  </div>
</main>

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