35324-vm/service.php
Flatlogic Bot 1b9afa40ef v4
2025-10-29 10:45:26 +00:00

138 lines
5.4 KiB
PHP

<?php
require_once 'db/config.php';
$service_id = $_GET['id'] ?? null;
if (!$service_id) {
header("Location: index.php");
exit;
}
$service = null;
$other_providers = [];
$error_message = null;
try {
$pdo = db();
// Fetch the main service details
$sql = "
SELECT
s.id,
s.name,
s.description,
s.price,
c.name as category_name,
u.first_name,
u.last_name,
u.location
FROM services s
JOIN users u ON s.provider_id = u.id
JOIN service_categories c ON s.category_id = c.id
WHERE s.id = :service_id AND u.user_type = 'provider'
";
$stmt = $pdo->prepare($sql);
$stmt->execute([':service_id' => $service_id]);
$service = $stmt->fetch();
if ($service) {
// Fetch other providers offering a service with the same name in the same location
$sql_others = "
SELECT
u.first_name,
u.last_name,
u.location,
s.price
FROM services s
JOIN users u ON s.provider_id = u.id
WHERE s.name = :service_name
AND u.location = :location
AND s.id != :service_id
AND u.user_type = 'provider'
ORDER BY s.price ASC
";
$stmt_others = $pdo->prepare($sql_others);
$stmt_others->execute([
':service_name' => $service['name'],
':location' => $service['location'],
':service_id' => $service_id
]);
$other_providers = $stmt_others->fetchAll();
}
} catch (PDOException $e) {
$error_message = "Si è verificato un errore. Riprova più tardi.";
// In a real app, you would log this error: error_log($e->getMessage());
}
$pageTitle = $service ? htmlspecialchars($service['name']) : "Servizio non trovato";
$pageDescription = $service ? htmlspecialchars(substr($service['description'], 0, 155)) : "";
?>
<!DOCTYPE html>
<html lang="it">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?= $pageTitle ?> - MeToo</title>
<meta name="description" content="<?= $pageDescription ?>">
<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.1/font/bootstrap-icons.css">
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
</head>
<body>
<?php include __DIR__ . '/_header.php'; ?>
<main class="container my-5">
<?php if ($error_message): ?>
<div class="alert alert-danger"><?= $error_message ?></div>
<?php elseif (!$service): ?>
<div class="alert alert-warning">Servizio non trovato.</div>
<?php else: ?>
<div class="row">
<div class="col-lg-8">
<div class="card">
<div class="card-body">
<h1 class="card-title h2"><?= htmlspecialchars($service['name']) ?></h1>
<h6 class="card-subtitle mb-3 text-muted"><?= htmlspecialchars($service['category_name']) ?></h6>
<p class="lead"><?= htmlspecialchars($service['description']) ?></p>
<hr>
<p>
<strong>Prezzo:</strong> € <?= htmlspecialchars(number_format($service['price'], 2, ',', '.')) ?><br>
<strong>Fornitore:</strong> <?= htmlspecialchars($service['first_name'] . ' ' . $service['last_name']) ?><br>
<strong>Comune:</strong> <?= htmlspecialchars($service['location']) ?>
</p>
</div>
</div>
</div>
<div class="col-lg-4">
<div class="card">
<div class="card-body">
<h5 class="card-title">Migliori fornitori in zona</h5>
<?php if (empty($other_providers)): ?>
<p class="text-muted">Nessun altro fornitore trovato in questa zona per questo servizio.</p>
<?php else: ?>
<ul class="list-group list-group-flush">
<?php foreach ($other_providers as $provider): ?>
<li class="list-group-item d-flex justify-content-between align-items-center">
<?= htmlspecialchars($provider['first_name'] . ' ' . $provider['last_name']) ?>
<span class="badge bg-primary rounded-pill">€ <?= htmlspecialchars(number_format($provider['price'], 2, ',', '.')) ?></span>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</div>
</div>
</div>
</div>
<?php endif; ?>
</main>
<?php include __DIR__ . '/_footer.php'; ?>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
<script src="assets/js/main.js?v=<?php echo time(); ?>"></script>
</body>
</html>