<?php
// public/atas.php
require_once __DIR__ . '/../src/db.php';
require_once __DIR__ . '/../src/helpers.php';

$st = $pdo->query("
  SELECT a.*, s.tipo, s.numero, DATE_FORMAT(s.data_sessao,'%d/%m/%Y') AS s_data
  FROM atas a
  LEFT JOIN sessoes s ON s.id=a.sessao_id
  WHERE a.publicar_site=1
  ORDER BY COALESCE(a.data_ata, a.created_at) DESC, a.id DESC
");
$atas = $st->fetchAll();
?>
<!doctype html>
<html lang="pt-br">
<head>
<meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
<title>Atas</title>
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
<style>
  body{font-family:ui-sans-serif,system-ui;background:#f6f8fb;color:#0f172a;margin:0}
  .wrap{max-width:1000px;margin:22px auto;padding:0 16px}
  .card{background:#fff;border:1px solid #e5e7eb;border-radius:14px;box-shadow:0 10px 26px rgba(2,6,23,.06);padding:14px;margin-bottom:12px}
  .muted{color:#64748b}
  .btn{display:inline-flex;gap:8px;align-items:center;padding:.48rem .78rem;border-radius:10px;border:2px solid #e5e7eb;background:#fff;font-weight:800;text-decoration:none}
  .btn-primary{background:#2563eb;border-color:#2563eb;color:#fff}
</style>
</head>
<body>
  <div class="wrap">
    <h1 style="margin:0 0 12px">Atas</h1>
    <?php if(!$atas): ?>
      <div class="muted">Nenhuma ata publicada.</div>
    <?php else: foreach($atas as $a): ?>
      <article class="card">
        <h3 style="margin:.2rem 0"><?= h($a['titulo']) ?></h3>
        <div class="muted" style="margin:4px 0 10px">
          <?php if(!empty($a['data_ata'])): ?>
            Data: <?= h(date('d/m/Y', strtotime($a['data_ata']))) ?> ·
          <?php endif; ?>
          Publicada em: <?= h(date('d/m/Y H:i', strtotime($a['created_at']))) ?>
          <?php if($a['sessao_id']): ?>
            · Sessão #<?= (int)$a['sessao_id'] ?> (<?= h(($a['tipo']??'').' '.($a['numero']??'')) ?> — <?= h($a['s_data']??'') ?>)
          <?php endif; ?>
        </div>
        <?php if(!empty($a['resumo'])): ?>
          <p><?= nl2br(h($a['resumo'])) ?></p>
        <?php endif; ?>
        <div style="display:flex;gap:8px;flex-wrap:wrap;margin-top:10px">
          <?php if(!empty($a['texto_html'])): ?>
            <a class="btn btn-primary" href="ver_ata.php?id=<?= (int)$a['id'] ?>"><i class="fa fa-eye"></i> Ver online</a>
          <?php endif; ?>
          <?php if(!empty($a['pdf_path'])): ?>
            <a class="btn" target="_blank" href="/arapiraca/<?= ltrim($a['pdf_path'],'/') ?>"><i class="fa fa-file-pdf"></i> PDF</a>
          <?php endif; ?>
        </div>
      </article>
    <?php endforeach; endif; ?>
  </div>
</body>
</html>
