<?php

declare(strict_types=1);
require_once __DIR__ . '/../app/controllers/ShopController.php';
require_once __DIR__ . '/../app/config/bootstrap.php';
require_once __DIR__ . '/../app/core/DijikalaLayout.php';

$q = trim((string)($_GET['q'] ?? ''));
$category = trim((string)($_GET['category'] ?? ''));
$products = (new ShopController())->catalogue($q !== '' ? $q : null, $category !== '' ? $category : null);

$titleTail = $category !== '' ? $category : ($q !== '' ? 'نتایج جستجو' : 'محصولات');
$pageTitle = APP_NAME . ' | ' . $titleTail;

renderDijikalaShellStart($pageTitle, [], $q);
?>

<article class="container" style="padding:1.25rem 0 2rem;">
    <h2 style="margin-bottom:1rem;"><?= htmlspecialchars($titleTail) ?></h2>
    <?php if ($q !== ''): ?>
        <p style="color:#64748b;margin-bottom:0.5rem;">عبارت جستجو: <strong><?= htmlspecialchars($q) ?></strong></p>
    <?php endif; ?>
    <?php if ($category !== ''): ?>
        <p style="color:#64748b;margin-bottom:0.5rem;">دسته: <strong><?= htmlspecialchars($category) ?></strong></p>
    <?php endif; ?>

    <div style="display:flex;flex-wrap:wrap;gap:14px;margin-top:1rem;">
        <?php foreach ($products as $p): ?>
            <?php
                $href = dijikalaProductUrl($p);
                $d = max(0, min(99, (int)($p['discount_percent'] ?? 0)));
                $price = (float)$p['price'];
                $listApprox = ($d > 0) ? (int)round($price / (1 - $d / 100)) : (int)$price;
            ?>
            <div class="slides" style="width:calc(33.333% - 14px);min-width:210px;">
                <div class="box-slides">
                    <a href="<?= $href ?>">
                        <div class="box-slides-img">
                            <img src="<?= dijikalaProductThumbUrl($p) ?>" alt="<?= htmlspecialchars((string)$p['title']) ?>" loading="lazy">
                        </div>
                        <div class="c-product-box__title"><?= htmlspecialchars((string)$p['title']) ?></div>
                        <div class="c-product-box__row c-product-box__row--price">
                            <div class="price">
                                <div class="c-price__value c-price__value--plp">
                                    <?php if ($d > 0): ?>
                                        <del><?= number_format((float)$listApprox) ?></del>
                                        <div class="c-price__discount-oval"><span><?= (int)$d ?>٪</span></div>
                                    <?php endif; ?>
                                    <div class="c-price__value-wrapper"><?= number_format($price) ?>
                                        <span class="c-price__currency">تومان</span>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </a>
                    <div style="padding:8px 0;">
                        <a class="btn-login" style="display:inline-block;text-align:center;width:100%;text-decoration:none;"
                           href="<?= htmlspecialchars(appPath('/public/cart.php')) ?>?add=<?= (int)$p['id'] ?>">افزودن به سبد</a>
                    </div>
                </div>
            </div>
        <?php endforeach; ?>
    </div>
    <?php if (!$products): ?>
        <p>محصولی یافت نشد.</p>
    <?php endif; ?>
</article>

<?php
renderDijikalaShellEnd('products', 'minimal');
Performance::finish('public.products');
