diff --git a/admin/coupons.php b/admin/coupons.php new file mode 100644 index 00000000..fca4e339 --- /dev/null +++ b/admin/coupons.php @@ -0,0 +1,134 @@ + 0 && $discount <= 100) { + try { + $stmt = $pdo->prepare("INSERT INTO coupons (code, discount_percentage) VALUES (:code, :discount)"); + $stmt->execute(['code' => $code, 'discount' => $discount]); + $feedback = ['type' => 'success', 'message' => 'Coupon created successfully.']; + } catch (PDOException $e) { + if ($e->getCode() == '23505') { // Unique violation + $feedback = ['type' => 'danger', 'message' => 'Error: Coupon code already exists.']; + } else { + $feedback = ['type' => 'danger', 'message' => 'Error creating coupon.']; + } + } + } else { + $feedback = ['type' => 'danger', 'message' => 'Invalid input. Please provide a valid code and a discount percentage between 0 and 100.']; + } + } + + // Toggle coupon status + if (isset($_POST['toggle_status'])) { + $coupon_id = $_POST['coupon_id']; + $current_status = $_POST['current_status']; + $new_status = $current_status ? 'false' : 'true'; + + $stmt = $pdo->prepare("UPDATE coupons SET is_active = :status WHERE id = :id"); + $stmt->execute(['status' => $new_status, 'id' => $coupon_id]); + $feedback = ['type' => 'success', 'message' => 'Coupon status updated.']; + } +} + +// Fetch all coupons +$coupons = $pdo->query("SELECT * FROM coupons ORDER BY created_at DESC")->fetchAll(); + +?> + +
+

Coupon Management

+ + + + + + +
+
Create New Coupon
+
+
+
+
+
+ + +
+
+
+
+ + +
+
+
+ +
+
+
+
+
+ + +
+
Existing Coupons
+
+ + + + + + + + + + + + + + + + + + + + + +
CodeDiscountStatusCreated AtAction
% + + Active + + Inactive + + +
+ + + +
+
+
+
+
+ + diff --git a/admin/header.php b/admin/header.php index 6679a651..45084b4b 100644 --- a/admin/header.php +++ b/admin/header.php @@ -17,7 +17,21 @@ if (!isset($_SESSION['admin_logged_in']) || $_SESSION['admin_logged_in'] !== tru