132 lines
5.3 KiB
PHP
132 lines
5.3 KiB
PHP
<?php
|
|
// Handle AJAX request for nearby restaurants
|
|
if (isset($_GET['action']) && $_GET['action'] == 'get_restaurants' && isset($_GET['lat']) && isset($_GET['lng'])) {
|
|
require_once 'db/config.php';
|
|
header('Content-Type: application/json');
|
|
|
|
$lat = (float)$_GET['lat'];
|
|
$lng = (float)$_GET['lng'];
|
|
$radius = 10; // Search radius in kilometers
|
|
|
|
$db = db();
|
|
// Haversine formula to calculate distance
|
|
$stmt = $db->prepare("
|
|
SELECT r.id, r.name, r.image_url, AVG(rt.rating) as average_rating,
|
|
(6371 * acos(cos(radians(?)) * cos(radians(latitude)) * cos(radians(longitude) - radians(?)) + sin(radians(?)) * sin(radians(latitude)))) AS distance
|
|
FROM restaurants r
|
|
LEFT JOIN ratings rt ON r.id = rt.restaurant_id
|
|
WHERE latitude IS NOT NULL AND longitude IS NOT NULL
|
|
GROUP BY r.id
|
|
HAVING distance < ?
|
|
ORDER BY distance
|
|
LIMIT 12
|
|
");
|
|
|
|
$stmt->execute([$lat, $lng, $lat, $radius]);
|
|
$restaurants = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
// Get cuisines for each restaurant
|
|
$cuisine_sql = "SELECT c.name FROM cuisines c JOIN restaurant_cuisines rc ON c.id = rc.cuisine_id WHERE rc.restaurant_id = ?";
|
|
$cuisine_stmt = $db->prepare($cuisine_sql);
|
|
|
|
foreach ($restaurants as &$restaurant) {
|
|
$cuisine_stmt->execute([$restaurant['id']]);
|
|
$restaurant['cuisines'] = $cuisine_stmt->fetchAll(PDO::FETCH_COLUMN);
|
|
}
|
|
|
|
echo json_encode($restaurants);
|
|
exit;
|
|
}
|
|
?>
|
|
<?php include 'header.php'; ?>
|
|
<?php include 'hero.php'; ?>
|
|
|
|
<div class="container page-content">
|
|
|
|
<section class="promo-section">
|
|
<div class="promo-card">
|
|
<h3>$0 Delivery Fee</h3>
|
|
<p>On your first order</p>
|
|
</div>
|
|
<div class="promo-card">
|
|
<h3>Earn Rewards</h3>
|
|
<p>With every meal</p>
|
|
</div>
|
|
<div class="promo-card">
|
|
<h3>Support Local</h3>
|
|
<p>Majuro Restaurants</p>
|
|
</div>
|
|
</section>
|
|
|
|
<section class="featured-restaurants">
|
|
<h2 class="section-title">Featured Restaurants Near You</h2>
|
|
<div id="restaurant-grid" class="restaurant-grid">
|
|
<!-- Restaurants will be loaded here by JavaScript -->
|
|
<p id="restaurants-placeholder">Set your location to see nearby restaurants.</p>
|
|
</div>
|
|
<div class="see-all-container" style="display: none;">
|
|
<a href="index.php" class="see-all-btn">See all restaurants</a>
|
|
</div>
|
|
</section>
|
|
|
|
</div>
|
|
|
|
<div id="location-modal" class="modal">
|
|
<div class="modal-content">
|
|
<span class="close-button">×</span>
|
|
<h2>Pin Your Location</h2>
|
|
<div id="map"></div>
|
|
<button id="confirm-location" class="btn-primary">Confirm Location</button>
|
|
</div>
|
|
</div>
|
|
|
|
<?php include 'footer.php'; ?>
|
|
|
|
<script>
|
|
document.addEventListener('DOMContentLoaded', function() {
|
|
const locationData = sessionStorage.getItem('userLocation');
|
|
const restaurantGrid = document.getElementById('restaurant-grid');
|
|
const placeholder = document.getElementById('restaurants-placeholder');
|
|
|
|
if (locationData) {
|
|
const { lat, lng } = JSON.parse(locationData);
|
|
placeholder.textContent = 'Finding restaurants near you...';
|
|
|
|
fetch(`index.php?action=get_restaurants&lat=${lat}&lng=${lng}`)
|
|
.then(response => response.json())
|
|
.then(restaurants => {
|
|
restaurantGrid.innerHTML = ''; // Clear placeholder
|
|
if (restaurants.length > 0) {
|
|
restaurants.forEach(restaurant => {
|
|
const card = `
|
|
<a href="menu.php?restaurant_id=${restaurant.id}" class="restaurant-card">
|
|
<div class="card-image" style="background-image: url('${restaurant.image_url || 'assets/images/hero.jpg'}')"></div>
|
|
<div class="card-content">
|
|
<h3>${restaurant.name}</h3>
|
|
<p class="cuisine-tags">${restaurant.cuisines.join(', ')}</p>
|
|
<div class="restaurant-info">
|
|
<div class="rating-display">
|
|
${restaurant.average_rating ? `<span class="star">★</span> ${parseFloat(restaurant.average_rating).toFixed(1)}` : 'New'}
|
|
</div>
|
|
<div class="delivery-info">${Math.round(restaurant.distance * 5) + 15}-${Math.round(restaurant.distance * 5) + 25} min</div>
|
|
</div>
|
|
</div>
|
|
</a>
|
|
`;
|
|
restaurantGrid.innerHTML += card;
|
|
});
|
|
document.querySelector('.see-all-container').style.display = 'block';
|
|
} else {
|
|
placeholder.textContent = 'No restaurants found delivering to your location.';
|
|
restaurantGrid.appendChild(placeholder);
|
|
}
|
|
})
|
|
.catch(error => {
|
|
console.error('Error fetching restaurants:', error);
|
|
placeholder.textContent = 'Could not load restaurants. Please try again later.';
|
|
restaurantGrid.appendChild(placeholder);
|
|
});
|
|
}
|
|
});
|
|
</script>
|