yogiboook_new/public/userarea/user_dashboard.php

302 lines
15 KiB
PHP

<?php
include('include/headscript.php');
if (!isset($iduserlogin)) {
header('Location: login.php');
exit;
}
$dbHandler = DBHandlerSelect::getInstance();
$pdo = $dbHandler->getConnection();
// === DATI UTENTE ===
$stmt = $pdo->prepare("SELECT first_name, last_name, email, avatar FROM auth_users WHERE id = ?");
$stmt->execute([$iduserlogin]);
$user = $stmt->fetch();
$avatar = $user['avatar'] ? '../upload/users/' . $user['avatar'] : '../assets/images/default-avatar.png';
// === SCUOLA CORRENTE ===
$school_id = session('school_id');
$school_name = 'Nessuna scuola selezionata';
$school_logo_path = null; // niente default
if ($school_id) {
$stmt = $pdo->prepare("SELECT name, logo FROM schools WHERE id = ?");
$stmt->execute([$school_id]);
$school = $stmt->fetch();
if ($school) {
$school_name = $school['name'];
if (!empty($school['logo']) && file_exists("photoschool/" . $school['logo'])) {
$school_logo_path = "photoschool/" . $school['logo'];
}
}
}
// === ORDINI UTENTE ===
$stmt = $pdo->prepare("
SELECT o.*, p.name AS product_name, pv.name AS variation_name,
c.name AS class_name, ct.level, ct.day_of_week
FROM orders o
JOIN products p ON o.product_id = p.id
LEFT JOIN product_variations pv ON o.variation_id = pv.id
LEFT JOIN classes c ON o.class_id = c.id
LEFT JOIN class_types ct ON o.class_type_id = ct.id
WHERE o.user_id = ? AND o.school_id = ?
ORDER BY o.created_at DESC
");
$stmt->execute([$iduserlogin, $school_id]);
$orders = $stmt->fetchAll();
// === STATISTICHE RAPIDE ===
$total_spent = array_sum(array_column($orders, 'price'));
$total_entries = array_sum(array_column($orders, 'total_entries'));
$available_entries = array_sum(array_column($orders, 'available_entries'));
$active_orders = count(array_filter($orders, fn($o) => $o['status'] === 'completed' && (!$o['expiration_date'] || strtotime($o['expiration_date']) >= time())));
?>
<!doctype html>
<html lang="it">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>La mia Dashboard - Yogiboook</title>
<?php include('cssinclude.php'); ?>
<?php include('siteinfo.php'); ?>
<style>
:root {
--pastel-blue: #94bacc;
--pastel-green: #a3d9b1;
--pastel-pink: #f8bbd0;
--pastel-yellow: #fff8c4;
}
.card-pastel {
background: linear-gradient(135deg, #94bacc, #a3d9b1);
color: white;
}
.card-soft {
background: #ffffff;
border: 1px solid #e0e0e0;
}
.stat-card {
background: white;
border-radius: 15px;
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.08);
transition: all 0.3s ease;
}
.stat-card:hover {
transform: translateY(-5px);
box-shadow: 0 10px 25px rgba(0, 0, 0, 0.15);
}
.avatar-circle {
width: 100px;
height: 100px;
object-fit: cover;
border: 4px solid white;
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
}
.table-pastel thead {
background-color: var(--pastel-blue);
color: white;
}
.badge-soft {
padding: 0.4em 0.8em;
border-radius: 50px;
font-size: 0.85rem;
}
</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;">
<!-- SCUOLA + BENVENUTO -->
<div class="container-fluid px-4 pt-4">
<div class="card card-pastel radius-20 shadow-lg mb-4 position-relative">
<div class="card-body text-center py-5">
<!-- Logo solo se esiste -->
<?php if ($school_logo_path): ?>
<img src="<?php echo htmlspecialchars($school_logo_path); ?>"
alt="Logo <?php echo htmlspecialchars($school_name); ?>"
class="mb-4 rounded-3 shadow"
style="height: 90px; object-fit: contain;">
<?php endif; ?>
<!-- Saluto -->
<h2 class="mb-2 text-white">
Ciao<?php echo $user['first_name'] ? ', ' . htmlspecialchars($user['first_name']) : ''; ?>!
</h2>
<p class="mb-0 fs-4 text-white opacity-90">
Sei nella scuola: <strong><?php echo htmlspecialchars($school_name); ?></strong>
</p>
<!-- PULSANTI IN RIGA (uno accanto all'altro) -->
<div class="position-absolute top-0 end-0 mt-3 me-3 d-flex gap-2">
<a href="my_lessons.php" class="btn btn-light btn-lg shadow-sm px-4">
<i class="bx bx-calendar-heart me-2"></i>Le mie lezioni
</a>
<button type="button" class="btn btn-outline-light btn-lg shadow-sm px-4"
data-bs-toggle="modal" data-bs-target="#changeSchoolModal">
<i class="bx bx-transfer me-2"></i>Cambia scuola
</button>
</div>
</div>
</div>
<!-- PROFILO + STATISTICHE -->
<div class="row g-4 mb-5">
<div class="col-lg-4">
<div class="card stat-card text-center h-100">
<div class="card-body">
<img src="<?php echo $avatar; ?>" alt="Avatar" class="rounded-circle avatar-circle mb-3">
<h5><?php echo htmlspecialchars($user['first_name'] . ' ' . $user['last_name']); ?></h5>
<p class="text-muted"><?php echo htmlspecialchars($user['email']); ?></p>
</div>
</div>
</div>
<div class="col-lg-8">
<div class="row g-4">
<div class="col-md-4">
<div class="card stat-card text-center h-100">
<div class="card-body">
<h3 class="text-primary fw-bold"><?php echo count($orders); ?></h3>
<p class="mb-0">Ordini totali</p>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card stat-card text-center h-100">
<div class="card-body">
<h3 class="text-success fw-bold">€<?php echo number_format($total_spent, 2); ?></h3>
<p class="mb-0">Speso in totale</p>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card stat-card text-center h-100">
<div class="card-body">
<h3 class="text-info fw-bold"><?php echo $available_entries; ?></h3>
<p class="mb-0">Ingressi disponibili</p>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- I TUOI ORDINI -->
<div class="card radius-20 shadow-lg">
<div class="card-header bg-primary text-white d-flex justify-content-between align-items-center">
<h4 class="mb-0">I tuoi ordini</h4>
<span class="badge bg-light text-dark"><?php echo count($orders); ?> totali</span>
</div>
<div class="card-body p-0">
<?php if (empty($orders)): ?>
<div class="text-center py-5">
<i class="bx bx-package bx-lg text-muted"></i>
<h5 class="text-muted mt-3">Non hai ancora effettuato ordini</h5>
<a href="products.php" class="btn btn-primary mt-3">Vai ai corsi</a>
</div>
<?php else: ?>
<div class="table-responsive">
<table class="table table-hover align-middle mb-0">
<thead class="table-pastel">
<tr>
<th>Data</th>
<th>Ordine</th>
<th>Prodotto</th>
<th>Ingressi</th>
<th>Scadenza</th>
<th>Prezzo</th>
<th>Stato</th>
</tr>
</thead>
<tbody>
<?php foreach ($orders as $o): ?>
<tr class="table-light">
<td><?php echo date('d/m/Y', strtotime($o['created_at'])); ?></td>
<td><strong>#<?php echo $o['order_number']; ?></strong></td>
<td>
<strong><?php echo htmlspecialchars($o['product_name']); ?></strong>
<?php if ($o['variation_name']): ?>
<br><small class="text-success"><?php echo htmlspecialchars($o['variation_name']); ?></small>
<?php endif; ?>
<?php if ($o['class_name']): ?>
<br><small class="text-info"><?php echo htmlspecialchars($o['class_name']); ?></small>
<?php endif; ?>
</td>
<td>
<?php if ($o['available_entries'] != $o['total_entries']): ?>
<span class="text-success"><?php echo $o['available_entries']; ?></span>/<?php echo $o['total_entries']; ?>
<?php else: ?>
<?php echo $o['total_entries']; ?>
<?php endif; ?>
</td>
<td>
<?php if ($o['expiration_date']): ?>
<span class="<?php echo strtotime($o['expiration_date']) < time() ? 'text-danger' : 'text-warning'; ?>">
<?php echo date('d/m/Y', strtotime($o['expiration_date'])); ?>
</span>
<?php else: ?>
<em>Nessuna</em>
<?php endif; ?>
</td>
<td class="text-success fw-bold">€<?php echo number_format($o['price'], 2); ?></td>
<td>
<span class="badge-soft <?php echo $o['status'] == 'completed' ? 'bg-success' : ($o['status'] == 'pending' ? 'bg-warning' : 'bg-secondary'); ?>">
<?php echo ucfirst($o['status']); ?>
</span>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php endif; ?>
</div>
</div>
</div>
</div>
</div>
<?php include('include/footer.php'); ?>
</div>
<?php include('jsinclude.php'); ?>
<!-- Modal Cambia Scuola -->
<div class="modal fade" id="changeSchoolModal" tabindex="-1">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header bg-primary text-white">
<h5 class="modal-title">Scegli la scuola</h5>
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body text-center py-5">
<i class="bx bx-building-house bx-lg text-muted"></i>
<h4 class="mt-3 text-muted">Le tue scuole</h4>
<p class="text-muted">Qui compariranno tutte le scuole in cui sei iscritto</p>
<div class="spinner-border text-primary mt-4" role="status">
<span class="visually-hidden">Caricamento...</span>
</div>
</div>
</div>
</div>
</div>
</body>
</html>