<?php /** /app/views/admin/produtos/index.php */ ?>

<div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:20px">
  <div></div>
  <a href="<?= admin_url('produtos/criar') ?>" class="btn btn-gold">+ Novo produto</a>
</div>

<form method="get" class="filter-bar">
  <input type="text" name="busca" class="form-control" placeholder="Nome ou SKU..." value="<?= e($filtros['busca'] ?? '') ?>">

  <select name="categoria_id" class="form-select">
    <option value="">Todas categorias</option>
    <?php foreach ($categorias as $c): ?>
      <option value="<?= (int)$c['id'] ?>" <?= ($filtros['categoria_id'] ?? '') == $c['id'] ? 'selected' : '' ?>>
        <?= e($c['nome']) ?>
      </option>
    <?php endforeach; ?>
  </select>

  <select name="ativo" class="form-select">
    <option value="">Todos</option>
    <option value="1" <?= ($filtros['ativo'] ?? '') === '1' ? 'selected' : '' ?>>Ativos</option>
    <option value="0" <?= ($filtros['ativo'] ?? '') === '0' ? 'selected' : '' ?>>Inativos</option>
  </select>

  <button type="submit" class="btn">Filtrar</button>
  <a href="<?= admin_url('produtos') ?>" class="btn btn-light">Limpar</a>
</form>

<?php if (empty($produtos)): ?>
  <div class="card" style="text-align:center;padding:60px">
    <h3>Nenhum produto encontrado</h3>
    <p style="color:var(--admin-muted)">Comece cadastrando o primeiro modelo.</p>
    <a href="<?= admin_url('produtos/criar') ?>" class="btn btn-gold" style="margin-top:12px">+ Novo produto</a>
  </div>
<?php else: ?>
<table class="table">
  <thead>
    <tr>
      <th style="width:80px"></th>
      <th>Produto</th>
      <th>SKU</th>
      <th>Categoria</th>
      <th>Preço</th>
      <th>Status</th>
      <th>Ações</th>
    </tr>
  </thead>
  <tbody>
    <?php foreach ($produtos as $p): ?>
    <tr>
      <td>
        <?php if (!empty($p['imagem_principal'])): ?>
          <img class="thumb" src="<?= upload_url($p['imagem_principal']) ?>" alt="">
        <?php else: ?>
          <div class="thumb" style="display:flex;align-items:center;justify-content:center;color:#c3bcad">◎</div>
        <?php endif; ?>
      </td>
      <td>
        <strong><?= e($p['nome']) ?></strong>
        <?php if ($p['destaque']): ?>
          <span class="badge badge-warning">Destaque</span>
        <?php endif; ?>
      </td>
      <td><code><?= e($p['sku']) ?></code></td>
      <td><?= e($p['categoria_nome'] ?? '-') ?></td>
      <td><?= $p['mostrar_preco'] && $p['preco'] ? price((float)$p['preco']) : '-' ?></td>
      <td>
        <?php if ($p['ativo']): ?>
          <span class="badge badge-success">Ativo</span>
        <?php else: ?>
          <span class="badge badge-secondary">Inativo</span>
        <?php endif; ?>
      </td>
      <td style="white-space:nowrap">
        <a href="<?= admin_url('produtos/editar/' . $p['id']) ?>" class="btn btn-sm btn-outline">Editar</a>
        <a href="<?= admin_url('produtos/toggle-ativo/' . $p['id']) ?>" class="btn btn-sm btn-light" title="Ativar/Inativar">
          <?= $p['ativo'] ? '⏸' : '▶' ?>
        </a>
        <a href="<?= admin_url('produtos/toggle-destaque/' . $p['id']) ?>" class="btn btn-sm btn-light" title="Destaque">★</a>
        <a href="<?= admin_url('produtos/excluir/' . $p['id']) ?>" class="btn btn-sm btn-danger"
           onclick="return confirm('Excluir este produto?')">×</a>
      </td>
    </tr>
    <?php endforeach; ?>
  </tbody>
</table>

<?php if ($pagination['pages'] > 1): ?>
  <div class="pagination">
    <?php
      $params = $_GET; unset($params['page']);
      $base = admin_url('produtos') . '?' . http_build_query($params);
      $sep = str_contains($base, '?') ? '&' : '?';
      for ($i = 1; $i <= $pagination['pages']; $i++):
    ?>
      <?php if ($i === $pagination['page']): ?>
        <span class="current"><?= $i ?></span>
      <?php else: ?>
        <a href="<?= $base . $sep . 'page=' . $i ?>"><?= $i ?></a>
      <?php endif; ?>
    <?php endfor; ?>
  </div>
<?php endif; ?>
<?php endif; ?>
