<?php
include 'auth.php';
require_permission('cars_view');
include 'includes/header.php';
?>
<div class="row mb-4">
<div class="col">
<h3 class="fs-4 mb-3">إدارة السيارات</h3>
</div>
<div class="col text-start">
<a href="car_form.php" class="btn btn-success"><i class="fas fa-plus"></i> إضافة سيارة جديدة</a>
</div>
</div>
<div class="row">
<div class="col">
<div class="glass-card p-3">
<table class="table table-dark table-hover align-middle">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">صورة</th>
<th scope="col">الاسم</th>
<th scope="col">الماركة</th>
<th scope="col">نوع الوقود</th>
<th scope="col">حالة السيارة</th>
<th scope="col">الصور</th>
<th scope="col">فيديو</th>
<th scope="col">السعر</th>
<th scope="col">الإجراءات</th>
</tr>
</thead>
<tbody>
<?php
$sql = "SELECT c.id, c.name, c.brand, c.image, c.video_url,
MIN(t.price) as min_price,
MAX(t.price) as max_price,
GROUP_CONCAT(DISTINCT t.fuel_type SEPARATOR ' / ') as fuel_type,
GROUP_CONCAT(DISTINCT t.car_condition SEPARATOR ' / ') as car_condition,
COUNT(t.id) as trims_count
FROM cars c
LEFT JOIN car_trims t ON c.id = t.car_id
GROUP BY c.id, c.name, c.brand, c.image, c.video_url
ORDER BY c.created_at DESC";
$result = $conn->query($sql);
if ($result && $result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
// Get primary image from car_images table or fallback to old image field
$primary_image_query = $conn->prepare("SELECT image_name FROM car_images WHERE car_id = ? AND is_primary = 1 LIMIT 1");
$primary_image_query->bind_param("i", $row["id"]);
$primary_image_query->execute();
$primary_result = $primary_image_query->get_result();
if ($primary_result->num_rows > 0) {
$primary_image = $primary_result->fetch_assoc()['image_name'];
$image_path = '../uploads/cars/' . htmlspecialchars($primary_image);
} else {
$image_path = !empty($row["image"]) ? '../uploads/cars/' . htmlspecialchars($row["image"]) : 'https://via.placeholder.com/100';
}
$primary_image_query->close();
// Count total images for this car
$images_count_query = $conn->prepare("SELECT COUNT(*) as count FROM car_images WHERE car_id = ?");
$images_count_query->bind_param("i", $row["id"]);
$images_count_query->execute();
$images_count_result = $images_count_query->get_result();
$images_count = $images_count_result->fetch_assoc()['count'];
$images_count_query->close();
echo "<tr>";
echo "<th scope='row'>" . $row["id"] . "</th>";
echo "<td><img src='" . $image_path . "' width='100' class='rounded'></td>";
echo "<td>" . htmlspecialchars($row["name"]) . " <span class='badge bg-secondary'>" . $row["trims_count"] . " فئات</span></td>";
echo "<td>" . htmlspecialchars($row["brand"]) . "</td>";
echo "<td>" . (!empty($row["fuel_type"]) ? htmlspecialchars($row["fuel_type"]) : '<span class="text-muted">غير محدد</span>') . "</td>";
echo "<td>" . (!empty($row["car_condition"]) ? htmlspecialchars($row["car_condition"]) : '<span class="text-muted">غير محدد</span>') . "</td>";
echo "<td>";
if ($images_count > 0) {
echo "<span class='badge bg-info'><i class='fas fa-images'></i> " . $images_count . "</span>";
} else {
echo "<span class='text-muted'>لا توجد</span>";
}
echo "</td>";
echo "<td>";
if (!empty($row["video_url"])) {
echo "<a href='" . htmlspecialchars($row["video_url"]) . "' target='_blank' class='btn btn-sm btn-outline-light'><i class='fas fa-play'></i></a>";
} else {
echo "<span class='text-muted'>لا يوجد</span>";
}
echo "</td>";
$price_display = '';
if ($row["min_price"] == $row["max_price"]) {
$price_display = number_format($row["min_price"] ?? 0) . " EGP";
} else {
$price_display = number_format($row["min_price"] ?? 0) . " - " . number_format($row["max_price"] ?? 0) . " EGP";
}
echo "<td>" . $price_display . "</td>";
echo "<td>
<a href='car_form.php?id=" . $row["id"] . "' class='btn btn-primary btn-sm'><i class='fas fa-edit'></i></a>
<a href='car_delete.php?id=" . $row["id"] . "' class='btn btn-danger btn-sm' onclick='return confirm(\"هل أنت متأكد من رغبتك في حذف هذه السيارة؟\");'><i class='fas fa-trash'></i></a>
</td>";
echo "</tr>";
}
} else {
echo "<tr><td colspan='10' class='text-center'>لا توجد سيارات مضافة حالياً.</td></tr>";
}
?>
</tbody>
</table>
</div>
</div>
</div>
<?php include 'includes/footer.php'; ?>