<?php
include 'auth.php';
require_permission('offers_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="offer_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>
</tr>
</thead>
<tbody>
<?php
$sql = "SELECT o.id, o.title, o.image, c.name as car_name
FROM offers o
LEFT JOIN cars c ON o.car_id = c.id
ORDER BY o.created_at DESC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$image_path = !empty($row["image"]) ? '../uploads/offers/' . htmlspecialchars($row["image"]) : 'https://via.placeholder.com/150x80';
echo "<tr>";
echo "<th scope='row'>" . $row["id"] . "</th>";
echo "<td><img src='" . $image_path . "' width='150' class='rounded'></td>";
echo "<td>" . htmlspecialchars($row["title"]) . "</td>";
echo "<td>" . htmlspecialchars($row["car_name"] ?? 'لا يوجد') . "</td>";
echo "<td>
<a href='offer_form.php?id=" . $row["id"] . "' class='btn btn-primary btn-sm'><i class='fas fa-edit'></i></a>
<a href='offer_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='5' class='text-center'>لا توجد عروض مضافة حالياً.</td></tr>";
}
?>
</tbody>
</table>
</div>
</div>
</div>
<?php include 'includes/footer.php'; ?>