<?php
include 'includes/header.php';

$q = $_GET['q'] ?? '';
$results = [];
if (!empty($q)) {
    $stmt = db()->prepare("SELECT p.*, c.name as category_name FROM products p JOIN categories c ON p.category_id = c.id WHERE p.name LIKE ? OR p.description LIKE ? OR c.name LIKE ? ORDER BY p.id DESC");
    $stmt->execute(['%'.$q.'%', '%'.$q.'%', '%'.$q.'%']);
    $results = $stmt->fetchAll();
}
?>

<div class="row mb-5 align-items-center">
    <div class="col-md-6">
        <h1 class="fw-bold text-dark mb-0">搜索结果</h1>
        <p class="text-muted mt-2">关键词: <span class="text-primary fw-bold">"<?php echo htmlspecialchars($q); ?>"</span></p>
    </div>
    <div class="col-md-6 text-md-end mt-3 mt-md-0">
        <span class="badge bg-primary bg-opacity-25 text-primary rounded-pill px-4 py-2 border border-primary border-opacity-25"><?php echo count($results); ?> 个相关结果</span>
    </div>
</div>

<?php if (empty($results)): ?>
    <div class="text-center py-5">
        <div class="glass-card p-5 bg-white shadow-sm">
            <i class="bi bi-search fs-1 text-muted d-block mb-3"></i>
            <h4 class="text-dark">未找到相关结果</h4>
            <p class="text-muted">换个关键词试试，或者联系客服定制您需要的软件。</p>
            <a href="index.php" class="btn btn-primary mt-3 px-5 rounded-pill">返回商城首页</a>
        </div>
    </div>
<?php else: ?>
    <div class="row g-2 g-lg-4">
        <?php foreach ($results as $product): ?>
            <div class="col-6 col-lg-3">
                <?php include 'includes/product_card.php'; ?>
            </div>
        <?php endforeach; ?>
    </div>
<?php endif; ?>

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