<?php
require_once __DIR__ . '/../functions.php';
ensure_role(['Admin', 'Manager']);
$categories = pdo()->query('SELECT * FROM portfolio_categories ORDER BY created_at DESC')->fetchAll();
$projects = pdo()->query('SELECT pp.*, pc.name AS category_name FROM portfolio_projects pp LEFT JOIN portfolio_categories pc ON pp.category_id = pc.id ORDER BY pp.created_at DESC')->fetchAll();
$images = [];
$rows = pdo()->query('SELECT project_id,path FROM portfolio_images')->fetchAll();
foreach ($rows as $row) {
    $images[$row['project_id']][] = $row['path'];
}
?>
<?php include __DIR__ . '/includes/header.php'; ?>
    <section class="panel">
        <header>
            <h2>Portfolio Projects</h2>
            <button class="btn btn-primary" onclick="showProjectModal()">Add Project</button>
        </header>
        <div class="table-scroll">
            <table>
                <thead><tr><th>Thumbnail</th><th>Title</th><th>Category</th><th>Status</th><th>Images</th><th>Actions</th></tr></thead>
                <tbody>
                <?php foreach ($projects as $project): ?>
                    <tr data-project='<?= json_encode($project, JSON_HEX_APOS | JSON_HEX_QUOT) ?>'>
                        <td>
                            <?php $thumb = $project['featured_image'] ?: (isset($images[$project['id']][0]) ? $images[$project['id']][0] : null); ?>
                            <?php if ($thumb): ?>
                                <img src="<?= BASE_URL . UPLOAD_URL . '/' . clean($thumb) ?>" alt="Project thumbnail" class="table-thumb">
                            <?php else: ?>
                                <span class="table-thumb placeholder">No</span>
                            <?php endif; ?>
                        </td>
                        <td><?= clean($project['title']) ?></td>
                        <td><?= clean($project['category_name'] ?? 'Uncategorized') ?></td>
                        <td><?= clean($project['status']) ?></td>
                        <td><?= isset($images[$project['id']]) ? count($images[$project['id']]) : 0 ?></td>
                        <td>
                            <button class="btn btn-secondary" onclick="editProject(this)">Edit</button>
                            <button class="btn btn-danger" onclick="deleteItem('delete_project', <?= $project['id'] ?>, 'admin/portfolio.php')">Delete</button>
                        </td>
                    </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </section>
    <section class="panel">
        <header>
            <h2>Portfolio Categories</h2>
            <button class="btn btn-primary" onclick="showPortfolioCategoryModal()">Add Category</button>
        </header>
        <div class="table-scroll">
            <table>
                <thead><tr><th>Thumbnail</th><th>Name</th><th>Status</th><th>Actions</th></tr></thead>
                <tbody>
                <?php foreach ($categories as $category): ?>
                    <tr data-category='<?= json_encode($category, JSON_HEX_APOS | JSON_HEX_QUOT) ?>'>
                        <td>
                            <?php if (!empty($category['thumbnail'])): ?>
                                <img src="<?= BASE_URL . UPLOAD_URL . '/' . clean($category['thumbnail']) ?>" alt="Category thumbnail" class="table-thumb">
                            <?php else: ?>
                                <span class="table-thumb placeholder">No image</span>
                            <?php endif; ?>
                        </td>
                        <td><?= clean($category['name']) ?></td>
                        <td><?= clean($category['status'] ?? 'Published') ?></td>
                        <td>
                            <button class="btn btn-secondary" onclick="editPortfolioCategory(this)">Edit</button>
                            <button class="btn btn-danger" onclick="deleteItem('delete_portfolio_category', <?= $category['id'] ?>, 'admin/portfolio.php')">Delete</button>
                        </td>
                    </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </section>
    <div id="project-modal" class="modal hidden">
        <div class="modal-panel big-modal">
            <header>
                <h3 id="project-modal-title">Add Portfolio Project</h3>
                <button class="close" onclick="closeModal('project-modal')">×</button>
            </header>
            <form id="project-form" method="post" action="actions.php" enctype="multipart/form-data">
                <input type="hidden" name="csrf" value="<?= csrf_token() ?>">
                <input type="hidden" name="action" value="create_project">
                <input type="hidden" name="id" value="">
                <label>Title</label>
                <input type="text" name="title" required>
                <label>Description</label>
                <textarea name="description" rows="4" required></textarea>
                <label>Category</label>
                <select name="category_id">
                    <option value="">Uncategorized</option>
                    <?php foreach ($categories as $category): ?>
                        <option value="<?= $category['id'] ?>"><?= clean($category['name']) ?></option>
                    <?php endforeach; ?>
                </select>
                <label>Technologies Used</label>
                <input type="text" name="technologies">
                <label>Outcome / Results</label>
                <textarea name="outcome" rows="3"></textarea>
                <label>SEO Title</label>
                <input type="text" name="seo_title" placeholder="Optional SEO title">
                <label>SEO Description</label>
                <textarea name="seo_description" rows="3" placeholder="Optional SEO description"></textarea>
                <label>Status</label>
                <select name="status">
                    <option>Draft</option>
                    <option>Published</option>
                </select>
                <label>Featured Image</label>
                <input type="file" name="featured_image" accept="image/*">
                <label>Upload Images</label>
                <input type="file" name="images[]" accept="image/*" multiple>
                <button type="submit" class="btn btn-primary">Save Project</button>
            </form>
        </div>
    </div>

    <div id="portfolio-category-modal" class="modal hidden">
        <div class="modal-panel">
            <header>
                <h3 id="portfolio-category-modal-title">Add Portfolio Category</h3>
                <button class="close" onclick="closeModal('portfolio-category-modal')">×</button>
            </header>
            <form method="post" action="actions.php" enctype="multipart/form-data">
                <input type="hidden" name="csrf" value="<?= csrf_token() ?>">
                <input type="hidden" name="action" value="create_portfolio_category">
                <input type="hidden" name="id" value="">
                <label>Name</label>
                <input type="text" name="name" required>
                <label>SEO Title</label>
                <input type="text" name="seo_title" placeholder="Optional SEO title">
                <label>SEO Description</label>
                <textarea name="seo_description" rows="3" placeholder="Optional SEO description"></textarea>
                <label>Thumbnail</label>
                <input type="file" name="thumbnail" accept="image/*">
                <label>Status</label>
                <select name="status">
                    <option>Published</option>
                    <option>Draft</option>
                </select>
                <button type="submit" class="btn btn-primary">Save Category</button>
            </form>
        </div>
    </div>
<?php include __DIR__ . '/includes/footer.php'; ?>
