getConnection(); // Verifica utente loggato if (!isset($iduserlogin)) { header("Location: login.php"); exit; } // Controlla se esiste almeno un salone $stmt = $pdo->prepare("SELECT COUNT(*) FROM shops WHERE owner_id = ?"); $stmt->execute([$iduserlogin]); if ((int)$stmt->fetchColumn() === 0) { header("Location: onboarding_salon.php"); exit; } // Prendi il primo salone $stmt = $pdo->prepare(" SELECT id, name FROM shops WHERE owner_id = ? ORDER BY created_at ASC LIMIT 1 "); $stmt->execute([$iduserlogin]); $shop = $stmt->fetch(PDO::FETCH_ASSOC); if (!$shop) { die("Errore: salone non trovato."); } $shop_id = (int)$shop['id']; $shop_name = $shop['name']; // Helpers flash function setFlash(string $type, string $text): void { $_SESSION['flash'] = ['type' => $type, 'text' => $text]; } function getFlash(): ?array { if (!isset($_SESSION['flash'])) return null; $f = $_SESSION['flash']; unset($_SESSION['flash']); return $f; } // POST - Salva impostazioni if ($_SERVER['REQUEST_METHOD'] === 'POST') { try { $show_prices_online = isset($_POST['show_prices_online']) ? 1 : 0; $appointment_slot_interval = (int)($_POST['appointment_slot_interval'] ?? 30); $allow_same_time_multiple = isset($_POST['allow_same_time_multiple']) ? 1 : 0; $min_booking_notice_hours = (int)($_POST['min_booking_notice_hours'] ?? 2); $max_booking_days_ahead = (int)($_POST['max_booking_days_ahead'] ?? 90); $require_appointment_confirmation = isset($_POST['require_appointment_confirmation']) ? 1 : 0; $no_show_warning_after = (int)($_POST['no_show_warning_after'] ?? 3); $no_show_block_after = (int)($_POST['no_show_block_after'] ?? 5); // Validazione restrict_start_minutes $restrict_start_minutes = trim($_POST['restrict_start_minutes'] ?? '00,30'); $valid_options = ['any', '00', '00,30', '00,15,30,45']; if (!in_array($restrict_start_minutes, $valid_options)) { $restrict_start_minutes = '00,30'; } // Validazione intervallo slot if ($appointment_slot_interval < 5 || $appointment_slot_interval > 120) { $appointment_slot_interval = 30; } // Controlla esistenza $stmt = $pdo->prepare("SELECT id FROM shop_settings WHERE shop_id = ?"); $stmt->execute([$shop_id]); $exists = $stmt->fetchColumn() !== false; if ($exists) { $stmt = $pdo->prepare(" UPDATE shop_settings SET show_prices_online = ?, appointment_slot_interval = ?, allow_same_time_multiple = ?, min_booking_notice_hours = ?, max_booking_days_ahead = ?, allowed_start_minutes = ?, require_appointment_confirmation = ?, no_show_warning_after = ?, no_show_block_after = ?, updated_at = NOW() WHERE shop_id = ? "); $ok = $stmt->execute([ $show_prices_online, $appointment_slot_interval, $allow_same_time_multiple, $min_booking_notice_hours, $max_booking_days_ahead, $restrict_start_minutes, $require_appointment_confirmation, $no_show_warning_after, $no_show_block_after, $shop_id ]); } else { $stmt = $pdo->prepare(" INSERT INTO shop_settings ( shop_id, show_prices_online, appointment_slot_interval, allow_same_time_multiple, min_booking_notice_hours, max_booking_days_ahead, allowed_start_minutes, require_appointment_confirmation, no_show_warning_after, no_show_block_after ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) "); $ok = $stmt->execute([ $shop_id, $show_prices_online, $appointment_slot_interval, $allow_same_time_multiple, $min_booking_notice_hours, $max_booking_days_ahead, $restrict_start_minutes, $require_appointment_confirmation, $no_show_warning_after, $no_show_block_after ]); } setFlash($ok ? 'success' : 'danger', $ok ? "Impostazioni salvate con successo!" : "Errore durante il salvataggio."); header("Location: salon_settings.php"); exit; } catch (Throwable $e) { setFlash('danger', "Errore: " . $e->getMessage()); header("Location: salon_settings.php"); exit; } } // Fetch impostazioni $stmt = $pdo->prepare("SELECT * FROM shop_settings WHERE shop_id = ?"); $stmt->execute([$shop_id]); $settings = $stmt->fetch(PDO::FETCH_ASSOC) ?: [ 'show_prices_online' => 1, 'appointment_slot_interval' => 30, 'allow_same_time_multiple' => 0, 'min_booking_notice_hours' => 2, 'max_booking_days_ahead' => 90, 'allowed_start_minutes' => '00,30', 'require_appointment_confirmation' => 1, 'no_show_warning_after' => 3, 'no_show_block_after' => 5 ]; $flash = getFlash(); ?>