<?php
$q = mb_substr(get('q', ''), 0, 100);
$like = '%' . $q . '%';

// Arama panelinde yazarken gelen canlı sonuçlar
if (is_ajax() && get('canli')) {
    $products = mb_strlen($q) >= 2
        ? db_all('SELECT * FROM products WHERE status = 1 AND name LIKE ? ORDER BY sold DESC, id DESC LIMIT 6', [$like])
        : [];
    $categories = mb_strlen($q) >= 2
        ? db_all('SELECT * FROM categories WHERE status = 1 AND name LIKE ? ORDER BY sort LIMIT 4', [$like])
        : [];
    reply(true, '', ['html' => render('search-results', ['products' => $products, 'categories' => $categories, 'q' => $q])]);
}

listing([
    'title'       => $q !== '' ? '"' . $q . '" araması' : 'Arama',
    'crumbs'      => ['Arama' => null],
    'heading'     => $q !== '' ? '"' . $q . '"' : 'Ürün ara',
    'description' => $q !== '' ? 'Aramanızla eşleşen ürünler.' : 'Aramak istediğiniz ürünü üstteki arama simgesinden yazın.',
    'keep'        => ['q' => $q],
    'where'       => $q !== '' ? 'status = 1 AND (name LIKE ? OR description LIKE ?)' : '0 = 1',
    'params'      => $q !== '' ? [$like, $like] : [],
    'empty'       => $q !== '' ? 'Aramanızla eşleşen ürün bulunamadı.' : 'Henüz bir arama yapmadınız.',
]);
