<?php
require_once __DIR__ . '/includes/config.php';

$type = isset($_GET['type']) && in_array($_GET['type'], ['blog','news','articolo'], true) ? $_GET['type'] : '';
$catSlug = $_GET['cat'] ?? '';
$search = trim($_GET['q'] ?? '');

$where = ["p.status='published'"];
$params = [];
if ($type)    { $where[] = "p.type = :type"; $params[':type'] = $type; }
if ($catSlug) { $where[] = "c.slug = :cat"; $params[':cat'] = $catSlug; }
if ($search)  { $where[] = "(p.title LIKE :s OR p.body LIKE :s OR p.tags LIKE :s)"; $params[':s'] = '%'.$search.'%'; }
$whereSql = implode(' AND ', $where);

$posts = q("SELECT p.*, c.name cat_name, c.slug cat_slug FROM posts p LEFT JOIN categories c ON c.id=p.category_id WHERE $whereSql ORDER BY p.featured DESC, p.published_at DESC", $params);
$cats = q("SELECT * FROM categories ORDER BY name");

$pageTitle = 'Blog, News & Casi Studio | ' . SITE_NAME;
$pageDesc  = 'Strategie di ottimizzazione fiscale, novità e casi reali spiegati con numeri da Giovanni Battista Franzone.';
require __DIR__ . '/includes/header.php';
?>
<section class="page-hero">
  <div class="container">
    <span class="kicker" style="color:var(--gold-soft);border-color:rgba(201,162,75,.5)">Approfondimenti</span>
    <h1>Blog, News &amp; Casi Studio</h1>
    <p>Strategie fiscali, novità e casi reali spiegati con numeri. Tutto ciò che serve per smettere di regalare il 40% del tuo utile.</p>
  </div>
</section>

<section class="section">
  <div class="container">
    <div class="faq-tabs reveal" style="margin-bottom:24px">
      <a class="faq-tab <?= !$type?'active':'' ?>" href="<?= url('blog.php') ?>">Tutti</a>
      <a class="faq-tab <?= $type==='blog'?'active':'' ?>" href="<?= url('blog.php?type=blog') ?>">Blog</a>
      <a class="faq-tab <?= $type==='news'?'active':'' ?>" href="<?= url('blog.php?type=news') ?>">News</a>
      <a class="faq-tab <?= $type==='articolo'?'active':'' ?>" href="<?= url('blog.php?type=articolo') ?>">Casi Studio</a>
    </div>
    <form method="get" action="<?= url('blog.php') ?>" style="max-width:520px;margin:0 auto 40px;display:flex;gap:10px">
      <input type="text" name="q" value="<?= e($search) ?>" placeholder="Cerca un argomento…" style="flex:1;padding:14px 18px;border:1px solid var(--line);border-radius:100px;font-family:var(--ff-body);font-size:1rem">
      <button class="btn btn-ink" type="submit">Cerca</button>
    </form>

    <?php if (!$posts): ?>
      <p class="muted" style="text-align:center;padding:60px 0">Nessun contenuto trovato<?= $search ? ' per “'.e($search).'”' : '' ?>.</p>
    <?php else: ?>
    <div class="post-grid">
      <?php foreach ($posts as $p): ?>
      <a class="post-card reveal" href="<?= url('post.php?slug='.urlencode($p['slug'])) ?>">
        <div class="thumb"><?php if($p['cover']): ?><img src="<?= e(uploads_url('images/'.$p['cover'])) ?>" alt="<?= e($p['title']) ?>"><?php else: ?><span class="ph">F</span><?php endif; ?></div>
        <div class="body">
          <span class="cat"><span class="badge <?= e($p['type']) ?>"><?= e($p['type']) ?></span> <?= e($p['cat_name'] ?? '') ?></span>
          <h3><?= e($p['title']) ?></h3>
          <p><?= e($p['excerpt'] ?: excerpt_from($p['body'])) ?></p>
          <div class="meta"><span><?= it_date($p['published_at']) ?></span><span><?= reading_time($p['body']) ?> min lettura</span><span><?= (int)$p['views'] ?> letture</span></div>
        </div>
      </a>
      <?php endforeach; ?>
    </div>
    <?php endif; ?>
  </div>
</section>
<?php require __DIR__ . '/includes/footer.php'; ?>
