<?php
include 'includes/header.php';
if (isset($_GET['id'])) {
require_permission('offers_edit');
} else {
require_permission('offers_add');
}
include '../includes/db.php';
// Initialize variables
$offer = [
'id' => '',
'title' => '',
'description' => '',
'car_id' => null,
'discount_price' => '',
'image' => ''
];
$page_title = "إضافة عرض جديد";
// Fetch cars for the dropdown
$cars_result = $conn->query("SELECT id, name FROM cars ORDER BY name");
// Check if this is an edit request
if (isset($_GET['id'])) {
$offer_id = $_GET['id'];
$stmt = $conn->prepare("SELECT * FROM offers WHERE id = ?");
$stmt->bind_param("i", $offer_id);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows === 1) {
$offer = $result->fetch_assoc();
$page_title = "تعديل العرض";
} else {
header("Location: offers.php?error=Offer not found");
exit();
}
$stmt->close();
}
?>
<h3 class="fs-4 mb-3"><?= $page_title ?></h3>
<div class="row">
<div class="col">
<div class="glass-card p-4">
<form action="offer_process.php" method="POST" enctype="multipart/form-data">
<input type="hidden" name="id" value="<?= htmlspecialchars($offer['id']) ?>">
<div class="mb-3">
<label for="title" class="form-label">عنوان العرض</label>
<input type="text" class="form-control" id="title" name="title" value="<?= htmlspecialchars($offer['title']) ?>" required>
</div>
<div class="mb-3">
<label for="description" class="form-label">وصف العرض</label>
<textarea class="form-control" id="description" name="description" rows="3"><?= htmlspecialchars($offer['description']) ?></textarea>
</div>
<div class="mb-3">
<label for="type" class="form-label">نوع العرض</label>
<select class="form-select" id="type" name="type">
<option value="individual" <?= ($offer['type'] ?? 'individual') == 'individual' ? 'selected' : '' ?>>فردي</option>
<option value="corporate" <?= ($offer['type'] ?? '') == 'corporate' ? 'selected' : '' ?>>شركات</option>
</select>
</div>
<div class="row">
<div class="col-md-6 mb-3">
<label for="car_id" class="form-label">ربط العرض بسيارة (اختياري)</label>
<select class="form-select" id="car_id" name="car_id">
<option value="">-- اختر سيارة --</option>
<?php while($car = $cars_result->fetch_assoc()): ?>
<option value="<?= $car['id'] ?>" <?= ($offer['car_id'] == $car['id']) ? 'selected' : '' ?>><?= htmlspecialchars($car['name']) ?></option>
<?php endwhile; ?>
</select>
</div>
<div class="col-md-6 mb-3">
<label for="discount_price" class="form-label">سعر العرض (اختياري)</label>
<input type="number" step="0.01" class="form-control" id="discount_price" name="discount_price" value="<?= htmlspecialchars($offer['discount_price']) ?>">
</div>
</div>
<div class="mb-3">
<label for="image" class="form-label">صورة العرض</label>
<input class="form-control" type="file" id="image" name="image" accept="image/*" <?= empty($offer['id']) ? 'required' : '' ?>>
<?php if (!empty($offer['image'])): ?>
<div class="mt-2">
<small>الصورة الحالية:</small>
<img src="../uploads/offers/<?= htmlspecialchars($offer['image']) ?>" width="150" class="ms-2 rounded">
<input type="hidden" name="current_image" value="<?= htmlspecialchars($offer['image']) ?>">
</div>
<?php endif; ?>
</div>
<button type="submit" class="btn btn-success mt-3">حفظ</button>
<a href="offers.php" class="btn btn-secondary mt-3">إلغاء</a>
</form>
</div>
</div>
</div>
<?php include 'includes/footer.php'; ?>