<?php
include 'includes/header.php';
if (isset($_GET['id'])) {
require_permission('slider_edit');
} else {
require_permission('slider_add');
}
include '../includes/db.php';
$slide = [
'id' => '',
'title' => '',
'subtitle' => '',
'image_path' => '',
'car_id' => '',
'active' => 1
];
$page_title = "إضافة شريحة جديدة";
if (isset($_GET['id'])) {
$slide_id = $_GET['id'];
$stmt = $conn->prepare("SELECT * FROM slider WHERE id = ?");
$stmt->bind_param("i", $slide_id);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows === 1) {
$slide = $result->fetch_assoc();
$page_title = "تعديل الشريحة";
} else {
header("Location: slider.php?error=Slide not found");
exit();
}
$stmt->close();
}
// جلب السيارات المتاحة للربط
$cars_result = $conn->query("SELECT id, name, brand, model FROM cars ORDER BY brand ASC, name ASC");
?>
<h3 class="fs-4 mb-3"><?= $page_title ?></h3>
<div class="row">
<div class="col">
<div class="glass-card p-4">
<form action="slider_process.php" method="POST" enctype="multipart/form-data">
<input type="hidden" name="id" value="<?= htmlspecialchars($slide['id']) ?>">
<div class="mb-3">
<label for="title" class="form-label">العنوان الرئيسي</label>
<input type="text" class="form-control" id="title" name="title" value="<?= htmlspecialchars($slide['title']) ?>">
</div>
<div class="mb-3">
<label for="subtitle" class="form-label">العنوان الفرعي</label>
<textarea class="form-control" id="subtitle" name="subtitle" rows="3"><?= htmlspecialchars($slide['subtitle']) ?></textarea>
</div>
<div class="mb-3">
<label for="car_id" class="form-label">ربط بسيارة (عند النقر على الشريحة يتم الانتقال إليها - اختياري)</label>
<select class="form-select" id="car_id" name="car_id">
<option value="">-- لا يوجد ربط (شريحة عرض فقط) --</option>
<?php if ($cars_result && $cars_result->num_rows > 0): ?>
<?php while ($car = $cars_result->fetch_assoc()): ?>
<?php
$car_label = trim(($car['brand'] ?? '') . ' ' . $car['name'] . ' ' . ($car['model'] ?? ''));
?>
<option value="<?= $car['id'] ?>" <?= ($slide['car_id'] == $car['id']) ? 'selected' : '' ?>>
<?= htmlspecialchars($car_label) ?>
</option>
<?php endwhile; ?>
<?php endif; ?>
</select>
</div>
<div class="mb-3">
<label for="image" class="form-label">صورة الشريحة (يفضل مقاس 1920x1080)</label>
<input class="form-control" type="file" id="image" name="image" accept="image/*" <?= empty($slide['id']) ? 'required' : '' ?>>
<?php if (!empty($slide['image_path'])): ?>
<div class="mt-2">
<small>الصورة الحالية:</small>
<img src="../uploads/slider/<?= htmlspecialchars($slide['image_path']) ?>" width="200" class="ms-2 rounded">
<input type="hidden" name="current_image" value="<?= htmlspecialchars($slide['image_path']) ?>">
</div>
<?php endif; ?>
</div>
<div class="form-check mb-3">
<input class="form-check-input" type="checkbox" id="active" name="active" value="1" <?= $slide['active'] ? 'checked' : '' ?>>
<label class="form-check-label" for="active">
فعال (عرض هذه الشريحة في الصفحة الرئيسية)
</label>
</div>
<button type="submit" class="btn btn-success mt-3">حفظ</button>
<a href="slider.php" class="btn btn-secondary mt-3">إلغاء</a>
</form>
</div>
</div>
</div>
<?php include 'includes/footer.php'; ?>