<?php use App\Helpers\Helper; ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Receipt <?= Helper::e($payment['receipt_number'] ?? $payment['payment_ref'] ?? '') ?></title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css" rel="stylesheet">
<style>
  body { background:#f0f4f8; font-family:'Segoe UI',Arial,sans-serif; }
  .receipt-wrap { max-width:620px; margin:30px auto; background:#fff; border-radius:16px; box-shadow:0 4px 24px rgba(0,0,0,.10); overflow:hidden; }
  .receipt-header { background:linear-gradient(135deg,#1a3c5e,#2d6a9f); color:#fff; padding:32px 36px 24px; text-align:center; }
  .receipt-header h3 { font-weight:800; margin:0 0 4px; }
  .receipt-header p { opacity:.8; font-size:.85rem; margin:0; }
  .receipt-badge { display:inline-block; background:rgba(255,255,255,.2); border:1px solid rgba(255,255,255,.4); border-radius:20px; padding:4px 20px; font-size:.8rem; letter-spacing:2px; margin-top:12px; }
  .receipt-body { padding:32px 36px; }
  .receipt-row { display:flex; justify-content:space-between; padding:10px 0; border-bottom:1px dashed #e2e8f0; }
  .receipt-row:last-child { border-bottom:none; }
  .receipt-label { color:#64748b; font-size:.82rem; }
  .receipt-value { font-weight:600; font-size:.9rem; text-align:right; }
  .amount-box { background:linear-gradient(135deg,#1a3c5e,#2d6a9f); color:#fff; border-radius:12px; padding:24px; text-align:center; margin:24px 0; }
  .amount-box .amount { font-size:2.4rem; font-weight:800; }
  .amount-box .label { opacity:.75; font-size:.82rem; }
  .receipt-footer { background:#f8fafc; border-top:1px solid #e2e8f0; padding:20px 36px; text-align:center; }
  .status-paid { background:#d1fae5; color:#065f46; border-radius:20px; padding:3px 14px; font-size:.78rem; font-weight:700; display:inline-block; }
  .status-pending { background:#fef3c7; color:#92400e; border-radius:20px; padding:3px 14px; font-size:.78rem; font-weight:700; display:inline-block; }
  .no-print { margin:20px auto; max-width:620px; display:flex; gap:10px; justify-content:center; }
  @media print {
    body { background:#fff; }
    .no-print { display:none!important; }
    .receipt-wrap { box-shadow:none; margin:0; border-radius:0; }
  }
</style>
</head>
<body>

<div class="no-print">
  <button onclick="window.print()" class="btn btn-primary"><i class="bi bi-printer me-2"></i>Print Receipt</button>
  <?php
    $backUrl = isset($_SERVER['HTTP_REFERER']) && !empty($_SERVER['HTTP_REFERER'])
      ? $_SERVER['HTTP_REFERER']
      : APP_URL . '/tenant/payments';
  ?>
  <a href="<?= htmlspecialchars($backUrl) ?>" class="btn btn-outline-secondary"><i class="bi bi-arrow-left me-2"></i>Back</a>
</div>

<div class="receipt-wrap">
  <!-- Header -->
  <div class="receipt-header">
    <i class="bi bi-buildings-fill" style="font-size:2rem;opacity:.8"></i>
    <h3><?= Helper::e($settings['company_name'] ?? APP_NAME) ?></h3>
    <p><?= Helper::e($settings['company_address'] ?? '') ?></p>
    <p><?= Helper::e($settings['company_phone'] ?? '') ?> &bull; <?= Helper::e($settings['company_email'] ?? '') ?></p>
    <div class="receipt-badge">OFFICIAL PAYMENT RECEIPT</div>
  </div>

  <!-- Body -->
  <div class="receipt-body">

    <!-- Amount -->
    <div class="amount-box">
      <div class="label">Amount Paid</div>
      <div class="amount"><?= Helper::money($payment['amount']) ?></div>
      <?php if (!empty($payment['period_from'])): ?>
      <div class="label mt-1">Period: <?= Helper::date($payment['period_from']) ?> &mdash; <?= Helper::date($payment['period_to'] ?? '') ?></div>
      <?php endif; ?>
    </div>

    <!-- Details -->
    <div class="receipt-row">
      <span class="receipt-label">Receipt No.</span>
      <span class="receipt-value"><?= Helper::e($payment['receipt_number'] ?? $payment['payment_ref'] ?? '—') ?></span>
    </div>
    <div class="receipt-row">
      <span class="receipt-label">Payment Date</span>
      <span class="receipt-value"><?= Helper::date($payment['payment_date']) ?></span>
    </div>
    <div class="receipt-row">
      <span class="receipt-label">Paid By</span>
      <span class="receipt-value">
        <?php
          $tenantName = '';
          if (!empty($payment['tenant_name'])) {
            $tenantName = $payment['tenant_name'];
          } elseif (!empty($tenant)) {
            $tenantName = ($tenant['first_name'] ?? '') . ' ' . ($tenant['last_name'] ?? '');
          }
        ?>
        <?= Helper::e(trim($tenantName) ?: '—') ?>
      </span>
    </div>
    <?php if (!empty($payment['unit_number'])): ?>
    <div class="receipt-row">
      <span class="receipt-label">Unit</span>
      <span class="receipt-value"><?= Helper::e($payment['unit_number']) ?><?= !empty($payment['property_name']) ? ' — '.Helper::e($payment['property_name']) : '' ?></span>
    </div>
    <?php endif; ?>
    <div class="receipt-row">
      <span class="receipt-label">Payment For</span>
      <span class="receipt-value"><?= ucfirst(str_replace('_', ' ', $payment['payment_for'] ?? 'Rent')) ?></span>
    </div>
    <div class="receipt-row">
      <span class="receipt-label">Payment Method</span>
      <span class="receipt-value"><?= Helper::e($payment['method_name'] ?? 'Cash') ?></span>
    </div>
    <?php if (!empty($payment['transaction_id'])): ?>
    <div class="receipt-row">
      <span class="receipt-label">Transaction ID</span>
      <span class="receipt-value font-monospace small"><?= Helper::e($payment['transaction_id']) ?></span>
    </div>
    <?php endif; ?>
    <div class="receipt-row">
      <span class="receipt-label">Status</span>
      <span class="receipt-value">
        <span class="status-<?= $payment['status'] === 'completed' ? 'paid' : 'pending' ?>">
          <?= strtoupper($payment['status']) ?>
        </span>
      </span>
    </div>
    <?php if (!empty($payment['notes'])): ?>
    <div class="mt-3 p-3 rounded-2" style="background:#f8fafc;font-size:.82rem;color:#64748b">
      <strong>Notes:</strong> <?= Helper::e($payment['notes']) ?>
    </div>
    <?php endif; ?>
  </div>

  <!-- Footer -->
  <div class="receipt-footer">
    <p style="font-size:.75rem;color:#94a3b8;margin:0">This is a computer-generated receipt. No signature required.</p>
    <p style="font-size:.75rem;color:#94a3b8;margin:4px 0 0">Generated on <?= date('d M Y, H:i') ?> &bull; <?= Helper::e($settings['company_name'] ?? APP_NAME) ?></p>
  </div>
</div>

</body>
</html>
