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

$conn = require __DIR__ . '/../../app/config/database.php';
require_login();
$u = current_user();

$ref = trim($_GET['ref'] ?? '');
if (!$ref) { flash('err','Missing ref'); redirect('crypto_wallet.php'); }

$st = $conn->prepare("
  SELECT t.*, a.symbol, a.name AS asset_name
  FROM crypto_txn t
  JOIN crypto_assets a ON a.id=t.asset_id
  WHERE t.ref=? AND t.user_id=? AND t.type='send'
  LIMIT 1
");
$st->bind_param("si", $ref, $u['id']);
$st->execute();
$txn = $st->get_result()->fetch_assoc();
if (!$txn) { flash('err','Transaction not found'); redirect('crypto_wallet.php'); }

$title = 'Crypto Receipt';
include __DIR__ . '/../../views/partials/head.php';
include __DIR__ . '/../../views/partials/customer_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">Transaction Receipt</h3>
        <div class="ab-muted">Reference: <?= e($txn['ref']) ?></div>
      </div>
      <div class="d-flex gap-2">
        <a href="crypto_wallet.php" class="btn btn-outline-secondary"><i class="bi bi-arrow-left"></i> Back</a>
        <button onclick="window.print()" class="btn ab-btn text-white"><i class="bi bi-printer me-1"></i>Print</button>
      </div>
    </div>

    <div class="card ab-card p-4">
      <div class="row g-3">
        <div class="col-md-3">
          <div class="ab-muted">Asset</div>
          <div class="fw-bold"><?= e($txn['symbol']) ?> — <?= e($txn['asset_name']) ?></div>
        </div>
        <div class="col-md-3">
          <div class="ab-muted">Amount</div>
          <div class="fw-bold"><?= e((string)$txn['amount']) ?></div>
        </div>
        <div class="col-md-3">
          <div class="ab-muted">Fee</div>
          <div class="fw-bold"><?= e((string)$txn['fee']) ?></div>
        </div>
        <div class="col-md-3">
          <div class="ab-muted">Total Debit</div>
          <div class="fw-bold"><?= e((string)$txn['total_debit']) ?></div>
        </div>

        <div class="col-12">
          <div class="ab-muted">Destination</div>
          <div class="fw-bold" style="word-break:break-all;"><?= e($txn['destination']) ?></div>
        </div>

        <div class="col-md-4">
          <div class="ab-muted">Status</div>
          <div class="fw-bold"><?= e($txn['status']) ?></div>
        </div>
        <div class="col-md-4">
          <div class="ab-muted">OTP Verified</div>
          <div class="fw-bold"><?= ((int)$txn['otp_verified']===1) ? 'YES' : 'NO' ?></div>
        </div>
        <div class="col-md-4">
          <div class="ab-muted">Risk</div>
          <div class="fw-bold"><?= ((int)$txn['risk_flag']===1) ? 'FLAGGED' : 'OK' ?></div>
          <?php if (!empty($txn['risk_reason'])): ?>
            <div class="ab-muted" style="font-size:12px;"><?= e($txn['risk_reason']) ?></div>
          <?php endif; ?>
        </div>

        <?php if (!empty($txn['tx_hash'])): ?>
          <div class="col-12">
            <div class="ab-muted">Blockchain Tx Hash</div>
            <div class="fw-bold" style="word-break:break-all;"><?= e($txn['tx_hash']) ?></div>
          </div>
        <?php endif; ?>
      </div>
    </div>

  </div>
</main>

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