<?php
session_start();
require_once 'includes/database.php';

header('Content-Type: application/json');

if(!isset($_SESSION['username'])) {
    echo json_encode(['success' => false, 'message' => 'Unauthorized']);
    exit();
}

if(!isset($_POST['entry_id'])) {
    echo json_encode(['success' => false, 'message' => 'Entry ID is required']);
    exit();
}

$conn = new Database();

try {
    $sql = "DELETE FROM timetable_template_details WHERE id = :entry_id";
    $conn->query($sql);
    $conn->bind(':entry_id', $_POST['entry_id']);

    if($conn->execute()) {
        echo json_encode([
            'success' => true,
            'message' => 'Entry deleted successfully'
        ]);
    } else {
        echo json_encode([
            'success' => false,
            'message' => 'Failed to delete entry'
        ]);
    }

} catch(Exception $e) {
    echo json_encode([
        'success' => false,
        'message' => 'Error: ' . $e->getMessage()
    ]);
}