prepare( "SELECT cp.id, cp.sessions_remaining, sp.type, sp.client_limit FROM client_packages cp JOIN service_packages sp ON cp.package_id = sp.id WHERE cp.client_id = ? AND sp.coach_id = ? AND cp.sessions_remaining > 0 ORDER BY cp.purchase_date ASC LIMIT 1" ); $pkg_stmt->execute([$client_id, $coach_id]); $active_package = $pkg_stmt->fetch(); if ($active_package) { // Use a session from the package try { $pdo->beginTransaction(); $is_recurring = isset($_POST['recurring']) && $_POST['recurring'] === 'on'; $recurrences = $is_recurring ? (int)$_POST['recurrences'] : 1; $frequency = $is_recurring ? $_POST['frequency'] : 'weekly'; // Default to weekly if ($active_package['sessions_remaining'] < $recurrences) { throw new Exception('Not enough sessions remaining in the package.'); } $current_booking_time = new DateTime($booking_time); for ($i = 0; $i < $recurrences; $i++) { if ($active_package['type'] === 'group') { $count_stmt = $pdo->prepare("SELECT COUNT(*) FROM bookings WHERE coach_id = ? AND booking_time = ? AND status IN ('pending', 'confirmed')"); $count_stmt->execute([$coach_id, $current_booking_time->format('Y-m-d H:i:s')]); $current_bookings = $count_stmt->fetchColumn(); if ($current_bookings >= $active_package['client_limit']) { throw new Exception('This group session is already full.'); } } // 1. Create the booking $book_stmt = $pdo->prepare( "INSERT INTO bookings (coach_id, client_id, booking_time, status, payment_status) VALUES (?, ?, ?, 'pending', 'paid_with_package')" ); $book_stmt->execute([$coach_id, $client_id, $current_booking_time->format('Y-m-d H:i:s')]); // 2. Decrement remaining sessions $update_pkg_stmt = $pdo->prepare("UPDATE client_packages SET sessions_remaining = sessions_remaining - 1 WHERE id = ?"); $update_pkg_stmt->execute([$active_package['id']]); // Calculate next booking time if ($i < $recurrences - 1) { if ($frequency === 'weekly') { $current_booking_time->modify('+1 week'); } elseif ($frequency === 'bi-weekly') { $current_booking_time->modify('+2 weeks'); } } } $pdo->commit(); // Send email notification to coach require_once __DIR__ . '/mail/MailService.php'; $coach_stmt = $pdo->prepare("SELECT email FROM coaches WHERE id = ?"); $coach_stmt->execute([$coach_id]); $coach = $coach_stmt->fetch(); $client_stmt = $pdo->prepare("SELECT name FROM clients WHERE id = ?"); $client_stmt->execute([$client_id]); $client = $client_stmt->fetch(); if ($coach && $client) { $to = $coach['email']; $subject = 'New Pending Booking'; $message = "You have a new booking from {$client['name']} for {$booking_time}. Please log in to your dashboard to approve or decline it."; MailService::sendMail($to, $subject, $message, $message); } header('Location: dashboard.php?booking=pending'); exit; } catch (Exception $e) { $pdo->rollBack(); error_log('Package booking failed: ' . $e->getMessage()); header('Location: profile.php?id=' . $coach_id . '&error=booking_failed'); exit; } } else { // No active package, redirect to coach's profile to purchase one header('Location: profile.php?id=' . $coach_id . '&error=no_package'); exit; } } else { header('Location: coaches.php'); exit; }