yogiboook_new/public/userarea/product_detail.php

297 lines
12 KiB
PHP

<?php include('include/headscript.php'); ?>
<?php
// Ottieni l'istanza del DBHandlerSelect
$dbHandler = DBHandlerSelect::getInstance();
$pdo = $dbHandler->getConnection();
// Recupera lo school_id dalla sessione
$school_id = session('school_id');
$school = null;
$school_name = 'Nessuna scuola selezionata';
$school_logo_path = url('userarea/photoschool/yogibook_logo.png'); // Default logo
if ($school_id) {
$school = \Vanguard\Models\School::find($school_id);
if ($school) {
$school_name = $school->name;
$school_logo_path = $school->logo ? url('userarea/photoschool/' . $school->logo) : $school_logo_path;
}
}
$product_id = $_GET['product_id'] ?? 0;
// Recupera i dettagli del prodotto, verificando che appartenga alla scuola selezionata
$stmt = $pdo->prepare("
SELECT p.id, p.name AS product_name,
c.name AS class_name, c.description AS class_description, c.photo AS class_photo
FROM products p
LEFT JOIN product_class_types pct ON p.id = pct.product_id AND pct.variation_id IS NULL
LEFT JOIN class_types ct ON pct.class_type_id = ct.id
LEFT JOIN classes c ON ct.class_id = c.id
WHERE p.id = ? AND p.school_id = ?
GROUP BY p.id
");
$stmt->execute([$product_id, $school_id]);
$product = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$product) {
die("Prodotto non trovato o non appartiene alla scuola selezionata.");
}
// Recupera le variazioni del prodotto
$stmt = $pdo->prepare("
SELECT id, name, price
FROM product_variations
WHERE product_id = ? AND status = 'active'
");
$stmt->execute([$product_id]);
$variations = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Recupera le variazioni delle classi associate al prodotto
$stmt = $pdo->prepare("
SELECT ct.id, ct.level, ct.day_of_week
FROM product_class_types pct
JOIN class_types ct ON pct.class_type_id = ct.id
WHERE pct.product_id = ? AND pct.variation_id IS NULL
");
$stmt->execute([$product_id]);
$class_types = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!--favicon-->
<link rel="icon" href="assets/images/favicon-32x32.png" type="image/png" />
<?php include('cssinclude.php'); ?>
<?php include('siteinfo.php'); ?>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<style>
.page-content {
background-color: #f0f4f5;
}
.card-pastel {
background-color: rgb(149, 217, 248);
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
border: none;
}
.product-image {
width: 100%;
height: 300px;
object-fit: cover;
border-radius: 8px;
}
.btn-pastel {
background-color: rgb(148, 186, 204);
border: none;
transition: background-color 0.3s ease;
}
.btn-pastel:hover {
background-color: rgb(155, 189, 221);
}
h5,
h6 {
font-family: 'Poppins', sans-serif;
color: #333;
}
.price-display {
font-size: 1.5rem;
color: #28a745;
margin-top: 10px;
}
.form-label {
font-family: 'Poppins', sans-serif;
color: #333;
}
.form-select {
border: 1px solid #c8e6c9;
}
.swal2-pastel {
background-color: #f0f4f5;
border-radius: 10px;
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
}
.swal2-title {
font-family: 'Poppins', sans-serif;
color: #333;
}
.swal2-content {
font-family: 'Poppins', sans-serif;
color: #333;
}
</style>
</head>
<body>
<div class="wrapper">
<?php include('include/navbar.php'); ?>
<?php include('include/topbar.php'); ?>
<div class="page-wrapper">
<div class="page-content">
<div class="card card-pastel radius-10 mb-4">
<div class="card-body text-center">
<?php if ($school): ?>
<h5 class="mb-3">Dettagli Prodotto - Scuola: <?php echo htmlspecialchars($school_name); ?></h5>
<img src="<?php echo $school_logo_path; ?>" alt="School Logo" style="max-height: 100px;">
<?php else: ?>
<h5 class="mb-3">Nessuna scuola selezionata</h5>
<img src="<?php echo $school_logo_path; ?>" alt="Default Logo" style="max-height: 100px;">
<?php endif; ?>
</div>
</div>
<div class="card card-pastel radius-10">
<div class="card-header">
<div class="d-flex align-items-center">
<div>
<h6 class="mb-0"><?php echo htmlspecialchars($product['product_name']); ?></h6>
</div>
</div>
</div>
<div class="card-body">
<div class="row">
<div class="col-md-6 mb-3">
<img src="<?php echo htmlspecialchars($product['class_photo'] ?: 'default_class_image.jpg'); ?>" alt="<?php echo htmlspecialchars($product['class_name']); ?>" class="product-image">
</div>
<div class="col-md-6">
<h5><?php echo htmlspecialchars($product['class_name']); ?></h5>
<p><?php echo htmlspecialchars($product['class_description']); ?></p>
<div class="mb-3">
<label for="variation_select" class="form-label">Seleziona Variazione Prodotto:</label>
<select id="variation_select" class="form-select">
<option value="">-- Seleziona --</option>
<?php foreach ($variations as $variation): ?>
<option value="<?php echo $variation['id']; ?>" data-price="<?php echo $variation['price']; ?>">
<?php echo htmlspecialchars($variation['name']); ?> (<?php echo $variation['price']; ?>€)
</option>
<?php endforeach; ?>
</select>
</div>
<div class="mb-3">
<label for="class_type_select" class="form-label">Seleziona Variazione Classe:</label>
<select id="class_type_select" class="form-select">
<option value="">-- Seleziona --</option>
<?php foreach ($class_types as $class_type): ?>
<option value="<?php echo $class_type['id']; ?>">
<?php echo htmlspecialchars($class_type['level'] . ' (' . $class_type['day_of_week'] . ')'); ?>
</option>
<?php endforeach; ?>
</select>
</div>
<div class="price-display" id="price_display">Prezzo: -- €</div>
<button id="add_to_cart" class="btn btn-pastel mt-3" disabled>Aggiungi al Carrello</button>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="overlay toggle-icon"></div>
<a href="javaScript:;" class="back-to-top"><i class='bx bxs-up-arrow-alt'></i></a>
<?php include('include/footer.php'); ?>
</div>
<?php include('jsinclude.php'); ?>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function() {
const variationSelect = $('#variation_select');
const classTypeSelect = $('#class_type_select');
const priceDisplay = $('#price_display');
const addToCartButton = $('#add_to_cart');
function updatePriceAndButton() {
const selectedVariation = variationSelect.val();
const selectedClassType = classTypeSelect.val();
if (selectedVariation && selectedClassType) {
const price = variationSelect.find('option:selected').data('price');
priceDisplay.text(`Prezzo: ${price}€`);
addToCartButton.prop('disabled', false);
} else {
priceDisplay.text('Prezzo: -- €');
addToCartButton.prop('disabled', true);
}
}
variationSelect.on('change', updatePriceAndButton);
classTypeSelect.on('change', updatePriceAndButton);
addToCartButton.on('click', function() {
const variationId = variationSelect.val();
const classTypeId = classTypeSelect.val();
const variationText = variationSelect.find('option:selected').text();
const classText = classTypeSelect.find('option:selected').text();
// Chiamata AJAX per aggiungere al carrello
$.ajax({
url: 'add_to_cart.php',
method: 'POST',
data: {
product_id: <?php echo $product_id; ?>,
variation_id: variationId,
class_type_id: classTypeId,
quantity: 1
},
success: function(response) {
const result = JSON.parse(response);
if (result.success) {
Swal.fire({
icon: 'success',
title: 'Prodotto Aggiunto!',
html: `
<p><strong>Variazione:</strong> ${variationText}</p>
<p><strong>Classe:</strong> ${classText}</p>
`,
confirmButtonText: 'OK',
confirmButtonColor: '#94bacc',
customClass: {
popup: 'swal2-pastel',
title: 'swal2-title',
content: 'swal2-content'
}
}).then(() => {
// Ricarica il conteggio del carrello nella topbar
updateCartCount(result.cart_count);
});
} else {
Swal.fire({
icon: 'error',
title: 'Errore',
text: 'Impossibile aggiungere il prodotto al carrello.',
confirmButtonColor: '#94bacc'
});
}
},
error: function() {
Swal.fire({
icon: 'error',
title: 'Errore',
text: 'Errore di connessione.',
confirmButtonColor: '#94bacc'
});
}
});
});
// Funzione per aggiornare il conteggio del carrello nella topbar
function updateCartCount(count) {
$('.alert-count.cart-count').text(count);
}
});
</script>
</body>
</html>