<?php
// This block of PHP code, which handles the delete action and a subsequent redirect,
// must be executed before any HTML is sent to the browser.
session_start();
include_once("../config/db.php");

// Security: Ensure user is an admin before performing any action.
if(!isset($_SESSION['admin'])){
    header("Location: login.php");
    exit();
}

// Handle delete request
if(isset($_GET['delete'])){
    $id = $_GET['delete'];

    // First, get the image filename to delete the file from the server
    $select_stmt = mysqli_prepare($conn, "SELECT image FROM blog WHERE id=?");
    mysqli_stmt_bind_param($select_stmt, "i", $id);
    mysqli_stmt_execute($select_stmt);
    $result = mysqli_stmt_get_result($select_stmt);
    if($row = mysqli_fetch_assoc($result)){
        $image_path = "uploads/" . $row['image'];
        if(file_exists($image_path)){
            unlink($image_path); // Delete the file
        }
    }

    // Now delete the record from the database
    $stmt = mysqli_prepare($conn, "DELETE FROM blog WHERE id=?");
    mysqli_stmt_bind_param($stmt, "i", $id);
    mysqli_stmt_execute($stmt);
    header("Location: manage_blog.php"); // Redirect to avoid re-deleting on refresh
    exit();
}

include 'admin_header.php';

$result = mysqli_query($conn,"SELECT * FROM blog ORDER BY id DESC");
?>
<div class="page-header">
    <h1>Manage Blog</h1>
    <a href="add_blog.php" class="btn">Add New Post</a>
</div>

<div class="content-wrapper">
    <?php if(mysqli_num_rows($result) > 0): ?>
        <table>
            <thead>
                <tr>
                    <th style="width: 150px;">Image</th>
                    <th>Title</th>
                    <th>Category</th>
                    <th style="width: 200px; text-align: center;">Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php while($row = mysqli_fetch_assoc($result)): ?>
                    <tr>
                        <td><img src="uploads/<?php echo htmlspecialchars($row['image']); ?>" alt="<?php echo htmlspecialchars($row['title']); ?>"></td>
                        <td><?php echo htmlspecialchars($row['title']); ?></td>
                        <td><?php echo htmlspecialchars($row['category']); ?></td>
                        <td style="text-align: center;">
                            <a href="edit_blog.php?id=<?php echo $row['id']; ?>" class="edit" style="margin-right: 15px; text-decoration: none;">
                                <i class="fas fa-edit"></i> Edit
                            </a>
                            <a href="?delete=<?php echo $row['id']; ?>" class="delete" onclick="return confirm('Are you sure you want to delete this post?');" style="text-decoration: none;">
                                <i class="fas fa-trash"></i> Delete
                            </a>
                        </td>
                    </tr>
                <?php endwhile; ?>
            </tbody>
        </table>
    <?php else: ?>
        <div style="text-align: center; color: var(--text-secondary); padding: 2rem 0;">
            <i class="fas fa-blog" style="font-size: 48px; margin-bottom: 15px; opacity: 0.3;"></i>
            <p>No blog posts found. <a href="add_blog.php">Add one now</a>.</p>
        </div>
    <?php endif; ?>
</div>
<?php include 'admin_footer.php'; ?>