<?php
// blog_posts.php - Manage Blog Posts
// File location: /bidwheels.net/admin/blog_posts.php

require_once '../includes/auth.php';
require_once '../includes/functions.php';
require_once 'includes/admin_header.php';

requireAdmin();

// Handle delete
if (isset($_GET['delete'])) {
    $id = (int)$_GET['delete'];
    
    // Get featured image to delete
    $post = fetchOne("SELECT featured_image FROM blog_posts WHERE id = $id");
    if ($post['featured_image']) {
        $filepath = $_SERVER['DOCUMENT_ROOT'] . '/bidwheels/uploads/blog/' . $post['featured_image'];
        if (file_exists($filepath)) {
            unlink($filepath);
        }
    }
    
    delete('blog_posts', "id = $id");
    setSuccess('Blog post deleted.');
    redirect('/admin/blog_posts.php');
}

$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$offset = ($page - 1) * 20;

$posts = fetchAll("
    SELECT * FROM blog_posts 
    ORDER BY created_at DESC 
    LIMIT $offset, 20
");

$total = getCount('blog_posts');
$total_pages = ceil($total / 20);
?>

<div class="admin-page">
    <div class="page-header">
        <h1 class="page-title">Blog Posts</h1>
        <a href="add_blog_post.php" class="btn btn-primary"><i class="fas fa-plus"></i> Add Post</a>
    </div>
    
    <?php echo getMessage(); ?>
    
    <div class="table-responsive">
        <table class="admin-table">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Image</th>
                    <th>Title</th>
                    <th>Category</th>
                    <th>Tags</th>
                    <th>Views</th>
                    <th>Date</th>
                    <th>Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php if (count($posts) > 0): ?>
                    <?php foreach ($posts as $post): ?>
                        <tr>
                            <td><?php echo $post['id']; ?></td>
                            <td>
                                <?php if ($post['featured_image']): ?>
                                    <img src="<?php echo SITE_URL; ?>/uploads/blog/<?php echo $post['featured_image']; ?>" style="width: 60px; height: 40px; object-fit: cover;">
                                <?php else: ?>
                                    <span style="color: #ccc;">No image</span>
                                <?php endif; ?>
                            </td>
                            <td><?php echo htmlspecialchars($post['title']); ?></td>
                            <td><?php echo htmlspecialchars($post['category']); ?></td>
                            <td><?php echo htmlspecialchars($post['tags']); ?></td>
                            <td><?php echo $post['views']; ?></td>
                            <td><?php echo date('M d, Y', strtotime($post['created_at'])); ?></td>
                            <td class="actions">
                                <a href="edit_blog_post.php?id=<?php echo $post['id']; ?>" class="btn-icon btn-edit"><i class="fas fa-edit"></i></a>
                                <a href="?delete=<?php echo $post['id']; ?>" class="btn-icon btn-danger" onclick="return confirm('Delete this post?')"><i class="fas fa-trash"></i></a>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                <?php else: ?>
                    <tr>
                        <td colspan="8" style="text-align: center;">No blog posts yet.</td>
                    </tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
    
    <?php if ($total_pages > 1): ?>
        <div class="pagination">
            <?php for ($i = 1; $i <= $total_pages; $i++): ?>
                <a href="?page=<?php echo $i; ?>" class="<?php echo $i == $page ? 'active' : ''; ?>"><?php echo $i; ?></a>
            <?php endfor; ?>
        </div>
    <?php endif; ?>
</div>

<?php require_once 'includes/admin_footer.php'; ?>