<?php
/**
 * 국민자료실 - 가이드 목록 페이지
 */
require_once __DIR__ . '/bootstrap.php';
require_once BASE_DIR . '/includes/functions.php';

$currentPage = 'guides';
logPageView('guides');
$pageTitle = '정책 가이드';
$pageDescription = '정부 지원정책 신청 방법, 자격 조건, 주의사항을 쉽게 정리한 가이드 모음. 청년, 주거, 복지, 채용 등 분야별 맞춤 안내.';

$db = getDB();

// 카테고리 필터
$category = $_GET['category'] ?? '';

$sql = "SELECT id, slug, title, description, category, author, view_count, published_at
        FROM gs_guides WHERE status = 'published'";
$params = [];

if ($category) {
    $sql .= " AND category = ?";
    $params[] = $category;
}
$sql .= " ORDER BY published_at DESC";

$stmt = $db->prepare($sql);
$stmt->execute($params);
$guides = $stmt->fetchAll(PDO::FETCH_ASSOC);

// 카테고리 목록
$categories = $db->query(
    "SELECT category, COUNT(*) as cnt FROM gs_guides WHERE status = 'published' GROUP BY category ORDER BY cnt DESC"
)->fetchAll(PDO::FETCH_ASSOC);

require_once BASE_DIR . '/templates/header.php';
?>

<section class="section" style="padding-top: 2.5rem;">
<div class="container">

<div style="margin-bottom: 2rem;">
    <h1 style="font-size: 1.8rem; margin-bottom: 0.5rem;">정책 가이드</h1>
    <p style="color: var(--text-secondary);">정부 지원정책의 신청 방법, 자격 조건, 주의사항을 쉽게 정리했습니다.</p>
</div>

<?php if (!empty($categories)): ?>
<div style="display: flex; gap: 0.5rem; flex-wrap: wrap; margin-bottom: 2rem;">
    <a href="/guides" class="btn <?= !$category ? '' : 'btn-outline' ?>" style="font-size: 0.85rem; padding: 0.4rem 0.8rem;">전체</a>
    <?php foreach ($categories as $cat): ?>
    <a href="/guides?category=<?= urlencode($cat['category']) ?>"
       class="btn <?= $category === $cat['category'] ? '' : 'btn-outline' ?>"
       style="font-size: 0.85rem; padding: 0.4rem 0.8rem;">
        <?= e($cat['category']) ?> (<?= $cat['cnt'] ?>)
    </a>
    <?php endforeach; ?>
</div>
<?php endif; ?>

<?php if (empty($guides)): ?>
<div class="empty-state" style="padding: 3rem; text-align: center;">
    <p style="color: var(--text-muted);">아직 작성된 가이드가 없습니다.</p>
</div>
<?php else: ?>
<div style="display: grid; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); gap: 1.2rem;">
    <?php foreach ($guides as $g): ?>
    <a href="/guide/<?= e($g['slug']) ?>" class="info-card" style="text-decoration: none; color: inherit; display: block; transition: box-shadow 0.2s; cursor: pointer;">
        <span style="display: inline-block; background: var(--primary-50); color: var(--primary-600); padding: 0.15rem 0.5rem; border-radius: 3px; font-size: 0.75rem; margin-bottom: 0.6rem;"><?= e($g['category']) ?></span>
        <h2 style="font-size: 1.1rem; line-height: 1.4; margin-bottom: 0.5rem; color: var(--text-primary);"><?= e($g['title']) ?></h2>
        <p style="font-size: 0.9rem; color: var(--text-secondary); line-height: 1.5; margin-bottom: 0.8rem;
                  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;">
            <?= e($g['description']) ?>
        </p>
        <div style="font-size: 0.8rem; color: var(--text-muted);">
            <?= e($g['author']) ?> &middot; <?= date('Y.n.j', strtotime($g['published_at'])) ?>
            &middot; 조회 <?= number_format($g['view_count']) ?>
        </div>
    </a>
    <?php endforeach; ?>
</div>
<?php endif; ?>

</div>
</section>

<?php require_once BASE_DIR . '/templates/footer.php'; ?>
