ini_set('display_errors', '1'); ini_set('display_startup_errors', '1'); error_reporting(E_ALL); require_once __DIR__ . '/config.php'; $error = ''; $message = ''; /* ========================================================= SERVICII EVENIMENT ========================================================= */ $serviceAction = $_POST['service_action'] ?? ''; if ($serviceAction === 'add_service') { $serviceId = (int)($_POST['service_id'] ?? 0); $quantity = (int)($_POST['quantity'] ?? 1); $serviceNotes = trim($_POST['service_notes'] ?? ''); if ($quantity < 1) { $quantity = 1; } if ($serviceId <= 0) { $error = 'Selectează un serviciu.'; } else { try { /* * Verificăm că serviciul aparține aceleiași firme * și este activ. */ $stmtService = $pdo->prepare(" SELECT id, name, price FROM services WHERE id = ? AND company_id = ? AND active = 1 LIMIT 1 "); $stmtService->execute([ $serviceId, $companyId ]); $service = $stmtService->fetch(PDO::FETCH_ASSOC); if (!$service) { $error = 'Serviciul selectat nu este disponibil.'; } else { /* * Prețul este copiat în event_services. * Astfel, dacă prețul serviciului se schimbă * ulterior, evenimentul păstrează prețul vechi. */ $stmtAdd = $pdo->prepare(" INSERT INTO event_services ( event_id, service_id, quantity, price, notes ) VALUES (?, ?, ?, ?, ?) "); $stmtAdd->execute([ $id, $serviceId, $quantity, $service['price'], $serviceNotes !== '' ? $serviceNotes : null ]); $message = 'Serviciul a fost adăugat la eveniment.'; } } catch (PDOException $e) { $error = 'Eroare la adăugarea serviciului: ' . $e->getMessage(); } } } /* ========================================================= ȘTERGERE SERVICIU DIN EVENIMENT ========================================================= */ if ($serviceAction === 'delete_service') { $eventServiceId = (int)($_POST['event_service_id'] ?? 0); if ($eventServiceId > 0) { try { /* * Ștergem doar dacă înregistrarea aparține * evenimentului curent. */ $stmtDelete = $pdo->prepare(" DELETE FROM event_services WHERE id = ? AND event_id = ? "); $stmtDelete->execute([ $eventServiceId, $id ]); $message = 'Serviciul a fost eliminat din eveniment.'; } catch (PDOException $e) { $error = 'Eroare la eliminarea serviciului: ' . $e->getMessage(); } } } /* ========================================================= SERVICII DISPONIBILE ========================================================= */ $stmtServices = $pdo->prepare(" SELECT id, name, duration_hours, price FROM services WHERE company_id = ? AND active = 1 ORDER BY name ASC "); $stmtServices->execute([ $companyId ]); $availableServices = $stmtServices->fetchAll(PDO::FETCH_ASSOC); /* ========================================================= SERVICII DEJA ADAUGATE LA EVENIMENT ========================================================= */ $stmtEventServices = $pdo->prepare(" SELECT es.id, es.service_id, es.quantity, es.price, es.notes, s.name, s.duration_hours FROM event_services es INNER JOIN services s ON s.id = es.service_id WHERE es.event_id = ? ORDER BY es.id ASC "); $stmtEventServices->execute([ $id ]); $eventServices = $stmtEventServices->fetchAll(PDO::FETCH_ASSOC); /* ========================================================= TOTAL SERVICII ========================================================= */ $servicesTotal = 0; foreach ($eventServices as $eventService) { $servicesTotal += (float)$eventService['price'] * (int)$eventService['quantity']; } /* ========================= VERIFICĂ ID EVENIMENT ========================= */ $id = (int)($_GET['id'] ?? $_POST['id'] ?? 0); if ($id <= 0) { die('Eveniment invalid.'); } /* ========================= SALVARE MODIFICĂRI ========================= */ if ($_SERVER['REQUEST_METHOD'] === 'POST') { $client_id = (int)($_POST['client_id'] ?? 0); $title = trim($_POST['title'] ?? ''); $event_date = $_POST['event_date'] ?? ''; $start_time = $_POST['start_time'] ?: null; $end_time = $_POST['end_time'] ?: null; $location = trim($_POST['location'] ?? ''); $status = $_POST['status'] ?? 'lead'; $total = (float)($_POST['total'] ?? 0); $deposit = (float)($_POST['deposit'] ?? 0); $notes = trim($_POST['notes'] ?? ''); if ($client_id <= 0) { $error = 'Selectează un client.'; } elseif ($title === '') { $error = 'Introdu denumirea evenimentului.'; } elseif ($event_date === '') { $error = 'Selectează data evenimentului.'; } else { try { $stmt = $pdo->prepare(" UPDATE events SET client_id = ?, title = ?, event_date = ?, start_time = ?, end_time = ?, location = ?, status = ?, total = ?, deposit = ?, notes = ? WHERE id = ? "); $stmt->execute([ $client_id, $title, $event_date, $start_time, $end_time, $location ?: null, $status, $total, $deposit, $notes ?: null, $id ]); $message = 'Evenimentul a fost actualizat cu succes!'; } catch (PDOException $e) { $error = 'Eroare la actualizare: ' . $e->getMessage(); } } } /* ========================= ÎNCARCĂ EVENIMENTUL ========================= */ try { $stmt = $pdo->prepare(" SELECT * FROM events WHERE id = ? LIMIT 1 "); $stmt->execute([$id]); $event = $stmt->fetch(PDO::FETCH_ASSOC); if (!$event) { die('Evenimentul nu există.'); } } catch (PDOException $e) { die('Eroare la încărcarea evenimentului: ' . $e->getMessage()); } /* ========================= CLIENȚI ========================= */ try { $clientsStmt = $pdo->query(" SELECT id, name FROM clients ORDER BY name ASC "); $clients = $clientsStmt->fetchAll(PDO::FETCH_ASSOC); } catch (PDOException $e) { die('Eroare la încărcarea clienților: ' . $e->getMessage()); } ?>