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

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

  <div class="card">
    <h3 style="margin-top:0">Coleções</h3>

    <?php if (empty($items)): ?>
      <p style="color:var(--admin-muted)">Nenhuma coleção cadastrada ainda.</p>
    <?php else: ?>
    <table class="table" style="box-shadow:none">
      <thead>
        <tr><th>Nome</th><th>Ano</th><th>Ordem</th><th>Status</th><th></th></tr>
      </thead>
      <tbody>
        <?php foreach ($items as $c): ?>
        <tr>
          <td><strong><?= e($c['nome']) ?></strong></td>
          <td><?= e($c['ano'] ?? '-') ?></td>
          <td><?= (int)$c['ordem'] ?></td>
          <td>
            <?php if ($c['ativo']): ?><span class="badge badge-success">Ativa</span>
            <?php else: ?><span class="badge badge-secondary">Inativa</span><?php endif; ?>
          </td>
          <td style="white-space:nowrap">
            <button type="button" class="btn btn-sm btn-outline" onclick='editar(<?= json_encode($c) ?>)'>Editar</button>
            <a href="<?= admin_url('colecoes/excluir/' . $c['id']) ?>" class="btn btn-sm btn-danger" onclick="return confirm('Excluir?')">×</a>
          </td>
        </tr>
        <?php endforeach; ?>
      </tbody>
    </table>
    <?php endif; ?>
  </div>

  <div class="card">
    <h3 style="margin-top:0" id="form-title">Nova coleção</h3>

    <form id="colForm" action="<?= admin_url('colecoes/salvar') ?>" method="post">
      <?= csrf_field() ?>
      <input type="hidden" name="id" id="col-id" value="0">

      <div class="form-group">
        <label>Nome *</label>
        <input type="text" name="nome" id="col-nome" class="form-control" required>
      </div>

      <div class="form-group">
        <label>Slug</label>
        <input type="text" name="slug" id="col-slug" class="form-control">
      </div>

      <div class="form-row">
        <div class="form-group">
          <label>Ano</label>
          <input type="number" name="ano" id="col-ano" class="form-control" value="<?= date('Y') ?>">
        </div>
        <div class="form-group">
          <label>Ordem</label>
          <input type="number" name="ordem" id="col-ordem" class="form-control" value="0">
        </div>
      </div>

      <div class="form-group">
        <label>Descrição</label>
        <textarea name="descricao" id="col-descricao" class="form-control" rows="2"></textarea>
      </div>

      <label class="form-check">
        <input type="checkbox" name="ativo" id="col-ativo" value="1" checked> Ativa
      </label>

      <button type="submit" class="btn btn-gold" style="width:100%;margin-top:12px">Salvar</button>
      <button type="button" class="btn btn-light" style="width:100%;margin-top:8px" onclick="resetForm()">Limpar</button>
    </form>
  </div>
</div>

<script>
function editar(c) {
  document.getElementById('form-title').textContent = 'Editando: ' + c.nome;
  document.getElementById('col-id').value = c.id;
  document.getElementById('col-nome').value = c.nome;
  document.getElementById('col-slug').value = c.slug;
  document.getElementById('col-ano').value = c.ano || '';
  document.getElementById('col-descricao').value = c.descricao || '';
  document.getElementById('col-ordem').value = c.ordem;
  document.getElementById('col-ativo').checked = c.ativo == 1;
  window.scrollTo({top:0,behavior:'smooth'});
}
function resetForm() {
  document.getElementById('form-title').textContent = 'Nova coleção';
  document.getElementById('colForm').reset();
  document.getElementById('col-id').value = 0;
}
</script>
