<?php
// delete_event.php
// Autors: Kārlis Mālnieks
// pievieno datubazes savienojumu
require_once 'db_connection.php';

// parbauda vai ir noradits notikuma ID
if (!isset($_GET['id'])) {
    echo json_encode(['success' => false, 'message' => 'Nepieciesams notikuma ID.']);
    exit;
}

$eventId = (int)$_GET['id'];

try {
    // iegust notikuma datumu pec ID
    $stmt = $pdo->prepare("SELECT event_date FROM events WHERE id = :id");
    $stmt->bindParam(':id', $eventId, PDO::PARAM_INT);
    $stmt->execute();
    
    $event = $stmt->fetch(PDO::FETCH_ASSOC);
    
    if ($event) {
        // saglaba notikuma datumu
        $eventDate = $event['event_date'];
        
        // dzes notikumu pec ID
        $stmt = $pdo->prepare("DELETE FROM events WHERE id = :id");
        $stmt->bindParam(':id', $eventId, PDO::PARAM_INT);
        
        if ($stmt->execute()) {
            echo json_encode(['success' => true, 'message' => 'Notikums veiksmigi izdzests.', 'eventDate' => $eventDate]);
        } else {
            echo json_encode(['success' => false, 'message' => 'Neizdevas izdzest notikumu.']);
        }
    } else {
        echo json_encode(['success' => false, 'message' => 'Notikums nav atrasts.']);
    }
} catch (PDOException $e) {
    // atgriez kludas pazinojumu ja radusies datubazes kluda
    echo json_encode(['success' => false, 'message' => 'Datubazes kluda: ' . $e->getMessage()]);
}
?>
