<!DOCTYPE html>
<html lang="pt">
<head>
<meta charset="UTF-8">
<title>Recibo <?= h($order['order_number']) ?></title>
<style>
/* Ecrã */
* { box-sizing:border-box; margin:0; padding:0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, 'Helvetica Neue', Arial, sans-serif;
  background: #f5f5f7; color: #1d1d1f;
  display: flex; flex-direction: column; align-items: center;
  min-height: 100vh; padding: 32px 16px;
}
.actions {
  display: flex; gap: 10px; margin-bottom: 24px;
  width: 100%; max-width: 620px;
}
.btn-print {
  background: #1d1d1f; color: #fff; border: none;
  padding: 10px 22px; border-radius: 9px; font-size: 14px;
  font-weight: 600; cursor: pointer; font-family: inherit;
}
.btn-back {
  background: #f2f2f7; color: #1d1d1f; border: none;
  padding: 10px 18px; border-radius: 9px; font-size: 14px;
  font-weight: 500; cursor: pointer; font-family: inherit; text-decoration: none;
  display: inline-flex; align-items: center;
}

/* Documento */
.doc {
  width: 100%; max-width: 620px;
  background: #fff; border-radius: 16px;
  border: 1px solid #e5e5ea; overflow: hidden;
}

/* Cabeçalho */
.doc-header {
  background: #1d1d1f; padding: 28px 32px;
  display: flex; align-items: flex-start; justify-content: space-between;
}
.doc-header-left h1 { font-size: 20px; font-weight: 700; color: #fff; margin-bottom: 4px; }
.doc-header-left p  { font-size: 13px; color: #86868b; }
.doc-header-right   { text-align: right; }
.doc-order-num      { font-size: 22px; font-weight: 800; color: #fff; letter-spacing: .5px; font-family: monospace; }
.doc-date           { font-size: 12px; color: #86868b; margin-top: 4px; }

/* Secções */
.doc-body     { padding: 24px 32px; }
.doc-section  { margin-bottom: 24px; }
.section-title {
  font-size: 11px; font-weight: 700; text-transform: uppercase;
  letter-spacing: .08em; color: #86868b; margin-bottom: 12px;
  padding-bottom: 8px; border-bottom: 1px solid #f2f2f7;
}

/* Info cliente */
.info-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.info-item label { font-size: 11px; color: #86868b; font-weight: 500; display: block; margin-bottom: 2px; }
.info-item span  { font-size: 14px; font-weight: 600; color: #1d1d1f; }

/* Tabela de fotos */
.photos-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.photos-table th {
  text-align: left; padding: 8px 10px; font-size: 11px; font-weight: 600;
  text-transform: uppercase; letter-spacing: .06em; color: #86868b;
  background: #f9f9fb; border-bottom: 1px solid #e5e5ea;
}
.photos-table td { padding: 10px 10px; border-bottom: 1px solid #f2f2f7; color: #3a3a3c; }
.photos-table tr:last-child td { border-bottom: none; }
.photos-table .qty { text-align: center; font-weight: 700; color: #1d1d1f; }
.photos-table .fname { font-family: monospace; font-size: 12px; }

/* Nota de entrega */
.delivery-note {
  background: #f0f9ff; border: 1px solid #bae6fd;
  border-radius: 10px; padding: 14px 16px; margin-top: 20px;
}
.delivery-note p { font-size: 13px; color: #0369a1; line-height: 1.5; }
.delivery-note strong { color: #075985; }

/* Rodapé */
.doc-footer {
  background: #f9f9fb; border-top: 1px solid #e5e5ea;
  padding: 16px 32px; text-align: center;
}
.doc-footer p { font-size: 12px; color: #86868b; }

/* Estado */
.status-badge {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 4px 12px; border-radius: 999px; font-size: 12px; font-weight: 600;
}
.status-pending  { background: #fef3c7; color: #92400e; }
.status-paid     { background: #d1fae5; color: #065f46; }
.status-sent     { background: #dbeafe; color: #1e40af; }
.status-cancelled{ background: #fee2e2; color: #991b1b; }

/* IMPRESSÃO */
@media print {
  body { background: #fff; padding: 0; }
  .actions { display: none !important; }
  .doc { border: none; border-radius: 0; box-shadow: none; max-width: 100%; }
  .doc-header { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  .delivery-note { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}
</style>
</head>
<body>

<!-- Botões (ocultados na impressão) -->
<div class="actions">
  <a href="javascript:window.close()||history.back()" class="btn-back">← Voltar</a>
  <button class="btn-print" onclick="window.print()">🖨 Imprimir / Guardar PDF</button>
</div>

<!-- Documento -->
<div class="doc">

  <!-- Cabeçalho -->
  <div class="doc-header">
    <div class="doc-header-left">
      <h1>📸 <?= h(config('app.name')) ?></h1>
      <p>Comprovativo de Encomenda</p>
    </div>
    <div class="doc-header-right">
      <div class="doc-order-num"><?= h($order['order_number']) ?></div>
      <div class="doc-date"><?= date('d/m/Y \à\s H:i', strtotime($order['created_at'])) ?></div>
      <div style="margin-top:8px;">
        <span class="status-badge status-<?= $order['status'] ?>">
          <?= match($order['status']){'pending'=>'Pendente','paid'=>'Pago','sent'=>'Enviado',default=>'Cancelado'} ?>
        </span>
      </div>
    </div>
  </div>

  <div class="doc-body">

    <!-- Dados do cliente -->
    <div class="doc-section">
      <div class="section-title">Dados do Cliente</div>
      <div class="info-grid">
        <div class="info-item">
          <label>Nome</label>
          <span><?= h($order['customer_name']) ?></span>
        </div>
        <div class="info-item">
          <label>Telefone</label>
          <span><?= h($order['phone']) ?></span>
        </div>
        <?php if(!empty($order['email'])): ?>
        <div class="info-item">
          <label>Email</label>
          <span><?= h($order['email']) ?></span>
        </div>
        <?php endif; ?>
        <?php if(!empty($order['nif'])): ?>
        <div class="info-item">
          <label>NIF</label>
          <span><?= h($order['nif']) ?></span>
        </div>
        <?php endif; ?>
        <div class="info-item">
          <label>Evento</label>
          <span><?= h($order['event_name'] ?: '—') ?></span>
        </div>
      </div>
    </div>

    <!-- Entrega -->
    <div class="doc-section">
      <div class="section-title">Entrega e Pagamento</div>
      <div class="info-grid">
        <div class="info-item">
          <label>Método de entrega</label>
          <span><?= match($order['shipping_method']){
            'ctt'           => 'Envio CTT',
            'internacional' => 'Envio Internacional',
            default         => 'Entrega aos Noivos'
          } ?></span>
        </div>
        <div class="info-item">
          <label>Método de pagamento</label>
          <span><?= ($order['payment_method']??'dinheiro')==='mbway' ? '📱 MBWay' : '💵 Dinheiro' ?></span>
        </div>
        <?php if($order['address']): ?>
        <div class="info-item" style="grid-column:span 2;">
          <label>Morada</label>
          <span><?= nl2br(h($order['address'])) ?></span>
        </div>
        <?php endif; ?>
      </div>
    </div>

    <!-- Fotografias -->
    <div class="doc-section">
      <?php $totalCopias=array_sum(array_column($items,'quantity')); ?>
      <div class="section-title">Fotografias Encomendadas — <?= $totalCopias ?> cópia<?= $totalCopias!=1?'s':'' ?></div>
      <table class="photos-table">
        <thead>
          <tr>
            <th>#</th>
            <th>Fotografia</th>
            <th style="text-align:center;">Quantidade</th>
          </tr>
        </thead>
        <tbody>
          <?php foreach($items as $i => $item): ?>
          <tr>
            <td style="color:#86868b;font-size:12px;"><?= $i+1 ?></td>
            <td class="fname"><?= h(fname($item['filename'] ?? '')) ?></td>
            <td class="qty">×<?= $item['quantity'] ?></td>
          </tr>
          <?php endforeach; ?>
        </tbody>
      </table>

      <?php if($order['photos_free'] > 0): ?>
      <div style="margin-top:10px;padding:8px 10px;background:#f0fdf4;border-radius:8px;font-size:13px;color:#166534;">
        🎁 Inclui <strong><?= $order['photos_free'] ?> fotografia<?= $order['photos_free']!=1?'s':'' ?> grátis</strong> pela promoção aplicada.
      </div>
      <?php endif; ?>
    </div>

    <!-- Nota de pagamento -->
    <div class="delivery-note">
      <p>💵 <strong>Pagamento junto dos fotógrafos.</strong><br>
      O pagamento é realizado presencialmente no momento da entrega das suas fotografias. Guarde este comprovativo como prova da sua encomenda.</p>
    </div>

  </div><!-- /doc-body -->

  <!-- Rodapé -->
  <div class="doc-footer">
    <p><?= h(config('app.name')) ?> &nbsp;·&nbsp; Encomenda <?= h($order['order_number']) ?> &nbsp;·&nbsp; <?= date('d/m/Y', strtotime($order['created_at'])) ?></p>
  </div>

</div><!-- /doc -->

<script>
// Imprimir automaticamente apenas se vier da confirmação de encomenda
if (document.referrer.includes('obrigado') || new URLSearchParams(location.search).get('auto') === '1') {
  setTimeout(() => window.print(), 600);
}
</script>
</body>
</html>
