<?php
// ========================================
// صفحه جستجو
// پروژه فروشگاه کیف‌لند
// ========================================

require_once 'config/database.php';
require_once 'config/settings.php';
require_once 'includes/auth.php';
require_once 'includes/functions.php';

$pageTitle = 'جستجو | کیف‌لند';
$db = Database::getInstance()->getConnection();
$searchQuery = $_GET['q'] ?? '';
$products = [];
$searchError = '';

if (!empty($searchQuery)) {
    if (!preg_match('/^[\p{L}\p{N}\s\-_]{1,50}$/u', $searchQuery)) {
        $searchError = 'عبارت جستجو نامعتبر است';
    } else {
        $stmt = $db->prepare("SELECT p.*, c.name as category_name, c.slug as category_slug 
                              FROM products p 
                              LEFT JOIN categories c ON p.category_id = c.id 
                              WHERE p.is_active = 1 AND (p.name LIKE ? OR p.description LIKE ? OR p.sku LIKE ?) 
                              ORDER BY p.created_at DESC");
        $stmt->execute(["%$searchQuery%", "%$searchQuery%", "%$searchQuery%"]);
        $products = $stmt->fetchAll();
    }
}

include 'includes/header.php';
?>

<section class="search-page">
    <h1>جستجو</h1>
    <form class="search-form-large" method="GET" action="search.php">
        <div class="search-input-large">
            <input type="text" name="q" value="<?= htmlspecialchars($searchQuery) ?>" placeholder="جستجو در محصولات..." autofocus>
            <button type="submit"><i class="fas fa-search"></i></button>
        </div>
    </form>
    
    <?php if($searchError): ?>
    <div class="alert alert-danger" style="max-width: 600px; margin: 20px auto;"><?= $searchError ?></div>
    <?php elseif(count($products) === 0 && !empty($searchQuery)): ?>
    <div class="no-results">
        <i class="fas fa-search"></i>
        <h2>محصولی یافت نشد</h2>
        <p>متاسفانه محصولی مطابق با جستجوی شما پیدا نشد</p>
        <div class="search-suggestions">
            <h3>پیشنهادات:</h3>
            <ul>
                <li>از کلمات کلیدی کوتاه‌تر استفاده کنید</li>
                <li>غلط املایی را بررسی کنید</li>
                <li>از کلمات عمومی‌تر استفاده کنید</li>
            </ul>
        </div>
        <a href="products.php" class="btn btn-primary">مشاهده همه محصولات</a>
    </div>
    <?php else: ?>
    <?php if(!empty($searchQuery)): ?>
    <p class="results-count"><?= count($products) ?> محصول برای "<?= htmlspecialchars($searchQuery) ?>" یافت شد</p>
    <?php endif; ?>
    <div class="product-grid">
        <?php foreach($products as $product): ?>
        <div class="product-card">
            <?php if($product['discount_price']): ?><span class="badge badge-danger">تخفیف</span><?php endif; ?>
            <a href="product-detail.php?id=<?= $product['id'] ?>">
                <img src="uploads/products/<?= $product['image'] ?>" alt="<?= htmlspecialchars($product['name']) ?>" onerror="this.src='https://via.placeholder.com/300'">
            </a>
            <div class="product-info">
                <span class="product-category"><?= htmlspecialchars($product['category_name']) ?></span>
                <h3 class="product-title"><a href="product-detail.php?id=<?= $product['id'] ?>"><?= htmlspecialchars($product['name']) ?></a></h3>
                <div class="product-price">
                    <span class="price"><?= formatPrice($product['discount_price'] ?? $product['price']) ?></span>
                    <button class="btn-add-cart" onclick="addToCart(<?= $product['id'] ?>)"><i class="fas fa-plus"></i></button>
                </div>
            </div>
        </div>
        <?php endforeach; ?>
    </div>
    <?php endif; ?>
</section>

<style>
.search-page { padding: 40px 5%; min-height: 80vh; }
.search-page h1 { text-align: center; margin-bottom: 30px; font-size: 2rem; }
.search-form-large { max-width: 600px; margin: 0 auto 40px; }
.search-input-large { display: flex; border: 2px solid var(--border-color); border-radius: 50px; overflow: hidden; }
.search-input-large input { flex: 1; padding: 15px 25px; border: none; background: var(--bg-card); color: var(--text-primary); font-size: 1rem; }
.search-input-large input:focus { outline: none; }
.search-input-large button { padding: 15px 30px; background: var(--primary); border: none; color: #fff; cursor: pointer; font-size: 1.2rem; transition: 0.3s; }
.search-input-large button:hover { background: var(--primary-hover); }
.results-count { text-align: center; color: var(--text-secondary); margin-bottom: 30px; }
.no-results { text-align: center; padding: 80px 20px; max-width: 600px; margin: 0 auto; }
.no-results i { font-size: 5rem; color: var(--text-secondary); margin-bottom: 20px; }
.no-results h2 { font-size: 1.8rem; margin-bottom: 15px; }
.no-results p { color: var(--text-secondary); margin-bottom: 30px; }
.search-suggestions { text-align: right; background: var(--bg-card); padding: 20px; border-radius: var(--radius-lg); margin-bottom: 30px; }
.search-suggestions h3 { margin-bottom: 15px; }
.search-suggestions ul { list-style: disc; padding-right: 20px; color: var(--text-secondary); }
.search-suggestions li { margin-bottom: 8px; }
</style>

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