<?php
session_start();
if (!isset($_SESSION['admin_logged_in'])) {
    header("Location: login.php");
    exit;
}
include '../includes/db.php';

if (isset($_GET['id'])) {
    $id = $_GET['id'];
    
    // Optional: Delete image file from server
    // $stmt = $conn->prepare("SELECT image_url FROM blogs WHERE id = ?");
    // $stmt->bind_param("i", $id);
    // $stmt->execute();
    // $res = $stmt->get_result();
    // if($row = $res->fetch_assoc()) {
    //     if(file_exists("../" . $row['image_url'])) {
    //         unlink("../" . $row['image_url']);
    //     }
    // }
    // $stmt->close();

    $stmt = $conn->prepare("DELETE FROM blogs WHERE id = ?");
    if ($stmt === false) {
        die("Error preparing statement: " . $conn->error);
    }
    $stmt->bind_param("i", $id);
    
    if ($stmt->execute()) {
        header("Location: manage_blogs.php");
        exit;
    } else {
        echo "Error deleting record: " . $conn->error;
    }
    $stmt->close();
} else {
    header("Location: manage_blogs.php");
    exit;
}
?>
