<?php /** /app/views/public/checkout.php */ ?>

<section style="padding: 60px 0">
  <div class="container">
    <div style="text-align:center;margin-bottom:48px">
      <span style="font-size:.78rem;letter-spacing:.32em;text-transform:uppercase;color:var(--color-gold)">Etapa final</span>
      <h1 style="margin:12px 0 0">Finalizar pedido</h1>
      <p style="color:var(--color-muted);margin-top:8px">Preencha seus dados. Nossa equipe entrará em contato para confirmação.</p>
    </div>

    <form action="<?= url('pedido/finalizar') ?>" method="post">
      <?= csrf_field() ?>

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

        <div class="card" style="background:#fff;padding:32px;border:1px solid var(--color-border)">
          <h3 style="margin-top:0;margin-bottom:24px;font-size:1.2rem">Seus dados</h3>

          <div class="form-group">
            <label>Nome completo *</label>
            <input type="text" name="cliente_nome" class="form-control" required>
          </div>

          <div class="form-row">
            <div class="form-group">
              <label>E-mail *</label>
              <input type="email" name="cliente_email" class="form-control" required>
            </div>
            <div class="form-group">
              <label>Telefone / WhatsApp *</label>
              <input type="text" name="cliente_telefone" class="form-control" required placeholder="(00) 00000-0000">
            </div>
          </div>

          <div class="form-group">
            <label>Empresa (opcional)</label>
            <input type="text" name="cliente_empresa" class="form-control">
          </div>

          <div class="form-row">
            <div class="form-group">
              <label>Cidade</label>
              <input type="text" name="cliente_cidade" class="form-control">
            </div>
            <div class="form-group">
              <label>Estado</label>
              <select name="cliente_estado" class="form-select">
                <option value="">Selecione</option>
                <?php foreach (estados_br() as $uf => $nome): ?>
                  <option value="<?= e($uf) ?>"><?= e($uf) ?> - <?= e($nome) ?></option>
                <?php endforeach; ?>
              </select>
            </div>
          </div>

          <div class="form-group">
            <label>CPF / CNPJ (opcional)</label>
            <input type="text" name="cliente_documento" class="form-control">
          </div>

          <div class="form-group">
            <label>Observações</label>
            <textarea name="observacoes" class="form-control" rows="4" placeholder="Alguma informação adicional sobre seu pedido?"></textarea>
          </div>
        </div>

        <div class="cart-summary">
          <h3 style="margin-top:0">Resumo do pedido</h3>

          <?php
            $temPreco = false; $total = 0;
            foreach ($itens as $it):
              if (!empty($it['preco_unitario'])) { $total += $it['preco_unitario'] * $it['quantidade']; $temPreco = true; }
          ?>
            <div style="display:flex;gap:12px;padding:12px 0;border-bottom:1px solid var(--color-border)">
              <?php if (!empty($it['imagem'])): ?>
                <img src="<?= upload_url($it['imagem']) ?>" alt="" style="width:60px;height:60px;object-fit:cover">
              <?php endif; ?>
              <div style="flex:1;font-size:.88rem">
                <div style="font-weight:500"><?= e($it['produto_nome']) ?></div>
                <div style="color:var(--color-muted);font-size:.78rem">
                  <?= e($it['produto_sku']) ?>
                  <?php if (!empty($it['variacao_descricao'])): ?> · <?= e($it['variacao_descricao']) ?><?php endif; ?>
                </div>
                <div style="margin-top:4px">Qtd: <strong><?= (int)$it['quantidade'] ?></strong></div>
              </div>
            </div>
          <?php endforeach; ?>

          <div style="display:flex;justify-content:space-between;padding:16px 0 8px">
            <span>Total de itens</span>
            <strong><?= cart_count() ?></strong>
          </div>

          <?php if ($temPreco): ?>
            <div style="display:flex;justify-content:space-between;padding:8px 0;border-top:1px solid var(--color-border);margin-top:8px">
              <span>Total estimado</span>
              <strong style="color:var(--color-gold);font-size:1.15rem"><?= price($total) ?></strong>
            </div>
          <?php endif; ?>

          <button type="submit" class="btn btn-gold btn-block" style="margin-top:20px">Confirmar pedido</button>
          <a href="<?= url('carrinho') ?>" style="display:block;text-align:center;margin-top:12px;font-size:.85rem">← Voltar ao pedido</a>
        </div>
      </div>
    </form>
  </div>
</section>
