<?php
$title = 'Encomendas';
$breadcrumbs = [['Encomendas', null]];
?>

<!-- Filtros -->
<form method="get" style="display:flex;gap:8px;flex-wrap:wrap;margin-bottom:16px;align-items:center;">
  <select name="status" onchange="this.form.submit()" class="input" style="width:auto;padding:9px 12px;">
    <option value="">Todos os estados</option>
    <?php foreach(['pending'=>'Pendente','paid'=>'Pago','sent'=>'Enviado','cancelled'=>'Cancelado'] as $v=>$l): ?>
    <option value="<?= $v ?>" <?= ($filters['status']??'')===$v?'selected':'' ?>><?= $l ?></option>
    <?php endforeach; ?>
  </select>
  <select name="event_id" onchange="this.form.submit()" class="input" style="width:auto;padding:9px 12px;max-width:200px;">
    <option value="">Todos os eventos</option>
    <?php foreach($events as $ev): ?>
    <option value="<?= $ev['id'] ?>" <?= ($filters['event_id']??0)==$ev['id']?'selected':'' ?>><?= h($ev['name']) ?></option>
    <?php endforeach; ?>
  </select>
  <input type="text" name="q" value="<?= h($filters['q']??'') ?>" placeholder="🔍 Nome, email, número..."
         class="input" style="flex:1;min-width:180px;padding:9px 12px;">
  <button type="submit" class="btn btn-secondary">Filtrar</button>
  <?php if(!empty($filters['status'])||!empty($filters['q'])||!empty($filters['event_id'])): ?>
  <a href="<?= base_url('admin/encomendas') ?>" class="btn btn-ghost">✕ Limpar</a>
  <?php endif; ?>
</form>

<!-- Contagem -->
<div style="margin-bottom:12px;font-size:13px;color:#6E6E73;">
  <?= count($orders) ?> encomenda<?= count($orders)!=1?'s':'' ?> encontrada<?= count($orders)!=1?'s':'' ?>
</div>

<div class="card">
  <div class="tbl-wrap">
    <table class="tbl">
      <thead><tr>
        <th>Nº Encomenda</th>
        <th>Cliente</th>
        <th>Evento</th>
        <th style="text-align:center;">Fotos</th><th>Pagamento</th>
        <th style="text-align:right;">Total</th>
        <th>Estado</th>
        <th>Data</th>
        <th></th>
      </tr></thead>
      <tbody>
      <?php if(empty($orders)): ?>
      <tr><td colspan="8" class="tbl-empty">
        <div style="font-size:32px;margin-bottom:8px;">📦</div>
        <div style="font-weight:600;margin-bottom:4px;">Nenhuma encomenda encontrada</div>
        <div style="font-size:12px;">Quando os clientes fizerem pedidos, aparecem aqui</div>
      </td></tr>
      <?php else: ?>
      <?php foreach($orders as $o): ?>
      <?php $nItems = (int)(\App\Core\Database::val("SELECT COALESCE(SUM(quantity),0) FROM order_items WHERE order_id=?",[$o['id']])?:0); ?>
      <tr onclick="window.location='<?= base_url('admin/encomendas/'.$o['id']) ?>'" style="cursor:pointer;">
        <td><span class="text-mono" style="color:#7C3AED;font-weight:700;"><?= h($o['order_number']) ?></span></td>
        <td>
          <div class="tbl-name"><?= h($o['customer_name']) ?></div>
          <div class="tbl-sub"><?= h($o['email']) ?> · <?= h($o['phone']) ?></div>
        </td>
        <td class="text-muted" style="font-size:13px;"><?= h($o['event_name']?:'—') ?></td>
        <td style="text-align:center;font-weight:600;"><?= $nItems ?></td>
        <td><?= ($o['payment_method']??'dinheiro')=='mbway'?'📱 MBWay':'💵 Dinheiro' ?></td>
        <td style="text-align:right;"><strong style="font-size:15px;"><?= money($o['total']) ?></strong></td>
        <td><span class="badge badge-<?= $o['status'] ?>"><?= match($o['status']){'pending'=>'Pendente','paid'=>'Pago','sent'=>'Enviado',default=>'Cancelado'} ?></span></td>
        <td style="white-space:nowrap;font-size:12px;color:#6E6E73;"><?= date('d/m/y',strtotime($o['created_at'])) ?><br><span style="color:#AEAEB2;"><?= date('H:i',strtotime($o['created_at'])) ?></span></td>
        <td onclick="event.stopPropagation()">
          <a href="<?= base_url('admin/encomendas/'.$o['id']) ?>" class="btn btn-ghost btn-sm">Ver →</a>
        </td>
      </tr>
      <?php endforeach; ?>
      <?php endif; ?>
      </tbody>
    </table>
  </div>
</div>
