<?php
include "db.php";

$q = $_GET["q"] ?? "";
$q = $baglan->real_escape_string($q);

$sonuc = $baglan->query("
    SELECT * FROM items
    WHERE item_adi LIKE '%$q%'
    ORDER BY id DESC
    LIMIT 50
");

echo "<table style='width:100%; border-collapse:collapse; font-size:13px;'>";

echo "
<tr style='background:#0f172a; color:#38bdf8;'>
    <th>Durum</th>
    <th>Item</th>
    <th>Alış</th>
    <th>Satış</th>
    <th>Güncel</th>
    <th>Notlar</th>
    <th>Tarih</th>
</tr>
";

while($r = $sonuc->fetch_assoc()) {

    $renk = ($r["durum"] == "Satıldı" ? 
        "background:rgba(6,78,59,0.8); color:#bbf7d0;" : 
        "background:rgba(124,45,18,0.8); color:#fff3cd;"
    );

    // Tarihi formatla
    $tarih = date("d/m/Y H:i", strtotime($r["tarih"]));

    // Notları satır kırılabilir yap
    $not = nl2br(htmlspecialchars($r["notlar"]));

    echo "
    <tr style='$renk'>
        <td>{$r['durum']}</td>
        <td>{$r['item_adi']}</td>
        <td>{$r['alis_fiyat']}</td>
        <td>{$r['satis_fiyat']}</td>
        <td>{$r['guncel_satis']}</td>
        <td style='max-width:220px; word-break:break-word;'>$not</td>
        <td>$tarih</td>
    </tr>
    ";
}

echo "</table>";
?>
