<?php
// This file is included by the AdminController, which will handle the layout
// No need to set $pageTitle here as it's already set in the controller
?>

<div class="admin-card">
    <div class="flex justify-between items-center mb-6">
        <h2 class="text-xl font-semibold">Blog Posts</h2>
        <a href="/admin/blog-posts/add" class="btn-primary">
            <i class="fas fa-plus mr-2"></i>Add New Post
        </a>
    </div>
    
    <?php if (empty($posts)): ?>
    <div class="bg-dark p-6 rounded-lg text-center">
        <i class="fas fa-blog text-4xl text-gray-500 mb-4"></i>
        <h3 class="text-lg font-medium mb-2">No Blog Posts Yet</h3>
        <p class="text-gray-400 mb-4">Start creating content for your blog.</p>
        <a href="/admin/blog-posts/add" class="btn-primary">
            <i class="fas fa-plus mr-2"></i>Create Your First Post
        </a>
    </div>
    <?php else: ?>
    <div class="overflow-x-auto">
        <table class="w-full">
            <thead>
                <tr>
                    <th class="text-left py-3 px-4 border-b border-gray-800">Title</th>
                    <th class="text-left py-3 px-4 border-b border-gray-800">Created</th>
                    <th class="text-left py-3 px-4 border-b border-gray-800">Tags</th>
                    <th class="text-right py-3 px-4 border-b border-gray-800">Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($posts as $post): ?>
                <tr class="hover:bg-dark">
                    <td class="py-3 px-4 border-b border-gray-800">
                        <div class="flex items-center">
                            <?php if (!empty($post['featured_image'])): ?>
                            <div class="w-12 h-12 mr-3 rounded overflow-hidden flex-shrink-0">
                                <img src="<?= htmlspecialchars($post['featured_image']) ?>" alt="<?= htmlspecialchars($post['title']) ?>" class="w-full h-full object-cover">
                            </div>
                            <?php endif; ?>
                            <div>
                                <div class="font-medium"><?= htmlspecialchars($post['title']) ?></div>
                                <div class="text-sm text-gray-400">
                                    <a href="/blog/<?= htmlspecialchars($post['slug']) ?>" target="_blank" class="hover:text-primary">
                                        <i class="fas fa-external-link-alt mr-1"></i>View
                                    </a>
                                </div>
                            </div>
                        </div>
                    </td>
                    <td class="py-3 px-4 border-b border-gray-800 text-gray-400">
                        <?= date('M j, Y', strtotime($post['created_at'])) ?>
                    </td>
                    <td class="py-3 px-4 border-b border-gray-800">
                        <?php if (!empty($post['tags'])): ?>
                            <?php foreach (explode(',', $post['tags']) as $tag): ?>
                                <span class="inline-block bg-dark px-2 py-1 rounded text-xs mr-1 mb-1"><?= htmlspecialchars(trim($tag)) ?></span>
                            <?php endforeach; ?>
                        <?php else: ?>
                            <span class="text-gray-500">No tags</span>
                        <?php endif; ?>
                    </td>
                    <td class="py-3 px-4 border-b border-gray-800 text-right">
                        <div class="flex justify-end space-x-2">
                            <a href="/admin/blog-posts/edit?id=<?= $post['id'] ?>" class="btn-icon" title="Edit">
                                <i class="fas fa-edit"></i>
                            </a>
                            <form method="POST" action="/admin/blog-posts/delete" class="inline" onsubmit="return confirm('Are you sure you want to delete this post?');">
                                <input type="hidden" name="id" value="<?= $post['id'] ?>">
                                <button type="submit" class="btn-icon text-red-500" title="Delete">
                                    <i class="fas fa-trash-alt"></i>
                                </button>
                            </form>
                        </div>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <?php endif; ?>
</div> 