<?php
require_once 'admin/config.php';
require_login();

$template_name = isset($_GET['template']) ? basename($_GET['template']) : '';

if (empty($template_name)) {
    header('Location: admin.php');
    exit;
}

$template_path = 'templates/' . $template_name;

if (file_exists($template_path)) {
    if (unlink($template_path)) {
        // Optionally, you might want to delete the associated image as well.
        // This part is left commented out as it requires more logic to find the correct image.
        /*
        $image_name = pathinfo($template_name, PATHINFO_FILENAME) . '.jpg'; // or png, etc.
        $image_path = 'assets/images/templates/' . $image_name;
        if (file_exists($image_path)) {
            unlink($image_path);
        }
        */
    }
}

header('Location: admin.php');
exit;
?>