<?php
require_once 'config/db.php';
require_once 'includes/header.php';

// Handle Delete Portfolio Item
if (isset($_GET['delete'])) {
    $id = (int)$_GET['delete'];
    try {
        // First get the image path so we can delete the file from the server
        $stmt = $pdo->prepare("SELECT image_path FROM portfolio_items WHERE id = ?");
        $stmt->execute([$id]);
        $item = $stmt->fetch();
        
        if ($item && !empty($item['image_path'])) {
            $filepath = "../" . $item['image_path'];
            if (file_exists($filepath)) {
                unlink($filepath);
            }
        }
        
        // Now delete the record
        $stmt = $pdo->prepare("DELETE FROM portfolio_items WHERE id = ?");
        $stmt->execute([$id]);
        $success = "Portfolio item deleted successfully.";
    } catch (PDOException $e) {
        $error = "Error deleting portfolio item: " . $e->getMessage();
    }
}

// Fetch all portfolio items with category and subcategory names
$query = "
    SELECT p.*, c.name as category_name, s.name as subcategory_name 
    FROM portfolio_items p
    LEFT JOIN portfolio_categories c ON p.category_id = c.id
    LEFT JOIN portfolio_subcategories s ON p.subcategory_id = s.id
    ORDER BY p.created_at DESC
";
$stmt = $pdo->query($query);
$items = $stmt ? $stmt->fetchAll() : [];
if (!$items) {
    $items = [];
}
?>

<div class="dashboard-content">
    <div class="welcome-banner" style="margin-bottom: 20px; padding: 20px; display: flex; justify-content: space-between; align-items: center;">
        <div>
            <h2><i class="fa-solid fa-images"></i> Portfolio Items</h2>
            <p>Manage your published and drafted portfolio projects.</p>
        </div>
        <div>
            <a href="portfolio_add.php" class="logout-btn" style="background: var(--white); color: var(--primary); text-decoration: none; font-weight: 600; display: inline-block;">
                <i class="fa-solid fa-plus"></i> Add New Item
            </a>
        </div>
    </div>

    <?php if (isset($success)): ?>
        <div class="badge badge-completed" style="display:block; padding:15px; margin-bottom: 20px; font-size:1rem;"><?= htmlspecialchars($success) ?></div>
    <?php endif; ?>
    <?php if (isset($error)): ?>
        <div class="badge badge-pending" style="display:block; padding:15px; margin-bottom: 20px; background-color: #fee2e2; color: #ef4444; font-size:1rem;"><?= htmlspecialchars($error) ?></div>
    <?php endif; ?>

    <div class="recent-activity">
        <div class="table-responsive">
            <table class="admin-table">
                <thead>
                    <tr>
                        <th>Image</th>
                        <th>Title</th>
                        <th>Category</th>
                        <th>Subcategory</th>
                        <th>Status</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (count($items) > 0): ?>
                        <?php foreach ($items as $item): ?>
                            <tr>
                                <td>
                                    <?php if (!empty($item['image_path'])): ?>
                                        <img src="../<?= htmlspecialchars($item['image_path']) ?>" alt="Thumbnail" style="width: 60px; height: 60px; object-fit: cover; border-radius: 6px;">
                                    <?php else: ?>
                                        <div style="width: 60px; height: 60px; background: #e2e8f0; border-radius: 6px; display: flex; align-items: center; justify-content: center; color: #94a3b8;"><i class="fa-solid fa-image"></i></div>
                                    <?php endif; ?>
                                </td>
                                <td style="font-weight:600;">
                                    <?= htmlspecialchars($item['title']) ?><br>
                                    <small style="color:#94a3b8; font-weight:normal;"><?= htmlspecialchars($item['client_name'] ?? 'No Client') ?></small>
                                </td>
                                <td><?= htmlspecialchars($item['category_name']) ?></td>
                                <td style="color:#64748b;"><?= htmlspecialchars($item['subcategory_name'] ?? 'None') ?></td>
                                <td>
                                    <?php if($item['status'] == 'published'): ?>
                                        <span class="badge badge-completed">Published</span>
                                    <?php else: ?>
                                        <span class="badge badge-pending" style="background:rgba(148, 163, 184, 0.2); color:#64748b;">Draft</span>
                                    <?php endif; ?>
                                </td>
                                <td>
                                    <a href="portfolio_edit.php?id=<?= $item['id'] ?>" style="color:var(--primary); margin-right:10px;"><i class="fa-solid fa-pen-to-square"></i></a>
                                    <a href="portfolio_items.php?delete=<?= $item['id'] ?>" onclick="return confirm('Are you sure you want to delete this portfolio item? This action will permanently remove the associated image file as well.');" style="color:var(--danger);"><i class="fa-solid fa-trash"></i></a>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php else: ?>
                        <tr>
                            <td colspan="6" style="text-align: center; color: #94a3b8; padding: 40px 20px;">
                                <i class="fa-solid fa-folder-open" style="font-size: 2rem; margin-bottom: 15px; color: #cbd5e1; display: block;"></i>
                                No portfolio items found. Click "Add New Item" to create one.
                            </td>
                        </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

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