getConnection();
// Verifica utente loggato
if (!isset($iduserlogin)) {
die("Errore: ID utente non definito.");
}
// =========================================================================
// Controllo se l'utente ha almeno un salone
// =========================================================================
$stmt = $pdo->prepare("SELECT COUNT(*) FROM shops WHERE owner_id = ?");
$stmt->execute([$iduserlogin]);
$hasShop = $stmt->fetchColumn() > 0;
if (!$hasShop) {
// Nessun salone → vai alla creazione
header("Location: onboarding_salon.php");
exit;
}
// =========================================================================
// Carichiamo il salone (prendiamo il primo creato o quello più recente)
// =========================================================================
$stmt = $pdo->prepare("
SELECT
id, name, slug, address, city, province, zip_code,
phone, email, instagram, logo, description, active
FROM shops
WHERE owner_id = ?
ORDER BY created_at ASC
LIMIT 1
");
$stmt->execute([$iduserlogin]);
$shop = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$shop) {
// Dovrebbe essere impossibile, ma per sicurezza
die("Errore: salone non trovato nonostante il conteggio dicesse di sì.");
}
$shop_id = $shop['id'];
$shop_name = $shop['name'];
// Servizi attivi
$stmt = $pdo->prepare("
SELECT id, name, duration_minutes, price, category, color_hex
FROM services
WHERE shop_id = ? AND is_active = 1
ORDER BY category, `order`, name
");
$stmt->execute([$shop_id]);
$services = $stmt->fetchAll();
// Staff attivo e visibile online
$stmt = $pdo->prepare("
SELECT s.id, s.first_name, s.last_name, s.nickname, s.color_hex,
u.avatar
FROM staff s
LEFT JOIN auth_users u ON s.user_id = u.id
WHERE s.shop_id = ? AND s.is_active = 1 AND s.can_book_online = 1
ORDER BY s.first_name, s.last_name
");
$stmt->execute([$shop_id]);
$staff_members = $stmt->fetchAll();
// Intervallo appuntamenti (default: oggi)
$start_date = $_GET['start_date'] ?? date('Y-m-d');
$end_date = $_GET['end_date'] ?? $start_date;
// Appuntamenti
$stmt = $pdo->prepare("
SELECT
a.id,
a.start_at,
a.end_at,
a.status,
a.notes,
c.first_name AS customer_first,
c.last_name AS customer_last,
c.phone AS customer_phone,
s.name AS service_name,
s.color_hex AS service_color,
st.first_name AS staff_first,
st.last_name AS staff_last,
st.color_hex AS staff_color
FROM appointments a
LEFT JOIN customers c ON a.customer_id = c.id
LEFT JOIN services s ON a.service_id = s.id
LEFT JOIN staff st ON a.staff_id = st.id
WHERE a.shop_id = ?
AND DATE(a.start_at) BETWEEN ? AND ?
ORDER BY a.start_at
");
$stmt->execute([$shop_id, $start_date, $end_date]);
$appointments = $stmt->fetchAll();
?>