<?php
/**
 * Delete Gallery Image
 */

session_start();
require_once __DIR__ . '/../config/database.php';
require_once __DIR__ . '/includes/auth.php';

checkAdminAuth();

$id = intval($_GET['id'] ?? 0);

if ($id > 0) {
    $pdo = getDBConnection();
    
    // Get filename
    $stmt = $pdo->prepare("SELECT filename FROM gallery_images WHERE id = :id");
    $stmt->execute([':id' => $id]);
    $image = $stmt->fetch();
    
    if ($image) {
        // Delete file
        $filepath = __DIR__ . '/../uploads/gallery/' . $image['filename'];
        if (file_exists($filepath)) {
            unlink($filepath);
        }
        
        // Delete from database
        $deleteStmt = $pdo->prepare("DELETE FROM gallery_images WHERE id = :id");
        $deleteStmt->execute([':id' => $id]);
    }
}

header('Location: gallery.php');
exit;

