<?php
require_once 'config/db.php';
$settings = getAllSettings($pdo);
$activePage = 'catalog';
$pageTitle = 'Catalog - '.($settings['site_name']??'');

$filter = $_GET['cat'] ?? 'all';
if ($filter !== 'all') {
    $stmt = $pdo->prepare("SELECT * FROM catalogs WHERE is_active=1 AND category=? ORDER BY sort_order ASC");
    $stmt->execute([$filter]);
    $catalogs = $stmt->fetchAll();
} else {
    $catalogs = $pdo->query("SELECT * FROM catalogs WHERE is_active=1 ORDER BY sort_order ASC")->fetchAll();
}

$cats = $pdo->query("SELECT DISTINCT category FROM catalogs WHERE is_active=1 ORDER BY category")->fetchAll(PDO::FETCH_COLUMN);

include 'partials/header.php';
?>

<div class="page-hero">
    <div class="container">
        <nav aria-label="breadcrumb"><ol class="breadcrumb mb-2"><li class="breadcrumb-item"><a href="index.php">Home</a></li><li class="breadcrumb-item active" data-en="Catalog" data-id="Katalog">Catalog</li></ol></nav>
        <h1 class="fw-bold mb-1 fs-2" data-en="Catalog" data-id="Katalog">Catalog</h1>
        <p class="mb-0" style="color:rgba(255,255,255,.6);" data-en="Download our product catalogs" data-id="Unduh katalog produk kami">Unduh katalog produk kami</p>
    </div>
</div>

<section class="py-5 py-lg-6">
    <div class="container">
        <div class="filter-tabs fade-up">
            <a href="catalog.php" class="filter-tab <?= $filter==='all'?'active':'' ?>" data-en="All" data-id="Semua">Semua</a>
            <?php foreach ($cats as $c): ?>
                <a href="catalog.php?cat=<?= urlencode($c) ?>" class="filter-tab <?= $filter===$c?'active':'' ?>"><?= htmlspecialchars($c) ?></a>
            <?php endforeach; ?>
        </div>
        <div class="row g-4">
            <?php foreach ($catalogs as $cat): ?>
            <div class="col-sm-6 col-md-4 col-lg-3 fade-up">
                <div class="catalog-card">
                    <img src="<?= htmlspecialchars($cat['cover_image']??'') ?>" class="catalog-cover" alt="<?= htmlspecialchars($cat['title']) ?>"
                         onerror="this.src='https://placehold.co/300x200/e0e0e0/888?text=Catalog'">
                    <div class="catalog-body">
                        <div class="catalog-cat"><?= htmlspecialchars($cat['category']) ?></div>
                        <h6 class="catalog-title"><?= htmlspecialchars($cat['title']) ?></h6>
                        <p class="small text-muted"><?= htmlspecialchars(substr($cat['description']??'',0,80)) ?></p>
                        <?php if ($cat['pdf_file']): ?>
                            <a href="<?= htmlspecialchars($cat['pdf_file']) ?>" target="_blank" class="btn-download">
                                <i class="bi bi-download"></i>
                                <span data-en="Download PDF" data-id="Unduh PDF">Unduh PDF</span>
                            </a>
                        <?php else: ?>
                            <a href="contact.php" class="btn-download" style="background:#888;">
                                <i class="bi bi-envelope"></i>
                                <span data-en="Request" data-id="Minta">Minta</span>
                            </a>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
            <?php endforeach; ?>
            <?php if (empty($catalogs)): ?>
                <div class="col-12 text-center py-5 text-muted"><i class="bi bi-file-pdf display-4 mb-3 d-block"></i>Belum ada katalog tersedia.</div>
            <?php endif; ?>
        </div>
    </div>
</section>

<!-- Request catalog -->
<section class="py-5 bg-section">
    <div class="container text-center">
        <h3 class="fw-bold mb-3" data-en="Don't Find What You're Looking For?" data-id="Tidak Menemukan yang Anda Cari?">Tidak Menemukan yang Anda Cari?</h3>
        <p class="text-muted mb-4" data-en="Contact us to request specific product catalogs or custom quotations" data-id="Hubungi kami untuk meminta katalog produk spesifik atau penawaran khusus">Hubungi kami untuk meminta katalog produk spesifik atau penawaran khusus</p>
        <a href="contact.php" class="btn btn-dark px-4" data-en="Contact Us" data-id="Hubungi Kami">Hubungi Kami</a>
    </div>
</section>

<?php include 'partials/footer.php'; ?>
