<!DOCTYPE html>
<html lang="pt">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Relatório — <?= h($event['name']) ?></title>
<style>
*{box-sizing:border-box;margin:0;padding:0;}
body{font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;color:#1a1a1a;background:#f5f5f5;font-size:13px;}

/* Barra de acções — oculta na impressão */
.actions{background:#1a1a1a;padding:14px 32px;display:flex;align-items:center;gap:12px;position:sticky;top:0;z-index:10;}
.actions h1{color:#fff;font-size:15px;font-weight:600;flex:1;}
.btn-print{background:#fff;color:#1a1a1a;border:none;padding:8px 20px;border-radius:7px;font-size:13px;font-weight:700;cursor:pointer;font-family:inherit;}
.btn-print:hover{background:#f0f0f0;}
.btn-back{background:rgba(255,255,255,.1);color:#fff;border:1px solid rgba(255,255,255,.2);padding:7px 16px;border-radius:7px;font-size:13px;cursor:pointer;font-family:inherit;text-decoration:none;}

/* Documento */
.doc{max-width:900px;margin:24px auto;padding:0 24px 60px;}

/* Cabeçalho */
.header{background:#1a1a1a;color:#fff;border-radius:12px;padding:32px;margin-bottom:20px;display:flex;justify-content:space-between;align-items:flex-start;gap:20px;}
.header-left h2{font-size:11px;font-weight:600;letter-spacing:.1em;text-transform:uppercase;color:#9ca3af;margin-bottom:6px;}
.header-left h1{font-size:26px;font-weight:800;line-height:1.2;margin-bottom:8px;}
.header-left p{font-size:13px;color:#9ca3af;}
.header-right{text-align:right;flex-shrink:0;}
.header-right .label{font-size:10px;text-transform:uppercase;letter-spacing:.08em;color:#9ca3af;margin-bottom:3px;}
.header-right .val{font-size:14px;font-weight:600;color:#fff;}

/* Resumo geral */
.summary{display:grid;grid-template-columns:repeat(5,1fr);gap:10px;margin-bottom:20px;}
.summary-card{background:#fff;border:1px solid #e5e5e5;border-radius:10px;padding:14px 16px;}
.summary-card .s-val{font-size:22px;font-weight:800;color:#1a1a1a;margin-bottom:2px;}
.summary-card .s-lbl{font-size:10px;font-weight:600;text-transform:uppercase;letter-spacing:.07em;color:#9ca3af;}

/* Tabela de encomendas */
.orders-table{width:100%;border-collapse:collapse;background:#fff;border-radius:12px;overflow:hidden;border:1px solid #e5e5e5;}
.orders-table thead tr{background:#f8f8f8;}
.orders-table th{padding:11px 14px;text-align:left;font-size:10px;font-weight:700;text-transform:uppercase;letter-spacing:.07em;color:#9ca3af;border-bottom:2px solid #e5e5e5;white-space:nowrap;}
.orders-table td{padding:0;border-bottom:1px solid #f0f0f0;vertical-align:top;}
.orders-table tr:last-child td{border-bottom:none;}
.orders-table tr:hover td{background:#fafafa;}

/* Célula expandida com fotos */
.order-main{padding:12px 14px;}
.order-num{font-family:monospace;font-size:12px;font-weight:800;color:#6d28d9;}
.order-client{font-weight:600;font-size:13px;color:#1a1a1a;margin-top:2px;}
.order-meta{font-size:11px;color:#9ca3af;margin-top:1px;}
.order-photos{padding:8px 14px 12px;border-top:1px solid #f5f5f5;}
.photo-row{display:flex;justify-content:space-between;padding:3px 0;font-size:12px;color:#6b7280;}
.photo-row .pname{font-family:monospace;font-size:11px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;max-width:280px;}
.photo-row .pqty{font-weight:700;color:#1a1a1a;flex-shrink:0;}
.badge{display:inline-block;padding:2px 8px;border-radius:999px;font-size:10px;font-weight:700;}
.b-pending{background:#fef9c3;color:#854d0e;}
.b-paid{background:#dcfce7;color:#166534;}
.b-sent{background:#dbeafe;color:#1d4ed8;}
.b-cancelled{background:#fee2e2;color:#991b1b;}

/* Totais finais */
.totals-box{background:#1a1a1a;color:#fff;border-radius:12px;padding:20px 24px;margin-top:16px;display:flex;justify-content:space-between;align-items:center;}
.totals-box .t-label{font-size:12px;color:#9ca3af;margin-bottom:2px;}
.totals-box .t-val{font-size:18px;font-weight:800;}

/* Rodapé */
.footer{margin-top:24px;text-align:center;font-size:11px;color:#9ca3af;}

@media print {
  .actions{display:none!important;}
  body{background:#fff;}
  .doc{margin:0;padding:16px;max-width:100%;}
  .header{-webkit-print-color-adjust:exact;print-color-adjust:exact;}
  .totals-box{-webkit-print-color-adjust:exact;print-color-adjust:exact;}
  .summary-card{break-inside:avoid;}
  .orders-table tr{break-inside:avoid;}
}
</style>
</head>
<body>

<!-- Barra acções -->
<div class="actions">
  <h1>📊 Relatório — <?= h($event['name']) ?></h1>
  <button class="btn-print" onclick="window.print()">🖨 Imprimir / PDF</button>
</div>

<div class="doc">

  <?php
  // Calcular totais
  $totalEncomendas  = count($orders);
  $totalFotos       = array_sum(array_column($orders,'total_fotos'));
  $totalReceita     = array_sum(array_column($orders,'total'));
  $totalPendente    = array_sum(array_map(fn($o)=>$o['status']==='pending'?$o['total']:0, $orders));
  $totalPago        = array_sum(array_map(fn($o)=>in_array($o['status'],['paid','sent'])?$o['total']:0, $orders));
  ?>

  <!-- Cabeçalho -->
  <div class="header">
    <div class="header-left">
      <h2>Relatório de Encomendas</h2>
      <h1><?= h($event['name']) ?></h1>
      <p>
        <?php if($event['date_event']): ?>📅 <?= date('d/m/Y',strtotime($event['date_event'])) ?> &nbsp;·&nbsp; <?php endif; ?>
        Gerado em <?= date('d/m/Y \à\s H:i') ?>
      </p>
    </div>
    <div class="header-right">
      <div class="label">Prefixo</div>
      <div class="val"><?= h($event['order_prefix'] ?: '—') ?></div>
      <div class="label" style="margin-top:10px;">Receita Total</div>
      <div class="val" style="font-size:22px;"><?= money($totalReceita) ?></div>
    </div>
  </div>

  <!-- Resumo -->
  <div class="summary">
    <div class="summary-card">
      <div class="s-val"><?= $totalEncomendas ?></div>
      <div class="s-lbl">Encomendas</div>
    </div>
    <div class="summary-card">
      <div class="s-val"><?= $totalFotos ?></div>
      <div class="s-lbl">Fotos Total</div>
    </div>
    <div class="summary-card">
      <div class="s-val" style="color:#166534;"><?= money($totalPago) ?></div>
      <div class="s-lbl">Recebido</div>
    </div>
    <div class="summary-card">
      <div class="s-val" style="color:#854d0e;"><?= money($totalPendente) ?></div>
      <div class="s-lbl">Pendente</div>
    </div>
    <div class="summary-card">
      <div class="s-val"><?= count(array_filter($orders,fn($o)=>$o['status']==='pending')) ?></div>
      <div class="s-lbl">Por aprovar</div>
    </div>
  </div>

  <?php if(empty($orders)): ?>
  <div style="background:#fff;border:1px solid #e5e5e5;border-radius:12px;padding:48px;text-align:center;color:#9ca3af;">
    <div style="font-size:32px;margin-bottom:8px;">📦</div>
    <div style="font-weight:600;">Nenhuma encomenda para este evento.</div>
  </div>
  <?php else: ?>

  <!-- Tabela de todas as encomendas -->
  <table class="orders-table">
    <thead>
      <tr>
        <th style="width:200px;">Cliente</th>
        <th>Contacto</th>
        <th style="text-align:center;">Fotos</th>
        <th>Fotografias encomendadas</th>
        <th style="text-align:right;">Total</th>
        <th>Estado</th>
      </tr>
    </thead>
    <tbody>
    <?php foreach($orders as $o): ?>
    <tr>
      <td>
        <div class="order-main">
          <div class="order-num"><?= h($o['order_number']) ?></div>
          <div class="order-client"><?= h($o['customer_name']) ?></div>
          <div class="order-meta"><?= date('d/m/Y',strtotime($o['created_at'])) ?></div>
          <?php if(!empty($o['nif'])): ?>
          <div class="order-meta">NIF: <?= h($o['nif']) ?></div>
          <?php endif; ?>
        </div>
      </td>
      <td>
        <div class="order-main">
          <div style="font-size:12px;"><?= h($o['phone']) ?></div>
          <?php if(!empty($o['email'])): ?>
          <div style="font-size:11px;color:#9ca3af;margin-top:2px;"><?= h($o['email']) ?></div>
          <?php endif; ?>
          <div style="font-size:11px;color:#9ca3af;margin-top:4px;">
            <?= match($o['shipping_method']){'ctt'=>'📦 CTT','internacional'=>'✈️ Intl',default=>'💍 Noivos'} ?>
            <?php if($o['address']): ?> · <?= h(str_replace("\n",' ',$o['address'])) ?><?php endif; ?>
          </div>
        </div>
      </td>
      <td style="text-align:center;">
        <div class="order-main">
          <span style="font-size:18px;font-weight:800;"><?= $o['total_fotos'] ?></span>
          <?php if($o['photos_free']>0): ?>
          <div style="font-size:10px;color:#166534;">+<?= $o['photos_free'] ?> oferta</div>
          <?php endif; ?>
        </div>
      </td>
      <td>
        <?php if(!empty($o['items'])): ?>
        <div class="order-photos">
          <?php foreach($o['items'] as $item): ?>
          <div class="photo-row">
            <span class="pname"><?= h(fname($item['filename']??'')) ?></span>
            <span class="pqty" style="margin-left:8px;">×<?= $item['quantity'] ?></span>
          </div>
          <?php endforeach; ?>
        </div>
        <?php endif; ?>
      </td>
      <td style="text-align:right;">
        <div class="order-main">
          <div style="font-size:15px;font-weight:800;"><?= money($o['total']) ?></div>
          <?php if($o['shipping_cost']>0): ?>
          <div style="font-size:10px;color:#9ca3af;">incl. <?= money($o['shipping_cost']) ?> envio</div>
          <?php endif; ?>
        </div>
      </td>
      <td>
        <div class="order-main">
          <span class="badge b-<?= $o['status'] ?>"><?= match($o['status']){'pending'=>'Pendente','paid'=>'Pago','sent'=>'Enviado',default=>'Cancelado'} ?></span>
        </div>
      </td>
    </tr>
    <?php endforeach; ?>
    </tbody>
  </table>

  <!-- Totais finais -->
  <div class="totals-box">
    <div>
      <div class="t-label">Total de encomendas</div>
      <div class="t-val"><?= $totalEncomendas ?> encomenda<?= $totalEncomendas!=1?'s':'' ?> · <?= $totalFotos ?> fotografia<?= $totalFotos!=1?'s':'' ?></div>
    </div>
    <div style="text-align:center;">
      <div class="t-label">Recebido</div>
      <div class="t-val" style="color:#4ade80;"><?= money($totalPago) ?></div>
    </div>
    <div style="text-align:center;">
      <div class="t-label">Pendente</div>
      <div class="t-val" style="color:#fbbf24;"><?= money($totalPendente) ?></div>
    </div>
    <div style="text-align:right;">
      <div class="t-label">RECEITA TOTAL</div>
      <div class="t-val" style="font-size:26px;"><?= money($totalReceita) ?></div>
    </div>
  </div>

  <?php endif; ?>

  <div class="footer">
    <?= h(config('app.name')) ?> &nbsp;·&nbsp; Relatório: <?= h($event['name']) ?> &nbsp;·&nbsp; <?= date('d/m/Y H:i') ?>
  </div>

</div>

<script>
// Imprimir automaticamente
setTimeout(() => window.print(), 500);
</script>
</body>
</html>
