<?php $basePath = defined('BASE_PATH') ? BASE_PATH : '/gmind'; ?>
<!-- Gallery Management -->
<div class="admin-section">
    <?php if (!($showForm ?? false)): ?>
    <div class="admin-section-header">
        <button type="button" class="admin-btn admin-btn-primary" onclick="document.getElementById('addGalleryForm').style.display='block';this.style.display='none';">
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="18" height="18"><line x1="12" y1="5" x2="12" y2="19"/><line x1="5" y1="12" x2="19" y2="12"/></svg>
            Add Gallery Image
        </button>
    </div>
    <?php endif; ?>

    <!-- Add/Edit Gallery Form -->
    <div class="admin-card" style="margin-bottom: 24px;<?= ($showForm ?? false) ? '' : 'display:none;' ?>" id="addGalleryForm">
        <div class="admin-card-header">
            <h2 class="admin-card-title"><?= ($editItem ?? null) ? 'Edit Gallery Image' : 'Add Gallery Image' ?></h2>
        </div>
        <div class="admin-card-body">
            <form method="POST" action="<?= ($editItem ?? null) ? $basePath . '/admin/gallery/' . (int)($editItem['id'] ?? 0) : $basePath . '/admin/gallery' ?>" class="admin-form" enctype="multipart/form-data" novalidate>
                <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8') ?>">
                <div class="admin-form-row">
                    <div class="admin-form-group">
                        <label for="gallery_title" class="admin-form-label">Title <span class="admin-required">*</span></label>
                        <input type="text" id="gallery_title" name="title" class="admin-form-input" required placeholder="Enter image title"
                               value="<?= htmlspecialchars($editItem['title'] ?? '', ENT_QUOTES, 'UTF-8') ?>">
                    </div>
                    <div class="admin-form-group">
                        <label for="gallery_category" class="admin-form-label">Category</label>
                        <input type="text" id="gallery_category" name="category" class="admin-form-input" placeholder="e.g. Events, Classroom, Campus"
                               value="<?= htmlspecialchars($editItem['category'] ?? '', ENT_QUOTES, 'UTF-8') ?>">
                    </div>
                </div>
                <div class="admin-form-group">
                    <label for="gallery_description" class="admin-form-label">Description</label>
                    <textarea id="gallery_description" name="description" class="admin-form-textarea" rows="2" placeholder="Brief description (optional)"><?= htmlspecialchars($editItem['description'] ?? '', ENT_QUOTES, 'UTF-8') ?></textarea>
                </div>
                <div class="admin-form-row">
                    <div class="admin-form-group">
                        <label for="gallery_image" class="admin-form-label">Image <?= ($editItem ?? null) ? '' : '<span class="admin-required">*</span>' ?></label>
                        <input type="file" id="gallery_image" name="image" class="admin-form-input" accept="image/jpeg,image/png,image/gif,image/webp"
                               <?= ($editItem ?? null) ? '' : 'required' ?>
                               data-preview="galleryImagePreview">
                        <?php if ($editItem ?? null): ?>
                        <?php if (!empty($editItem['image'])): ?>
                        <div class="admin-current-image">
                            <span>Current:</span>
                            <img src="<?= htmlspecialchars(upload_url($editItem['image']), ENT_QUOTES, 'UTF-8') ?>" alt="Current image" height="60">
                        </div>
                        <?php endif; ?>
                        <?php endif; ?>
                        <div id="galleryImagePreview" class="admin-image-preview"></div>
                    </div>
                    <div class="admin-form-group">
                        <label for="gallery_sort" class="admin-form-label">Sort Order</label>
                        <input type="number" id="gallery_sort" name="sort_order" class="admin-form-input" value="<?= (int) ($editItem['sort_order'] ?? 0) ?>" min="0">
                    </div>
                </div>
                <div class="admin-form-group">
                    <label class="admin-form-label">Active Status</label>
                    <label class="admin-toggle">
                        <input type="checkbox" name="active" value="1" <?= (($editItem['active'] ?? 1) == 1) ? 'checked' : '' ?>>
                        <span class="admin-toggle-slider"></span>
                        <span class="admin-toggle-label">Show on website</span>
                    </label>
                </div>
                <div class="admin-form-actions">
                    <button type="submit" class="admin-btn admin-btn-primary"><?= ($editItem ?? null) ? 'Update Gallery Image' : 'Add Gallery Image' ?></button>
                    <a href="<?= $basePath ?>/admin/gallery" class="admin-btn admin-btn-secondary">Cancel</a>
                </div>
            </form>
        </div>
    </div>

    <!-- Gallery Items List -->
    <div class="admin-card">
        <div class="admin-card-header">
            <h2 class="admin-card-title">All Gallery Images (<?= count($galleryItems ?? []) ?>)</h2>
        </div>
        <div class="admin-card-body">
            <div class="admin-table-responsive">
                <table class="admin-table" data-search="true">
                    <thead>
                        <tr>
                            <th>Image</th>
                            <th>Title</th>
                            <th>Category</th>
                            <th>Sort</th>
                            <th>Status</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($galleryItems ?? [] as $item): ?>
                        <tr>
                            <td>
                                <?php if (!empty($item['image'])): ?>
                                <img src="<?= htmlspecialchars(upload_url($item['image']), ENT_QUOTES, 'UTF-8') ?>" alt="" class="admin-table-thumb">
                                <?php else: ?>
                                <span class="admin-no-image">&mdash;</span>
                                <?php endif; ?>
                            </td>
                            <td><strong><?= htmlspecialchars($item['title'] ?? '', ENT_QUOTES, 'UTF-8') ?></strong></td>
                            <td><span class="admin-badge admin-badge-category"><?= htmlspecialchars(ucfirst($item['category'] ?? 'N/A'), ENT_QUOTES, 'UTF-8') ?></span></td>
                            <td><?= (int) ($item['sort_order'] ?? 0) ?></td>
                            <td>
                                <?php if (($item['active'] ?? 0) == 1): ?>
                                <span class="admin-badge admin-badge-success">Active</span>
                                <?php else: ?>
                                <span class="admin-badge admin-badge-warning">Inactive</span>
                                <?php endif; ?>
                            </td>
                            <td>
                                <div class="admin-actions">
                                    <!-- Edit Button -->
                                    <a href="<?= $basePath ?>/admin/gallery/<?= (int) ($item['id'] ?? 0) ?>/edit" class="admin-btn-icon" title="Edit">
                                        <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="16" height="16"><path d="M11 4H4a2 2 0 00-2 2v14a2 2 0 002 2h14a2 2 0 002-2v-7"/><path d="M18.5 2.5a2.121 2.121 0 013 3L12 15l-4 1 1-4 9.5-9.5z"/></svg>
                                    </a>
                                    <!-- Toggle Active Form -->
                                    <form method="POST" action="<?= $basePath ?>/admin/gallery/<?= (int) ($item['id'] ?? 0) ?>/toggle" style="display:inline">
                                        <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8') ?>">
                                        <button type="submit" class="admin-btn-icon" title="<?= ($item['active'] ?? 0) ? 'Deactivate' : 'Activate' ?>" style="border:none;background:none;cursor:pointer;padding:4px;">
                                            <?php if (($item['active'] ?? 0) == 1): ?>
                                            <span style="color:#27ae60;font-weight:600;font-size:0.8rem;">Active</span>
                                            <?php else: ?>
                                            <span style="color:#999;font-size:0.8rem;">Inactive</span>
                                            <?php endif; ?>
                                        </button>
                                    </form>
                                    <!-- Delete Form -->
                                    <form method="POST" action="<?= $basePath ?>/admin/gallery/<?= (int) ($item['id'] ?? 0) ?>/delete" style="display:inline" onsubmit="return confirm('Delete this gallery image? The image file will also be removed.')">
                                        <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8') ?>">
                                        <button type="submit" class="admin-btn-icon admin-btn-delete" title="Delete">
                                            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="16" height="16"><polyline points="3 6 5 6 21 6"/><path d="M19 6v14a2 2 0 01-2 2H7a2 2 0 01-2-2V6m3 0V4a2 2 0 012-2h4a2 2 0 012 2v2"/></svg>
                                        </button>
                                    </form>
                                </div>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                        <?php if (empty($galleryItems)): ?>
                        <tr><td colspan="6" style="text-align:center;padding:24px;color:#999;">No gallery images yet. Add your first image above.</td></tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
