<?php
require_once __DIR__ . '/../includes/header_customer.php';
$pdo = require __DIR__ . '/../config/pdo.php';

$categoryStmt = $pdo->prepare('SELECT id, name FROM categories WHERE is_active = 1 ORDER BY name LIMIT 6');
$categoryStmt->execute();
$categories = $categoryStmt->fetchAll();

$productStmt = $pdo->prepare('SELECT id, name, price, photo FROM products WHERE is_active = 1 ORDER BY created_at DESC LIMIT 8');
$productStmt->execute();
$products = $productStmt->fetchAll();
?>

<?php
// Fetch Active Sliders
$sliderStmt = $pdo->prepare('SELECT * FROM sliders WHERE is_active = 1 ORDER BY sort_order ASC, created_at DESC');
$sliderStmt->execute();
$sliders = $sliderStmt->fetchAll();
?>

<?php if (!empty($sliders)): ?>
<section class="mb-5 mt-2">
    <div id="homeCarousel" class="carousel slide rounded-4 overflow-hidden shadow-sm" data-bs-ride="carousel">
        <div class="carousel-indicators">
            <?php foreach ($sliders as $index => $slider): ?>
                <button type="button" data-bs-target="#homeCarousel" data-bs-slide-to="<?= $index; ?>" class="<?= $index === 0 ? 'active' : ''; ?>" aria-current="<?= $index === 0 ? 'true' : 'false'; ?>" aria-label="Slide <?= $index + 1; ?>"></button>
            <?php endforeach; ?>
        </div>
        <div class="carousel-inner">
            <?php foreach ($sliders as $index => $slider): ?>
                <div class="carousel-item <?= $index === 0 ? 'active' : ''; ?>">
                    <img src="<?= base_url('uploads/' . sanitize($slider['image'])); ?>" class="d-block w-100" alt="<?= sanitize($slider['title']); ?>" style="height: 400px; object-fit: cover;">
                    <?php if ($slider['title'] || $slider['description']): ?>
                        <div class="carousel-caption d-none d-md-block text-start px-4" style="background: rgba(0, 0, 0, 0.4); backdrop-filter: blur(2px); border-radius: 15px; bottom: 20%; left: 10%; max-width: 600px;">
                            <?php if ($slider['title']): ?>
                                <h2 class="fw-bold text-white mb-2"><?= sanitize($slider['title']); ?></h2>
                            <?php endif; ?>
                            <?php if ($slider['description']): ?>
                                <p class="text-white mb-3 lead"><?= sanitize($slider['description']); ?></p>
                            <?php endif; ?>
                            <?php if ($slider['link_url']): ?>
                                <a href="<?= base_url($slider['link_url']); ?>" class="btn btn-light fw-bold">Lihat Detail</a>
                            <?php endif; ?>
                        </div>
                    <?php endif; ?>
                </div>
            <?php endforeach; ?>
        </div>
        <button class="carousel-control-prev" type="button" data-bs-target="#homeCarousel" data-bs-slide="prev">
            <span class="carousel-control-prev-icon" aria-hidden="true" style="background-color: rgba(0,0,0,0.2); border-radius: 50%; padding: 10px;"></span>
            <span class="visually-hidden">Previous</span>
        </button>
        <button class="carousel-control-next" type="button" data-bs-target="#homeCarousel" data-bs-slide="next">
            <span class="carousel-control-next-icon" aria-hidden="true" style="background-color: rgba(0,0,0,0.2); border-radius: 50%; padding: 10px;"></span>
            <span class="visually-hidden">Next</span>
        </button>
    </div>
</section>
<?php endif; ?>

<section class="mb-4">
    <div class="section-title mb-2">
        <span></span>
        <h3 class="fw-bold mb-0">Kategori Populer</h3>
    </div>
    <div>
        <?php foreach ($categories as $category): ?>
            <a class="category-chip" href="<?= base_url('public/index.php?page=products&category=' . (int) $category['id']); ?>">
                <?= sanitize($category['name']); ?>
            </a>
        <?php endforeach; ?>
    </div>
</section>

<section class="mb-5">
    <div class="d-flex align-items-center justify-content-between">
        <div class="section-title">
            <span></span>
            <h3 class="fw-bold mb-0">Produk Terbaru</h3>
        </div>
        <a class="btn btn-sm" href="<?= base_url('public/index.php?page=products'); ?>">Lihat Semua</a>
    </div>
    <div class="row g-4 mt-2">
        <?php foreach ($products as $product): ?>
            <div class="col-sm-6 col-lg-4">
                <a class="text-decoration-none text-dark" href="<?= base_url('public/index.php?page=product_detail&id=' . (int) $product['id']); ?>">
                    <div class="product-card">
                        <div class="product-image-wrapper">
                            <?php if (!empty($product['photo'])): ?>
                                <img src="<?= base_url('uploads/' . sanitize($product['photo'])); ?>" alt="<?= sanitize($product['name']); ?>">
                            <?php else: ?>
                                <div class="product-thumb">Al-Qanuni</div>
                            <?php endif; ?>
                            <div class="card-actions">
                                <span class="action-btn" title="Lihat Detail"><i class="bi bi-eye"></i></span>
                                <span class="action-btn" title="Beli Sekarang"><i class="bi bi-bag-plus"></i></span>
                            </div>
                        </div>
                        <div class="p-3">
                            <h6 class="fw-bold mb-1"><?= sanitize($product['name']); ?></h6>
                            <div class="price-tag mb-2"><?= format_currency($product['price']); ?></div>
                            <span class="btn btn-sm w-100 btn-outline-primary">Detail Produk</span>
                        </div>
                    </div>
                </a>
            </div>
        <?php endforeach; ?>
    </div>
</section>

<?php require_once __DIR__ . '/../includes/footer_customer.php'; ?>
