34766-vm/listings.php
2025-10-08 05:46:19 +00:00

120 lines
5.3 KiB
PHP

<?php
session_start();
require_once 'db/config.php';
// Redirect to login if not logged in or not a restaurant
if (!isset($_SESSION['user_id']) || $_SESSION['user_role'] !== 'restaurant') {
header("Location: login.php");
exit;
}
$message = '';
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['add_listing'])) {
$user_id = $_SESSION['user_id'];
$title = $_POST['title'];
$description = $_POST['description'];
$quantity = $_POST['quantity'];
$food_type = $_POST['food_type'];
$prepared_time = $_POST['prepared_time'];
$pickup_deadline = $_POST['pickup_deadline'];
// Photo upload will be handled later
if (empty($title) || empty($quantity) || empty($prepared_time) || empty($pickup_deadline)) {
$message = '<div class="alert alert-danger">Please fill in all required fields.</div>';
} else {
try {
$pdo = db();
$stmt = $pdo->prepare("INSERT INTO food_listings (user_id, title, description, quantity, food_type, prepared_time, pickup_deadline) VALUES (?, ?, ?, ?, ?, ?, ?)");
$stmt->execute([$user_id, $title, $description, $quantity, $food_type, $prepared_time, $pickup_deadline]);
$message = '<div class="alert alert-success">Food listing created successfully!</div>';
} catch (PDOException $e) {
$message = '<div class="alert alert-danger">Error: ' . $e->getMessage() . '</div>';
}
}
}
// Fetch existing listings for this restaurant
$listings = [];
try {
$pdo = db();
$stmt = $pdo->prepare("SELECT * FROM food_listings WHERE user_id = ? ORDER BY created_at DESC");
$stmt->execute([$_SESSION['user_id']]);
$listings = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
// Handle error
}
?>
<?php include 'partials/header.php'; ?>
<div class="container py-5">
<div class="d-flex justify-content-between align-items-center mb-4">
<h1>Restaurant Dashboard</h1>
<a href="logout.php" class="btn btn-danger">Logout</a>
</div>
<p class="lead">Welcome, <?php echo htmlspecialchars($_SESSION['user_name']); ?>!</p>
<hr>
<div class="row">
<div class="col-md-8">
<h2>Your Food Listings</h2>
<?php if (empty($listings)): ?>
<p>You haven't posted any food listings yet.</p>
<?php else: ?>
<div class="list-group">
<?php foreach ($listings as $listing): ?>
<div class="list-group-item list-group-item-action flex-column align-items-start">
<div class="d-flex w-100 justify-content-between">
<h5 class="mb-1"><?php echo htmlspecialchars($listing['title']); ?></h5>
<small>Status: <?php echo htmlspecialchars($listing['status']); ?></small>
</div>
<p class="mb-1"><?php echo htmlspecialchars($listing['description']); ?></p>
<small>Quantity: <?php echo htmlspecialchars($listing['quantity']); ?></small>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
</div>
<div class="col-md-4">
<h2>Add New Listing</h2>
<?php echo $message; ?>
<form action="listings.php" method="POST">
<div class="mb-3">
<label for="title" class="form-label">Title</label>
<input type="text" class="form-control" id="title" name="title" required>
</div>
<div class="mb-3">
<label for="description" class="form-label">Description</label>
<textarea class="form-control" id="description" name="description" rows="3"></textarea>
</div>
<div class="mb-3">
<label for="quantity" class="form-label">Quantity (e.g., "Serves 10-15 people")</label>
<input type="text" class="form-control" id="quantity" name="quantity" required>
</div>
<div class="mb-3">
<label for="food_type" class="form-label">Food Type</label>
<select class="form-select" id="food_type" name="food_type">
<option value="veg">Vegetarian</option>
<option value="non-veg">Non-Vegetarian</option>
<option value="halal">Halal</option>
<option value="other">Other</option>
</select>
</div>
<div class="mb-3">
<label for="prepared_time" class="form-label">Time of Preparation</label>
<input type="datetime-local" class="form-control" id="prepared_time" name="prepared_time" required>
</div>
<div class="mb-3">
<label for="pickup_deadline" class="form-label">Pickup Deadline</label>
<input type="datetime-local" class="form-control" id="pickup_deadline" name="pickup_deadline" required>
</div>
<button type="submit" name="add_listing" class="btn btn-primary">Add Listing</button>
</form>
</div>
</div>
</div>
<?php include 'partials/footer.php'; ?>