<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

$conn = mysqli_connect('premium129', 'mosuclaz', 'CgQqC9g4msfD', 'mosuclaz_onlinefoodorder');
mysqli_set_charset($conn, "utf8mb4");

$cat_id = isset($_GET['cat_id']) ? $_GET['cat_id'] : 'all';

if ($cat_id === "all") {
    $sql = "SELECT * FROM tbl_food";
} elseif (is_numeric($cat_id) && intval($cat_id) > 0) {
    $cat_id = intval($cat_id);
    $sql = "SELECT * FROM tbl_food WHERE category_id='$cat_id'";
} else {
    echo "<div>فئة غير صالحة</div>";
    exit;
}

$res = mysqli_query($conn, $sql);

if (!$res) {
    echo "<div>خطأ في الاستعلام: " . mysqli_error($conn) . "</div>";
    exit;
}

if (mysqli_num_rows($res) == 0) {
    echo "<div style='grid-column:1/-1;text-align:center;font-size:19px;color:#999;padding:45px 0'>لا توجد أطعمة متوفرة حالياً.</div>";
} else {
    while($row = mysqli_fetch_assoc($res)) {
        $img = (!empty($row['image_name'])) ? 'images/food/'.$row['image_name'] : 'images/default-food.jpg';
        echo '<div class="food-card">';
        echo '<img class="food-img" src="'.$img.'" alt="">';
        echo '<div class="food-title">'.$row['title'].'</div>';
        echo '<div class="food-desc">'.$row['description'].'</div>';
        echo '<div class="food-price">'.number_format($row['price']).' د.ع</div>';
        echo '</div>';
    }
}
