<?php /** /app/views/admin/pedidos/ver.php */
$st = status_pedido()[$pedido['status']] ?? null;
?>

<div style="margin-bottom:16px">
  <a href="<?= admin_url('pedidos') ?>">← Voltar aos pedidos</a>
</div>

<div style="display:grid;grid-template-columns:1.6fr 1fr;gap:20px;align-items:start">

  <div>
    <div class="card">
      <div style="display:flex;justify-content:space-between;align-items:center;flex-wrap:wrap;gap:12px">
        <div>
          <div style="font-size:.75rem;letter-spacing:.2em;text-transform:uppercase;color:var(--admin-muted)">Código</div>
          <h2 style="margin:4px 0;font-family:'Cormorant Garamond',serif;letter-spacing:.05em"><?= e($pedido['codigo']) ?></h2>
        </div>
        <?php if ($st): ?>
          <span class="badge badge-<?= e($st['cor']) ?>" style="font-size:.85rem;padding:6px 14px"><?= e($st['label']) ?></span>
        <?php endif; ?>
      </div>
      <small style="color:var(--admin-muted)">Pedido realizado em <?= date_br($pedido['created_at'], true) ?></small>
    </div>

    <div class="card">
      <h3 style="margin-top:0">Itens do pedido</h3>
      <table class="table" style="box-shadow:none">
        <thead>
          <tr><th style="width:60px"></th><th>Produto</th><th>SKU</th><th>Qtd</th><th>Subtotal</th></tr>
        </thead>
        <tbody>
          <?php foreach ($itens as $it): ?>
          <tr>
            <td>
              <?php if (!empty($it['imagem'])): ?>
                <img class="thumb" src="<?= upload_url($it['imagem']) ?>" alt="">
              <?php endif; ?>
            </td>
            <td>
              <strong><?= e($it['produto_nome']) ?></strong>
              <?php if (!empty($it['variacao_descricao'])): ?>
                <br><small style="color:var(--admin-muted)"><?= e($it['variacao_descricao']) ?></small>
              <?php endif; ?>
            </td>
            <td><code><?= e($it['produto_sku']) ?></code></td>
            <td><?= (int)$it['quantidade'] ?></td>
            <td><?= $it['subtotal'] ? price((float)$it['subtotal']) : '-' ?></td>
          </tr>
          <?php endforeach; ?>
        </tbody>
        <?php if ($pedido['total_produtos']): ?>
        <tfoot>
          <tr>
            <td colspan="4" style="text-align:right"><strong>Total estimado</strong></td>
            <td><strong style="color:var(--admin-gold)"><?= price((float)$pedido['total_produtos']) ?></strong></td>
          </tr>
        </tfoot>
        <?php endif; ?>
      </table>
    </div>

    <?php if (!empty($pedido['observacoes'])): ?>
    <div class="card">
      <h3 style="margin-top:0">Observações do cliente</h3>
      <p style="white-space:pre-wrap;margin:0"><?= e($pedido['observacoes']) ?></p>
    </div>
    <?php endif; ?>
  </div>

  <div>
    <div class="card">
      <h3 style="margin-top:0">Dados do cliente</h3>
      <dl style="margin:0;font-size:.92rem;line-height:1.8">
        <dt style="color:var(--admin-muted);font-size:.78rem;text-transform:uppercase;letter-spacing:.1em">Nome</dt>
        <dd style="margin:0 0 10px"><?= e($pedido['cliente_nome']) ?></dd>

        <?php if ($pedido['cliente_empresa']): ?>
        <dt style="color:var(--admin-muted);font-size:.78rem;text-transform:uppercase;letter-spacing:.1em">Empresa</dt>
        <dd style="margin:0 0 10px"><?= e($pedido['cliente_empresa']) ?></dd>
        <?php endif; ?>

        <dt style="color:var(--admin-muted);font-size:.78rem;text-transform:uppercase;letter-spacing:.1em">E-mail</dt>
        <dd style="margin:0 0 10px"><a href="mailto:<?= e($pedido['cliente_email']) ?>"><?= e($pedido['cliente_email']) ?></a></dd>

        <dt style="color:var(--admin-muted);font-size:.78rem;text-transform:uppercase;letter-spacing:.1em">Telefone</dt>
        <dd style="margin:0 0 10px">
          <?= e($pedido['cliente_telefone']) ?>
          <a href="<?= e(whatsapp_link($pedido['cliente_telefone'])) ?>" target="_blank" style="margin-left:8px;font-size:.82rem">WhatsApp</a>
        </dd>

        <?php if ($pedido['cliente_documento']): ?>
        <dt style="color:var(--admin-muted);font-size:.78rem;text-transform:uppercase;letter-spacing:.1em">CPF/CNPJ</dt>
        <dd style="margin:0 0 10px"><?= e($pedido['cliente_documento']) ?></dd>
        <?php endif; ?>

        <?php if ($pedido['cliente_cidade'] || $pedido['cliente_estado']): ?>
        <dt style="color:var(--admin-muted);font-size:.78rem;text-transform:uppercase;letter-spacing:.1em">Localização</dt>
        <dd style="margin:0 0 10px">
          <?= e($pedido['cliente_cidade']) ?>
          <?php if ($pedido['cliente_estado']): ?> / <?= e($pedido['cliente_estado']) ?><?php endif; ?>
        </dd>
        <?php endif; ?>
      </dl>
    </div>

    <div class="card">
      <h3 style="margin-top:0">Atualizar status</h3>
      <form action="<?= admin_url('pedidos/status') ?>" method="post">
        <?= csrf_field() ?>
        <input type="hidden" name="id" value="<?= (int)$pedido['id'] ?>">
        <select name="status" class="form-select">
          <?php foreach (status_pedido() as $k => $s): ?>
            <option value="<?= e($k) ?>" <?= $pedido['status'] === $k ? 'selected' : '' ?>><?= e($s['label']) ?></option>
          <?php endforeach; ?>
        </select>
        <button type="submit" class="btn btn-gold" style="width:100%;margin-top:12px">Atualizar status</button>
      </form>
    </div>
  </div>
</div>
