2
This commit is contained in:
parent
7100e72a1d
commit
08c2df845a
@ -34,6 +34,9 @@
|
|||||||
.modal-content {
|
.modal-content {
|
||||||
border-radius: 0.5rem;
|
border-radius: 0.5rem;
|
||||||
}
|
}
|
||||||
|
.table th, .table td {
|
||||||
|
vertical-align: middle;
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
@ -41,7 +44,7 @@
|
|||||||
<div class="container mt-5">
|
<div class="container mt-5">
|
||||||
<div class="d-flex justify-content-between align-items-center mb-4">
|
<div class="d-flex justify-content-between align-items-center mb-4">
|
||||||
<h1>Manage Restaurants</h1>
|
<h1>Manage Restaurants</h1>
|
||||||
<button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addRestaurantModal">
|
<button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#restaurantModal" id="addNewBtn">
|
||||||
<i class="bi bi-plus-lg"></i> Add New Restaurant
|
<i class="bi bi-plus-lg"></i> Add New Restaurant
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
@ -59,46 +62,25 @@
|
|||||||
<th>Actions</th>
|
<th>Actions</th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody id="restaurantsTableBody">
|
||||||
<?php
|
<!-- Restaurants will be loaded here dynamically -->
|
||||||
require_once 'db/config.php';
|
|
||||||
try {
|
|
||||||
$pdo = db();
|
|
||||||
$stmt = $pdo->query("SELECT * FROM restaurants ORDER BY id DESC");
|
|
||||||
$restaurants = $stmt->fetchAll();
|
|
||||||
foreach ($restaurants as $restaurant) {
|
|
||||||
echo "<tr>";
|
|
||||||
echo "<td>" . htmlspecialchars($restaurant['id']) . "</td>";
|
|
||||||
echo "<td>" . htmlspecialchars($restaurant['name']) . "</td>";
|
|
||||||
echo "<td>" . htmlspecialchars($restaurant['address']) . "</td>";
|
|
||||||
echo "<td>" . htmlspecialchars($restaurant['phone']) . "</td>";
|
|
||||||
echo "<td>" . htmlspecialchars($restaurant['email']) . "</td>";
|
|
||||||
echo '<td>
|
|
||||||
<button class="btn btn-sm btn-secondary"><i class="bi bi-pencil"></i></button>
|
|
||||||
<button class="btn btn-sm btn-danger"><i class="bi bi-trash"></i></button>
|
|
||||||
</td>';
|
|
||||||
echo "</tr>";
|
|
||||||
}
|
|
||||||
} catch (PDOException $e) {
|
|
||||||
echo "<tr><td colspan='6'>Error: " . $e->getMessage() . "</td></tr>";
|
|
||||||
}
|
|
||||||
?>
|
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- Add Restaurant Modal -->
|
<!-- Add/Edit Restaurant Modal -->
|
||||||
<div class="modal fade" id="addRestaurantModal" tabindex="-1" aria-labelledby="addRestaurantModalLabel" aria-hidden="true">
|
<div class="modal fade" id="restaurantModal" tabindex="-1" aria-labelledby="restaurantModalLabel" aria-hidden="true">
|
||||||
<div class="modal-dialog">
|
<div class="modal-dialog">
|
||||||
<div class="modal-content">
|
<div class="modal-content">
|
||||||
<div class="modal-header">
|
<div class="modal-header">
|
||||||
<h5 class="modal-title" id="addRestaurantModalLabel">Add New Restaurant</h5>
|
<h5 class="modal-title" id="restaurantModalLabel">Add New Restaurant</h5>
|
||||||
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
||||||
</div>
|
</div>
|
||||||
<div class="modal-body">
|
<div class="modal-body">
|
||||||
<form id="addRestaurantForm">
|
<form id="restaurantForm">
|
||||||
|
<input type="hidden" id="restaurantId" name="id">
|
||||||
<div class="mb-3">
|
<div class="mb-3">
|
||||||
<label for="name" class="form-label">Restaurant Name</label>
|
<label for="name" class="form-label">Restaurant Name</label>
|
||||||
<input type="text" class="form-control" id="name" name="name" required>
|
<input type="text" class="form-control" id="name" name="name" required>
|
||||||
@ -124,23 +106,44 @@
|
|||||||
|
|
||||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
|
||||||
<script>
|
<script>
|
||||||
document.getElementById('addRestaurantForm').addEventListener('submit', function(e) {
|
document.addEventListener('DOMContentLoaded', function() {
|
||||||
|
const restaurantModal = new bootstrap.Modal(document.getElementById('restaurantModal'));
|
||||||
|
const restaurantForm = document.getElementById('restaurantForm');
|
||||||
|
const restaurantModalLabel = document.getElementById('restaurantModalLabel');
|
||||||
|
const tableBody = document.getElementById('restaurantsTableBody');
|
||||||
|
|
||||||
|
// Fetch and display restaurants on page load
|
||||||
|
fetchRestaurants();
|
||||||
|
|
||||||
|
// Handle "Add New" button click
|
||||||
|
document.getElementById('addNewBtn').addEventListener('click', function() {
|
||||||
|
restaurantForm.reset();
|
||||||
|
document.getElementById('restaurantId').value = '';
|
||||||
|
restaurantModalLabel.textContent = 'Add New Restaurant';
|
||||||
|
});
|
||||||
|
|
||||||
|
// Handle form submission for both add and edit
|
||||||
|
restaurantForm.addEventListener('submit', function(e) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
|
||||||
const formData = new FormData(this);
|
const formData = new FormData(this);
|
||||||
const data = Object.fromEntries(formData.entries());
|
const data = Object.fromEntries(formData.entries());
|
||||||
|
const restaurantId = document.getElementById('restaurantId').value;
|
||||||
|
|
||||||
fetch('api/restaurants.php?action=create', {
|
const isEdit = restaurantId !== '';
|
||||||
method: 'POST',
|
const url = isEdit ? `api/restaurants.php` : 'api/restaurants.php';
|
||||||
headers: {
|
const method = isEdit ? 'PUT' : 'POST';
|
||||||
'Content-Type': 'application/json',
|
|
||||||
},
|
fetch(url, {
|
||||||
|
method: method,
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
body: JSON.stringify(data),
|
body: JSON.stringify(data),
|
||||||
})
|
})
|
||||||
.then(response => response.json())
|
.then(response => response.json())
|
||||||
.then(result => {
|
.then(result => {
|
||||||
if (result.success) {
|
if (result.success) {
|
||||||
location.reload();
|
restaurantModal.hide();
|
||||||
|
fetchRestaurants(); // Refresh the table
|
||||||
} else {
|
} else {
|
||||||
alert('Error: ' + result.error);
|
alert('Error: ' + result.error);
|
||||||
}
|
}
|
||||||
@ -150,6 +153,98 @@
|
|||||||
alert('An unexpected error occurred.');
|
alert('An unexpected error occurred.');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
function fetchRestaurants() {
|
||||||
|
fetch('api/restaurants.php')
|
||||||
|
.then(response => response.json())
|
||||||
|
.then(result => {
|
||||||
|
if (result.success) {
|
||||||
|
renderTable(result.data);
|
||||||
|
} else {
|
||||||
|
tableBody.innerHTML = `<tr><td colspan="6" class="text-center">Could not load restaurants.</td></tr>`;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
tableBody.innerHTML = `<tr><td colspan="6" class="text-center">Error loading restaurants.</td></tr>`;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function renderTable(restaurants) {
|
||||||
|
tableBody.innerHTML = '';
|
||||||
|
if (restaurants.length === 0) {
|
||||||
|
tableBody.innerHTML = `<tr><td colspan="6" class="text-center">No restaurants found.</td></tr>`;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
restaurants.forEach(r => {
|
||||||
|
const row = document.createElement('tr');
|
||||||
|
row.setAttribute('data-id', r.id);
|
||||||
|
row.innerHTML = `
|
||||||
|
<td>${r.id}</td>
|
||||||
|
<td data-field="name">${r.name}</td>
|
||||||
|
<td data-field="address">${r.address}</td>
|
||||||
|
<td data-field="phone">${r.phone}</td>
|
||||||
|
<td data-field="email">${r.email}</td>
|
||||||
|
<td>
|
||||||
|
<a href="restaurant_menu.php?restaurant_id=${r.id}" class="btn btn-sm btn-info menu-btn" title="Manage Menu"><i class="bi bi-card-list"></i></a>
|
||||||
|
<button class="btn btn-sm btn-secondary edit-btn" title="Edit Restaurant"><i class="bi bi-pencil"></i></button>
|
||||||
|
<button class="btn btn-sm btn-danger delete-btn" title="Delete Restaurant"><i class="bi bi-trash"></i></button>
|
||||||
|
</td>
|
||||||
|
`;
|
||||||
|
tableBody.appendChild(row);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Add event listeners for the new buttons
|
||||||
|
addEventListeners();
|
||||||
|
}
|
||||||
|
|
||||||
|
function addEventListeners() {
|
||||||
|
// Edit button handler
|
||||||
|
document.querySelectorAll('.edit-btn').forEach(button => {
|
||||||
|
button.addEventListener('click', function() {
|
||||||
|
const row = this.closest('tr');
|
||||||
|
const restaurantId = row.dataset.id;
|
||||||
|
|
||||||
|
document.getElementById('restaurantId').value = restaurantId;
|
||||||
|
document.getElementById('name').value = row.querySelector('[data-field="name"]').textContent;
|
||||||
|
document.getElementById('address').value = row.querySelector('[data-field="address"]').textContent;
|
||||||
|
document.getElementById('phone').value = row.querySelector('[data-field="phone"]').textContent;
|
||||||
|
document.getElementById('email').value = row.querySelector('[data-field="email"]').textContent;
|
||||||
|
|
||||||
|
restaurantModalLabel.textContent = 'Edit Restaurant';
|
||||||
|
restaurantModal.show();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// Delete button handler
|
||||||
|
document.querySelectorAll('.delete-btn').forEach(button => {
|
||||||
|
button.addEventListener('click', function() {
|
||||||
|
const row = this.closest('tr');
|
||||||
|
const restaurantId = row.dataset.id;
|
||||||
|
|
||||||
|
if (confirm('Are you sure you want to delete this restaurant?')) {
|
||||||
|
fetch(`api/restaurants.php`, {
|
||||||
|
method: 'DELETE',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({ id: restaurantId })
|
||||||
|
})
|
||||||
|
.then(response => response.json())
|
||||||
|
.then(result => {
|
||||||
|
if (result.success) {
|
||||||
|
row.remove();
|
||||||
|
} else {
|
||||||
|
alert('Error: ' + result.error);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
console.error('Error:', error);
|
||||||
|
alert('An unexpected error occurred.');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
|
|||||||
133
api/menu.php
Normal file
133
api/menu.php
Normal file
@ -0,0 +1,133 @@
|
|||||||
|
<?php
|
||||||
|
require_once '../db/config.php';
|
||||||
|
|
||||||
|
header('Content-Type: application/json');
|
||||||
|
|
||||||
|
$method = $_SERVER['REQUEST_METHOD'];
|
||||||
|
|
||||||
|
switch ($method) {
|
||||||
|
case 'GET':
|
||||||
|
handle_get();
|
||||||
|
break;
|
||||||
|
case 'POST':
|
||||||
|
handle_post();
|
||||||
|
break;
|
||||||
|
case 'PUT':
|
||||||
|
handle_put();
|
||||||
|
break;
|
||||||
|
case 'DELETE':
|
||||||
|
handle_delete();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
header('HTTP/1.1 405 Method Not Allowed');
|
||||||
|
echo json_encode(['success' => false, 'error' => 'Method Not Allowed']);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
function handle_get() {
|
||||||
|
if (empty($_GET['restaurant_id'])) {
|
||||||
|
header('HTTP/1.1 400 Bad Request');
|
||||||
|
echo json_encode(['success' => false, 'error' => 'Restaurant ID is required.']);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
$restaurant_id = $_GET['restaurant_id'];
|
||||||
|
|
||||||
|
try {
|
||||||
|
$pdo = db();
|
||||||
|
$stmt = $pdo->prepare("SELECT id, name, description, price, category FROM menu_items WHERE restaurant_id = :restaurant_id ORDER BY category, name");
|
||||||
|
$stmt->execute([':restaurant_id' => $restaurant_id]);
|
||||||
|
$menu_items = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||||
|
echo json_encode(['success' => true, 'data' => $menu_items]);
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
header('HTTP/1.1 500 Internal Server Error');
|
||||||
|
echo json_encode(['success' => false, 'error' => 'Database error: ' . $e->getMessage()]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function handle_post() {
|
||||||
|
$data = json_decode(file_get_contents('php://input'), true);
|
||||||
|
|
||||||
|
if (empty($data['restaurant_id']) || empty($data['name']) || !isset($data['price'])) {
|
||||||
|
header('HTTP/1.1 400 Bad Request');
|
||||||
|
echo json_encode(['success' => false, 'error' => 'Restaurant ID, name, and price are required.']);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
$pdo = db();
|
||||||
|
$sql = "INSERT INTO menu_items (restaurant_id, name, description, price, category) VALUES (:restaurant_id, :name, :description, :price, :category)";
|
||||||
|
$stmt = $pdo->prepare($sql);
|
||||||
|
$stmt->execute([
|
||||||
|
':restaurant_id' => $data['restaurant_id'],
|
||||||
|
':name' => $data['name'],
|
||||||
|
':description' => $data['description'] ?? null,
|
||||||
|
':price' => $data['price'],
|
||||||
|
':category' => $data['category'] ?? null,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$lastInsertId = $pdo->lastInsertId();
|
||||||
|
$stmt = $pdo->prepare("SELECT * FROM menu_items WHERE id = :id");
|
||||||
|
$stmt->execute(['id' => $lastInsertId]);
|
||||||
|
$newItem = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||||
|
|
||||||
|
echo json_encode(['success' => true, 'data' => $newItem]);
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
header('HTTP/1.1 500 Internal Server Error');
|
||||||
|
echo json_encode(['success' => false, 'error' => 'Database error: ' . $e->getMessage()]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function handle_put() {
|
||||||
|
$data = json_decode(file_get_contents('php://input'), true);
|
||||||
|
|
||||||
|
if (empty($data['id']) || empty($data['name']) || !isset($data['price'])) {
|
||||||
|
header('HTTP/1.1 400 Bad Request');
|
||||||
|
echo json_encode(['success' => false, 'error' => 'All fields including ID are required.']);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
$pdo = db();
|
||||||
|
$sql = "UPDATE menu_items SET name = :name, description = :description, price = :price, category = :category WHERE id = :id";
|
||||||
|
$stmt = $pdo->prepare($sql);
|
||||||
|
$stmt->execute([
|
||||||
|
':id' => $data['id'],
|
||||||
|
':name' => $data['name'],
|
||||||
|
':description' => $data['description'] ?? null,
|
||||||
|
':price' => $data['price'],
|
||||||
|
':category' => $data['category'] ?? null,
|
||||||
|
]);
|
||||||
|
|
||||||
|
echo json_encode(['success' => true]);
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
header('HTTP/1.1 500 Internal Server Error');
|
||||||
|
echo json_encode(['success' => false, 'error' => 'Database error: ' . $e->getMessage()]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function handle_delete() {
|
||||||
|
$data = json_decode(file_get_contents('php://input'), true);
|
||||||
|
|
||||||
|
if (empty($data['id'])) {
|
||||||
|
header('HTTP/1.1 400 Bad Request');
|
||||||
|
echo json_encode(['success' => false, 'error' => 'Menu item ID is required.']);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
$pdo = db();
|
||||||
|
$sql = "DELETE FROM menu_items WHERE id = :id";
|
||||||
|
$stmt = $pdo->prepare($sql);
|
||||||
|
$stmt->execute([':id' => $data['id']]);
|
||||||
|
|
||||||
|
if ($stmt->rowCount() > 0) {
|
||||||
|
echo json_encode(['success' => true]);
|
||||||
|
} else {
|
||||||
|
header('HTTP/1.1 404 Not Found');
|
||||||
|
echo json_encode(['success' => false, 'error' => 'Menu item not found.']);
|
||||||
|
}
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
header('HTTP/1.1 500 Internal Server Error');
|
||||||
|
echo json_encode(['success' => false, 'error' => 'Database error: ' . $e->getMessage()]);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -3,21 +3,44 @@ require_once '../db/config.php';
|
|||||||
|
|
||||||
header('Content-Type: application/json');
|
header('Content-Type: application/json');
|
||||||
|
|
||||||
$action = $_GET['action'] ?? '';
|
$method = $_SERVER['REQUEST_METHOD'];
|
||||||
|
|
||||||
switch ($action) {
|
switch ($method) {
|
||||||
case 'create':
|
case 'GET':
|
||||||
handle_create();
|
handle_get();
|
||||||
|
break;
|
||||||
|
case 'POST':
|
||||||
|
handle_post();
|
||||||
|
break;
|
||||||
|
case 'PUT':
|
||||||
|
handle_put();
|
||||||
|
break;
|
||||||
|
case 'DELETE':
|
||||||
|
handle_delete();
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
echo json_encode(['success' => false, 'error' => 'Invalid action']);
|
header('HTTP/1.1 405 Method Not Allowed');
|
||||||
|
echo json_encode(['success' => false, 'error' => 'Method Not Allowed']);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
function handle_create() {
|
function handle_get() {
|
||||||
|
try {
|
||||||
|
$pdo = db();
|
||||||
|
$stmt = $pdo->query("SELECT id, name, address, phone, email FROM restaurants ORDER BY created_at DESC");
|
||||||
|
$restaurants = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||||
|
echo json_encode(['success' => true, 'data' => $restaurants]);
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
header('HTTP/1.1 500 Internal Server Error');
|
||||||
|
echo json_encode(['success' => false, 'error' => 'Database error: ' . $e->getMessage()]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function handle_post() {
|
||||||
$data = json_decode(file_get_contents('php://input'), true);
|
$data = json_decode(file_get_contents('php://input'), true);
|
||||||
|
|
||||||
if (empty($data['name']) || empty($data['address']) || empty($data['phone']) || empty($data['email'])) {
|
if (empty($data['name']) || empty($data['address']) || empty($data['phone']) || empty($data['email'])) {
|
||||||
|
header('HTTP/1.1 400 Bad Request');
|
||||||
echo json_encode(['success' => false, 'error' => 'All fields are required.']);
|
echo json_encode(['success' => false, 'error' => 'All fields are required.']);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -33,8 +56,71 @@ function handle_create() {
|
|||||||
':email' => $data['email'],
|
':email' => $data['email'],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
echo json_encode(['success' => true]);
|
$lastInsertId = $pdo->lastInsertId();
|
||||||
|
|
||||||
|
// Fetch the created restaurant to return it
|
||||||
|
$stmt = $pdo->prepare("SELECT id, name, address, phone, email FROM restaurants WHERE id = :id");
|
||||||
|
$stmt->execute(['id' => $lastInsertId]);
|
||||||
|
$newRestaurant = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||||
|
|
||||||
|
echo json_encode(['success' => true, 'data' => $newRestaurant]);
|
||||||
} catch (PDOException $e) {
|
} catch (PDOException $e) {
|
||||||
|
header('HTTP/1.1 500 Internal Server Error');
|
||||||
|
echo json_encode(['success' => false, 'error' => 'Database error: ' . $e->getMessage()]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function handle_put() {
|
||||||
|
$data = json_decode(file_get_contents('php://input'), true);
|
||||||
|
|
||||||
|
if (empty($data['id']) || empty($data['name']) || empty($data['address']) || empty($data['phone']) || empty($data['email'])) {
|
||||||
|
header('HTTP/1.1 400 Bad Request');
|
||||||
|
echo json_encode(['success' => false, 'error' => 'All fields including ID are required.']);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
$pdo = db();
|
||||||
|
$sql = "UPDATE restaurants SET name = :name, address = :address, phone = :phone, email = :email WHERE id = :id";
|
||||||
|
$stmt = $pdo->prepare($sql);
|
||||||
|
$stmt->execute([
|
||||||
|
':id' => $data['id'],
|
||||||
|
':name' => $data['name'],
|
||||||
|
':address' => $data['address'],
|
||||||
|
':phone' => $data['phone'],
|
||||||
|
':email' => $data['email'],
|
||||||
|
]);
|
||||||
|
|
||||||
|
echo json_encode(['success' => true]);
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
header('HTTP/1.1 500 Internal Server Error');
|
||||||
|
echo json_encode(['success' => false, 'error' => 'Database error: ' . $e->getMessage()]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function handle_delete() {
|
||||||
|
$data = json_decode(file_get_contents('php://input'), true);
|
||||||
|
|
||||||
|
if (empty($data['id'])) {
|
||||||
|
header('HTTP/1.1 400 Bad Request');
|
||||||
|
echo json_encode(['success' => false, 'error' => 'Restaurant ID is required.']);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
$pdo = db();
|
||||||
|
$sql = "DELETE FROM restaurants WHERE id = :id";
|
||||||
|
$stmt = $pdo->prepare($sql);
|
||||||
|
$stmt->execute([':id' => $data['id']]);
|
||||||
|
|
||||||
|
if ($stmt->rowCount() > 0) {
|
||||||
|
echo json_encode(['success' => true]);
|
||||||
|
} else {
|
||||||
|
header('HTTP/1.1 404 Not Found');
|
||||||
|
echo json_encode(['success' => false, 'error' => 'Restaurant not found.']);
|
||||||
|
}
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
header('HTTP/1.1 500 Internal Server Error');
|
||||||
echo json_encode(['success' => false, 'error' => 'Database error: ' . $e->getMessage()]);
|
echo json_encode(['success' => false, 'error' => 'Database error: ' . $e->getMessage()]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
189
index.php
189
index.php
@ -1,144 +1,87 @@
|
|||||||
<?php
|
<?php
|
||||||
declare(strict_types=1);
|
require_once 'db/config.php';
|
||||||
@ini_set('display_errors', '1');
|
|
||||||
@error_reporting(E_ALL);
|
$restaurants = [];
|
||||||
@date_default_timezone_set('UTC');
|
try {
|
||||||
|
$pdo = db();
|
||||||
|
$stmt = $pdo->query("SELECT id, name, address, phone, email FROM restaurants ORDER BY name ASC");
|
||||||
|
$restaurants = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
// For a real app, you would log this error and show a user-friendly message.
|
||||||
|
error_log("Database error: " . $e->getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
$phpVersion = PHP_VERSION;
|
|
||||||
$now = date('Y-m-d H:i:s');
|
|
||||||
?>
|
?>
|
||||||
<!doctype html>
|
<!DOCTYPE html>
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="utf-8" />
|
<meta charset="UTF-8">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
<title>New Style</title>
|
<title>Find a Restaurant</title>
|
||||||
<?php
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
|
||||||
// Read project preview data from environment
|
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600;700&display=swap" rel="stylesheet">
|
||||||
$projectDescription = $_SERVER['PROJECT_DESCRIPTION'] ?? '';
|
|
||||||
$projectImageUrl = $_SERVER['PROJECT_IMAGE_URL'] ?? '';
|
|
||||||
?>
|
|
||||||
<?php if ($projectDescription): ?>
|
|
||||||
<!-- Meta description -->
|
|
||||||
<meta name="description" content='<?= htmlspecialchars($projectDescription) ?>' />
|
|
||||||
<!-- Open Graph meta tags -->
|
|
||||||
<meta property="og:description" content="<?= htmlspecialchars($projectDescription) ?>" />
|
|
||||||
<!-- Twitter meta tags -->
|
|
||||||
<meta property="twitter:description" content="<?= htmlspecialchars($projectDescription) ?>" />
|
|
||||||
<?php endif; ?>
|
|
||||||
<?php if ($projectImageUrl): ?>
|
|
||||||
<!-- Open Graph image -->
|
|
||||||
<meta property="og:image" content="<?= htmlspecialchars($projectImageUrl) ?>" />
|
|
||||||
<!-- Twitter image -->
|
|
||||||
<meta property="twitter:image" content="<?= htmlspecialchars($projectImageUrl) ?>" />
|
|
||||||
<?php endif; ?>
|
|
||||||
<link rel="preconnect" href="https://fonts.googleapis.com">
|
|
||||||
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
|
||||||
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;700&display=swap" rel="stylesheet">
|
|
||||||
<style>
|
<style>
|
||||||
:root {
|
|
||||||
--bg-color-start: #6a11cb;
|
|
||||||
--bg-color-end: #2575fc;
|
|
||||||
--text-color: #ffffff;
|
|
||||||
--card-bg-color: rgba(255, 255, 255, 0.01);
|
|
||||||
--card-border-color: rgba(255, 255, 255, 0.1);
|
|
||||||
}
|
|
||||||
body {
|
body {
|
||||||
margin: 0;
|
font-family: 'Poppins', sans-serif;
|
||||||
font-family: 'Inter', sans-serif;
|
background-color: #F8F9FA;
|
||||||
background: linear-gradient(45deg, var(--bg-color-start), var(--bg-color-end));
|
}
|
||||||
color: var(--text-color);
|
.hero {
|
||||||
display: flex;
|
background-color: #343A40;
|
||||||
justify-content: center;
|
color: #FFFFFF;
|
||||||
align-items: center;
|
padding: 4rem 0;
|
||||||
min-height: 100vh;
|
|
||||||
text-align: center;
|
text-align: center;
|
||||||
overflow: hidden;
|
|
||||||
position: relative;
|
|
||||||
}
|
}
|
||||||
body::before {
|
.restaurant-card {
|
||||||
content: '';
|
transition: transform 0.2s, box-shadow 0.2s;
|
||||||
position: absolute;
|
|
||||||
top: 0;
|
|
||||||
left: 0;
|
|
||||||
width: 100%;
|
|
||||||
height: 100%;
|
|
||||||
background-image: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100" viewBox="0 0 100 100"><path d="M-10 10L110 10M10 -10L10 110" stroke-width="1" stroke="rgba(255,255,255,0.05)"/></svg>');
|
|
||||||
animation: bg-pan 20s linear infinite;
|
|
||||||
z-index: -1;
|
|
||||||
}
|
}
|
||||||
@keyframes bg-pan {
|
.restaurant-card:hover {
|
||||||
0% { background-position: 0% 0%; }
|
transform: translateY(-5px);
|
||||||
100% { background-position: 100% 100%; }
|
box-shadow: 0 0.5rem 1rem rgba(0,0,0,0.15);
|
||||||
}
|
}
|
||||||
main {
|
.btn-primary {
|
||||||
padding: 2rem;
|
background-color: #FF6347;
|
||||||
|
border-color: #FF6347;
|
||||||
}
|
}
|
||||||
.card {
|
.btn-primary:hover {
|
||||||
background: var(--card-bg-color);
|
background-color: #E5533D;
|
||||||
border: 1px solid var(--card-border-color);
|
border-color: #E5533D;
|
||||||
border-radius: 16px;
|
|
||||||
padding: 2rem;
|
|
||||||
backdrop-filter: blur(20px);
|
|
||||||
-webkit-backdrop-filter: blur(20px);
|
|
||||||
box-shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.1);
|
|
||||||
}
|
|
||||||
.hint {
|
|
||||||
opacity: 0.9;
|
|
||||||
}
|
|
||||||
h1 {
|
|
||||||
font-size: 3rem;
|
|
||||||
font-weight: 700;
|
|
||||||
margin: 0 0 1rem;
|
|
||||||
letter-spacing: -1px;
|
|
||||||
}
|
|
||||||
p {
|
|
||||||
margin: 0.5rem 0;
|
|
||||||
font-size: 1.1rem;
|
|
||||||
}
|
|
||||||
code {
|
|
||||||
background: rgba(0,0,0,0.2);
|
|
||||||
padding: 2px 6px;
|
|
||||||
border-radius: 4px;
|
|
||||||
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
|
|
||||||
}
|
|
||||||
footer {
|
|
||||||
position: absolute;
|
|
||||||
bottom: 1rem;
|
|
||||||
font-size: 0.8rem;
|
|
||||||
opacity: 0.7;
|
|
||||||
}
|
|
||||||
.btn {
|
|
||||||
display: inline-block;
|
|
||||||
padding: 0.75rem 1.5rem;
|
|
||||||
background-color: var(--bg-color-end);
|
|
||||||
color: var(--text-color);
|
|
||||||
text-decoration: none;
|
|
||||||
border-radius: 8px;
|
|
||||||
transition: background-color 0.3s;
|
|
||||||
border: none;
|
|
||||||
font-size: 1rem;
|
|
||||||
font-weight: 500;
|
|
||||||
cursor: pointer;
|
|
||||||
}
|
|
||||||
.btn:hover {
|
|
||||||
background-color: #1e5fa5;
|
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<main>
|
|
||||||
<div class="card">
|
<header class="hero">
|
||||||
<h1>Welcome to Your New Application!</h1>
|
<div class="container">
|
||||||
<p class="hint">This is the starting point of your project.</p>
|
<h1 class="display-4">Find Your Next Meal</h1>
|
||||||
<p>
|
<p class="lead">Browse through our collection of partner restaurants.</p>
|
||||||
<a href="admin_restaurants.php" class="btn">Manage Restaurants</a>
|
</div>
|
||||||
</p>
|
</header>
|
||||||
<p style="margin-top: 2rem;">Runtime: PHP <code><?= htmlspecialchars($phpVersion) ?></code> — UTC <code><?= htmlspecialchars($now) ?></code></p>
|
|
||||||
|
<main class="container my-5">
|
||||||
|
<div class="row">
|
||||||
|
<?php if (empty($restaurants)): ?>
|
||||||
|
<div class="col">
|
||||||
|
<p class="text-center text-muted">No restaurants are available at the moment. Please check back later.</p>
|
||||||
|
</div>
|
||||||
|
<?php else: ?>
|
||||||
|
<?php foreach ($restaurants as $restaurant): ?>
|
||||||
|
<div class="col-md-4 mb-4">
|
||||||
|
<div class="card h-100 restaurant-card">
|
||||||
|
<div class="card-body d-flex flex-column">
|
||||||
|
<h5 class="card-title"><?= htmlspecialchars($restaurant['name']) ?></h5>
|
||||||
|
<p class="card-text text-muted flex-grow-1"><?= htmlspecialchars($restaurant['address']) ?></p>
|
||||||
|
<a href="menu.php?restaurant_id=<?= $restaurant['id'] ?>" class="btn btn-primary mt-auto">View Menu</a>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<?php endforeach; ?>
|
||||||
|
<?php endif; ?>
|
||||||
</div>
|
</div>
|
||||||
</main>
|
</main>
|
||||||
<footer>
|
|
||||||
Page updated: <?= htmlspecialchars($now) ?> (UTC)
|
<footer class="text-center text-muted py-4">
|
||||||
|
<p>© <?= date('Y') ?> Food Marketplace</p>
|
||||||
</footer>
|
</footer>
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
109
menu.php
Normal file
109
menu.php
Normal file
@ -0,0 +1,109 @@
|
|||||||
|
<?php
|
||||||
|
require_once 'db/config.php';
|
||||||
|
|
||||||
|
if (!isset($_GET['restaurant_id']) || !is_numeric($_GET['restaurant_id'])) {
|
||||||
|
die("A valid restaurant ID is required.");
|
||||||
|
}
|
||||||
|
$restaurant_id = intval($_GET['restaurant_id']);
|
||||||
|
|
||||||
|
// Fetch restaurant details
|
||||||
|
$restaurant = null;
|
||||||
|
try {
|
||||||
|
$pdo = db();
|
||||||
|
$stmt = $pdo->prepare("SELECT name, address, phone FROM restaurants WHERE id = :id");
|
||||||
|
$stmt->execute(['id' => $restaurant_id]);
|
||||||
|
$restaurant = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
error_log("DB error fetching restaurant: " . $e->getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$restaurant) {
|
||||||
|
die("Restaurant not found.");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fetch menu items
|
||||||
|
$menu_items = [];
|
||||||
|
try {
|
||||||
|
$stmt = $pdo->prepare("SELECT name, description, price, category FROM menu_items WHERE restaurant_id = :restaurant_id ORDER BY category, name");
|
||||||
|
$stmt->execute(['restaurant_id' => $restaurant_id]);
|
||||||
|
$menu_items = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
error_log("DB error fetching menu items: " . $e->getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Group menu items by category
|
||||||
|
$menu_by_category = [];
|
||||||
|
foreach ($menu_items as $item) {
|
||||||
|
$category = $item['category'] ?: 'Uncategorized';
|
||||||
|
$menu_by_category[$category][] = $item;
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>Menu for <?= htmlspecialchars($restaurant['name']) ?></title>
|
||||||
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
|
||||||
|
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600;700&display=swap" rel="stylesheet">
|
||||||
|
<style>
|
||||||
|
body { font-family: 'Poppins', sans-serif; background-color: #F8F9FA; }
|
||||||
|
.menu-header {
|
||||||
|
background: #343A40;
|
||||||
|
color: white;
|
||||||
|
padding: 3rem 0;
|
||||||
|
}
|
||||||
|
.menu-item {
|
||||||
|
border-bottom: 1px dashed #E0E0E0;
|
||||||
|
padding: 1rem 0;
|
||||||
|
}
|
||||||
|
.menu-item:last-child {
|
||||||
|
border-bottom: none;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
|
||||||
|
<div class="menu-header text-center">
|
||||||
|
<div class="container">
|
||||||
|
<h1 class="display-5"><?= htmlspecialchars($restaurant['name']) ?></h1>
|
||||||
|
<p class="lead"><?= htmlspecialchars($restaurant['address']) ?></p>
|
||||||
|
<?php if ($restaurant['phone']): ?>
|
||||||
|
<p class="text-white-50">Call us at: <?= htmlspecialchars($restaurant['phone']) ?></p>
|
||||||
|
<?php endif; ?>
|
||||||
|
<a href="index.php" class="btn btn-sm btn-outline-light mt-3"><i class="bi bi-arrow-left"></i> Back to all restaurants</a>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<main class="container my-5">
|
||||||
|
<?php if (empty($menu_by_category)): ?>
|
||||||
|
<div class="text-center">
|
||||||
|
<p class="text-muted fs-4">This restaurant hasn't added any menu items yet.</p>
|
||||||
|
</div>
|
||||||
|
<?php else: ?>
|
||||||
|
<?php foreach ($menu_by_category as $category => $items): ?>
|
||||||
|
<div class="mb-5">
|
||||||
|
<h2 class="mb-4"><?= htmlspecialchars($category) ?></h2>
|
||||||
|
<?php foreach ($items as $item): ?>
|
||||||
|
<div class="row menu-item">
|
||||||
|
<div class="col-8">
|
||||||
|
<h5 class="mb-1"><?= htmlspecialchars($item['name']) ?></h5>
|
||||||
|
<p class="text-muted mb-0"><?= htmlspecialchars($item['description']) ?></p>
|
||||||
|
</div>
|
||||||
|
<div class="col-4 text-end">
|
||||||
|
<p class="fw-bold fs-5">$<?= htmlspecialchars(number_format((float)$item['price'], 2)) ?></p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<?php endforeach; ?>
|
||||||
|
</div>
|
||||||
|
<?php endforeach; ?>
|
||||||
|
<?php endif; ?>
|
||||||
|
</main>
|
||||||
|
|
||||||
|
<footer class="text-center text-muted py-4">
|
||||||
|
<p>© <?= date('Y') ?> Food Marketplace</p>
|
||||||
|
</footer>
|
||||||
|
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
249
restaurant_menu.php
Normal file
249
restaurant_menu.php
Normal file
@ -0,0 +1,249 @@
|
|||||||
|
<?php
|
||||||
|
require_once 'db/config.php';
|
||||||
|
|
||||||
|
// Get restaurant ID from URL
|
||||||
|
if (!isset($_GET['restaurant_id']) || !is_numeric($_GET['restaurant_id'])) {
|
||||||
|
die("A valid restaurant ID is required.");
|
||||||
|
}
|
||||||
|
$restaurant_id = intval($_GET['restaurant_id']);
|
||||||
|
|
||||||
|
// Fetch restaurant details
|
||||||
|
$restaurant_name = 'Unknown Restaurant';
|
||||||
|
try {
|
||||||
|
$pdo = db();
|
||||||
|
$stmt = $pdo->prepare("SELECT name FROM restaurants WHERE id = :id");
|
||||||
|
$stmt->execute(['id' => $restaurant_id]);
|
||||||
|
$restaurant = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||||
|
if ($restaurant) {
|
||||||
|
$restaurant_name = htmlspecialchars($restaurant['name']);
|
||||||
|
} else {
|
||||||
|
die("Restaurant not found.");
|
||||||
|
}
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
die("Database error while fetching restaurant details.");
|
||||||
|
}
|
||||||
|
?>
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>Manage Menu for <?php echo $restaurant_name; ?></title>
|
||||||
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
|
||||||
|
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
|
||||||
|
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600;700&display=swap" rel="stylesheet">
|
||||||
|
<style>
|
||||||
|
body { font-family: 'Poppins', sans-serif; background-color: #F8F9FA; }
|
||||||
|
.btn-primary { background-color: #4682B4; border-color: #4682B4; }
|
||||||
|
.btn-primary:hover { background-color: #3A6A92; border-color: #3A6A92; }
|
||||||
|
.table { background-color: #FFFFFF; border-radius: 0.5rem; box-shadow: 0 0.125rem 0.25rem rgba(0,0,0,0.075); }
|
||||||
|
.card, .modal-content { border-radius: 0.5rem; }
|
||||||
|
.table th, .table td { vertical-align: middle; }
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
|
||||||
|
<div class="container mt-5">
|
||||||
|
<div class="d-flex justify-content-between align-items-center mb-4">
|
||||||
|
<div>
|
||||||
|
<a href="admin_restaurants.php" class="btn btn-sm btn-outline-secondary mb-2"><i class="bi bi-arrow-left"></i> Back to Restaurants</a>
|
||||||
|
<h1>Manage Menu</h1>
|
||||||
|
<h5 class="text-muted">for <?php echo $restaurant_name; ?></h5>
|
||||||
|
</div>
|
||||||
|
<button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#menuItemModal" id="addNewBtn">
|
||||||
|
<i class="bi bi-plus-lg"></i> Add New Item
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="card">
|
||||||
|
<div class="card-body">
|
||||||
|
<table class="table table-hover">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>Name</th>
|
||||||
|
<th>Description</th>
|
||||||
|
<th>Price</th>
|
||||||
|
<th>Category</th>
|
||||||
|
<th>Actions</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody id="menuItemsTableBody">
|
||||||
|
<!-- Menu items will be loaded here dynamically -->
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Add/Edit Menu Item Modal -->
|
||||||
|
<div class="modal fade" id="menuItemModal" tabindex="-1" aria-labelledby="menuItemModalLabel" aria-hidden="true">
|
||||||
|
<div class="modal-dialog">
|
||||||
|
<div class="modal-content">
|
||||||
|
<div class="modal-header">
|
||||||
|
<h5 class="modal-title" id="menuItemModalLabel">Add New Menu Item</h5>
|
||||||
|
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
||||||
|
</div>
|
||||||
|
<div class="modal-body">
|
||||||
|
<form id="menuItemForm">
|
||||||
|
<input type="hidden" id="menuItemId" name="id">
|
||||||
|
<input type="hidden" id="restaurantId" name="restaurant_id" value="<?php echo $restaurant_id; ?>">
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="name" class="form-label">Item Name</label>
|
||||||
|
<input type="text" class="form-control" id="name" name="name" 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="row">
|
||||||
|
<div class="col-md-6 mb-3">
|
||||||
|
<label for="price" class="form-label">Price</label>
|
||||||
|
<div class="input-group">
|
||||||
|
<span class="input-group-text">$</span>
|
||||||
|
<input type="number" class="form-control" id="price" name="price" step="0.01" required>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="col-md-6 mb-3">
|
||||||
|
<label for="category" class="form-label">Category</label>
|
||||||
|
<input type="text" class="form-control" id="category" name="category" placeholder="e.g., Appetizer, Main, Dessert">
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<button type="submit" class="btn btn-primary">Save Item</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
|
||||||
|
<script>
|
||||||
|
document.addEventListener('DOMContentLoaded', function() {
|
||||||
|
const restaurantId = <?php echo $restaurant_id; ?>;
|
||||||
|
const menuItemModal = new bootstrap.Modal(document.getElementById('menuItemModal'));
|
||||||
|
const menuItemForm = document.getElementById('menuItemForm');
|
||||||
|
const menuItemModalLabel = document.getElementById('menuItemModalLabel');
|
||||||
|
const tableBody = document.getElementById('menuItemsTableBody');
|
||||||
|
|
||||||
|
fetchMenuItems();
|
||||||
|
|
||||||
|
document.getElementById('addNewBtn').addEventListener('click', function() {
|
||||||
|
menuItemForm.reset();
|
||||||
|
document.getElementById('menuItemId').value = '';
|
||||||
|
document.getElementById('restaurantId').value = restaurantId; // Ensure restaurantId is set on new items
|
||||||
|
menuItemModalLabel.textContent = 'Add New Menu Item';
|
||||||
|
});
|
||||||
|
|
||||||
|
menuItemForm.addEventListener('submit', function(e) {
|
||||||
|
e.preventDefault();
|
||||||
|
|
||||||
|
const formData = new FormData(this);
|
||||||
|
const data = Object.fromEntries(formData.entries());
|
||||||
|
const menuItemId = document.getElementById('menuItemId').value;
|
||||||
|
|
||||||
|
const isEdit = menuItemId !== '';
|
||||||
|
const method = isEdit ? 'PUT' : 'POST';
|
||||||
|
|
||||||
|
fetch('api/menu.php', {
|
||||||
|
method: method,
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify(data),
|
||||||
|
})
|
||||||
|
.then(response => response.json())
|
||||||
|
.then(result => {
|
||||||
|
if (result.success) {
|
||||||
|
menuItemModal.hide();
|
||||||
|
fetchMenuItems();
|
||||||
|
} else {
|
||||||
|
alert('Error: ' + result.error);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(error => console.error('Error:', error));
|
||||||
|
});
|
||||||
|
|
||||||
|
function fetchMenuItems() {
|
||||||
|
fetch(`api/menu.php?restaurant_id=${restaurantId}`)
|
||||||
|
.then(response => response.json())
|
||||||
|
.then(result => {
|
||||||
|
if (result.success) {
|
||||||
|
renderTable(result.data);
|
||||||
|
} else {
|
||||||
|
tableBody.innerHTML = `<tr><td colspan="5" class="text-center">Could not load menu items.</td></tr>`;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
tableBody.innerHTML = `<tr><td colspan="5" class="text-center">Error loading menu items.</td></tr>`;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function renderTable(items) {
|
||||||
|
tableBody.innerHTML = '';
|
||||||
|
if (items.length === 0) {
|
||||||
|
tableBody.innerHTML = `<tr><td colspan="5" class="text-center">No menu items found. Add one to get started.</td></tr>`;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
items.forEach(item => {
|
||||||
|
const row = document.createElement('tr');
|
||||||
|
row.setAttribute('data-id', item.id);
|
||||||
|
row.innerHTML = `
|
||||||
|
<td data-field="name">${item.name}</td>
|
||||||
|
<td data-field="description">${item.description || ''}</td>
|
||||||
|
<td data-field="price">${parseFloat(item.price).toFixed(2)}</td>
|
||||||
|
<td data-field="category">${item.category || ''}</td>
|
||||||
|
<td>
|
||||||
|
<button class="btn btn-sm btn-secondary edit-btn"><i class="bi bi-pencil"></i></button>
|
||||||
|
<button class="btn btn-sm btn-danger delete-btn"><i class="bi bi-trash"></i></button>
|
||||||
|
</td>
|
||||||
|
`;
|
||||||
|
tableBody.appendChild(row);
|
||||||
|
});
|
||||||
|
|
||||||
|
addEventListeners();
|
||||||
|
}
|
||||||
|
|
||||||
|
function addEventListeners() {
|
||||||
|
document.querySelectorAll('.edit-btn').forEach(button => {
|
||||||
|
button.addEventListener('click', function() {
|
||||||
|
const row = this.closest('tr');
|
||||||
|
const menuItemId = row.dataset.id;
|
||||||
|
|
||||||
|
document.getElementById('menuItemId').value = menuItemId;
|
||||||
|
document.getElementById('name').value = row.querySelector('[data-field="name"]').textContent;
|
||||||
|
document.getElementById('description').value = row.querySelector('[data-field="description"]').textContent;
|
||||||
|
document.getElementById('price').value = row.querySelector('[data-field="price"]').textContent;
|
||||||
|
document.getElementById('category').value = row.querySelector('[data-field="category"]').textContent;
|
||||||
|
|
||||||
|
menuItemModalLabel.textContent = 'Edit Menu Item';
|
||||||
|
menuItemModal.show();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
document.querySelectorAll('.delete-btn').forEach(button => {
|
||||||
|
button.addEventListener('click', function() {
|
||||||
|
const row = this.closest('tr');
|
||||||
|
const menuItemId = row.dataset.id;
|
||||||
|
|
||||||
|
if (confirm('Are you sure you want to delete this menu item?')) {
|
||||||
|
fetch(`api/menu.php`, {
|
||||||
|
method: 'DELETE',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({ id: menuItemId })
|
||||||
|
})
|
||||||
|
.then(response => response.json())
|
||||||
|
.then(result => {
|
||||||
|
if (result.success) {
|
||||||
|
row.remove();
|
||||||
|
} else {
|
||||||
|
alert('Error: ' + result.error);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(error => console.error('Error:', error));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
Loading…
x
Reference in New Issue
Block a user