235 lines
9.1 KiB
PHP

<?php
session_start();
include('include/headscript.php');
if (!isset($_SESSION['iduserlogin'])) {
header('Location: login.php');
exit;
}
$iduserlogin = $_SESSION['iduserlogin'];
$dbHandler = DBHandlerSelect::getInstance();
$pdo = $dbHandler->getConnection();
// === DATI UTENTE ===
$stmt = $pdo->prepare("SELECT first_name, last_name, avatar FROM auth_users WHERE id = ?");
$stmt->execute([$iduserlogin]);
$user = $stmt->fetch();
$avatar = $user['avatar'] ? '../upload/users/' . $user['avatar'] : '../assets/images/default-avatar.png';
$first_name = htmlspecialchars($user['first_name'] ?? '');
// === PROCESSA SELEZIONE SCUOLA (POST) ===
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['school_id'])) {
$school_id = (int)$_POST['school_id'];
$stmt = $pdo->prepare("SELECT id, name, logo FROM schools WHERE id = ? AND status = 'active'");
$stmt->execute([$school_id]);
$school = $stmt->fetch();
if ($school) {
// Iscrivi automaticamente se non era già iscritto
$stmtCheck = $pdo->prepare("SELECT 1 FROM user_schools WHERE user_id = ? AND school_id = ?");
$stmtCheck->execute([$iduserlogin, $school_id]);
if (!$stmtCheck->fetch()) {
$pdo->prepare("INSERT INTO user_schools (user_id, school_id, status) VALUES (?, ?, 'active')")
->execute([$iduserlogin, $school_id]);
}
// Imposta sessione
$_SESSION['school_id'] = $school['id'];
$_SESSION['school_name'] = $school['name'];
// Reindirizza alla dashboard finale
header('Location: user_dashboard.php');
exit;
}
}
// === RECUPERA SCUOLE DELL'UTENTE ===
$stmt = $pdo->prepare("
SELECT s.id, s.name, s.slug, s.logo, s.address_city
FROM user_schools us
JOIN schools s ON us.school_id = s.id
WHERE us.user_id = ? AND us.status = 'active' AND s.status = 'active'
ORDER BY s.name
");
$stmt->execute([$iduserlogin]);
$userSchools = $stmt->fetchAll();
// Caso 1: ha esattamente 1 scuola → vai diretto
if (count($userSchools) === 1) {
$school = $userSchools[0];
$_SESSION['school_id'] = $school['id'];
$_SESSION['school_name'] = $school['name'];
header('Location: user_dashboard.php');
exit;
}
// Caso 2: ha più scuole → mostra selezione
// Caso 3: nessuna scuola → mostra tutte le scuole pubbliche
if (empty($userSchools)) {
$stmt = $pdo->prepare("
SELECT id, name, slug, logo, address_city
FROM schools
WHERE status = 'active'
ORDER BY name
");
$stmt->execute();
$schools = $stmt->fetchAll();
$title = "Benvenuto! Scegli la tua scuola di yoga";
$subtitle = "Seleziona la scuola dove vuoi prenotare le lezioni";
} else {
$schools = $userSchools;
$title = "Ciao $first_name!";
$subtitle = "Seleziona la scuola in cui vuoi entrare oggi";
}
?>
<!doctype html>
<html lang="it">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Scegli la scuola - Yogiboook</title>
<?php include('cssinclude.php'); ?>
<?php include('siteinfo.php'); ?>
<style>
:root {
--pastel-blue: #94bacc;
--pastel-green: #a3d9b1;
--pastel-pink: #f8bbd0;
--pastel-yellow: #fff8c4;
}
.hero-header {
background: linear-gradient(135deg, #94bacc, #a3d9b1);
color: white;
border-radius: 20px;
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15);
}
.school-card {
transition: all 0.3s ease;
border: 2px solid transparent;
border-radius: 20px;
overflow: hidden;
cursor: pointer;
}
.school-card:hover {
transform: translateY(-10px);
box-shadow: 0 20px 40px rgba(0, 0, 0, 0.15);
border-color: var(--pastel-blue);
}
.school-logo {
height: 120px;
object-fit: contain;
background: #f8f9fa;
padding: 15px;
}
.btn-select {
background: linear-gradient(135deg, var(--pastel-blue), var(--pastel-green));
border: none;
color: white;
font-weight: bold;
border-radius: 15px;
padding: 12px;
}
.btn-select:hover {
transform: scale(1.05);
color: white;
}
</style>
</head>
<body>
<div class="wrapper">
<?php include('include/navbar.php'); ?>
<?php include('include/topbar.php'); ?>
<div class="page-wrapper">
<div class="page-content" style="background: linear-gradient(to bottom, #f0f8ff, #f8f9fa); min-height: 100vh;">
<div class="container-fluid px-4 pt-5">
<!-- Header Benvenuto -->
<div class="card hero-header mb-5 shadow-lg">
<div class="card-body text-center py-5">
<h1 class="display-5 fw-bold mb-3"><?php echo $title; ?></h1>
<p class="fs-5 opacity-90"><?php echo $subtitle; ?></p>
</div>
</div>
<!-- Griglia Scuole -->
<?php if (empty($schools)): ?>
<div class="text-center py-5">
<i class="bx bx-building-house bx-lg text-muted"></i>
<h4 class="mt-3 text-muted">Nessuna scuola disponibile al momento</h4>
</div>
<?php else: ?>
<form method="POST" id="schoolForm">
<?= csrf_field() ?> <!-- se usi CSRF, altrimenti rimuovi -->
<div class="row g-4 justify-content-center">
<?php foreach ($schools as $school): ?>
<div class="col-md-6 col-lg-4">
<div class="school-card card h-100 shadow-sm" onclick="selectSchool(<?= $school['id'] ?>)">
<div class="text-center">
<?php
$logoPath = !empty($school['logo']) && file_exists("photoschool/" . $school['logo'])
? "photoschool/" . $school['logo']
: null;
?>
<?php if ($logoPath): ?>
<img src="<?= htmlspecialchars($logoPath) ?>"
class="school-logo w-100"
alt="<?= htmlspecialchars($school['name']) ?>">
<?php else: ?>
<div class="school-logo d-flex align-items-center justify-content-center">
<i class="bx bx-building-house display-4 text-muted"></i>
</div>
<?php endif; ?>
</div>
<div class="card-body text-center pb-4">
<h5 class="card-title mb-2"><?= htmlspecialchars($school['name']) ?></h5>
<?php if (!empty($school['address_city'])): ?>
<p class="text-muted small mb-0">
<i class="bx bx-map me-1"></i><?= htmlspecialchars($school['address_city']) ?>
</p>
<?php endif; ?>
</div>
<div class="card-footer bg-transparent border-0 pt-0">
<button type="submit" class="btn btn-select w-100 shadow-sm">
<i class="bx bx-check me-2"></i>Seleziona questa scuola
</button>
</div>
</div>
<input type="radio" name="school_id" value="<?= $school['id'] ?>" id="school_<?= $school['id'] ?>" class="d-none" required>
</div>
<?php endforeach; ?>
</div>
</form>
<?php endif; ?>
</div>
</div>
</div>
<?php include('include/footer.php'); ?>
</div>
<?php include('jsinclude.php'); ?>
<script>
function selectSchool(id) {
document.getElementById('school_' + id).checked = true;
document.getElementById('schoolForm').submit();
}
</script>
</body>
</html>