<?php $basePath = defined('BASE_PATH') ? BASE_PATH : '/gmind'; ?>
<!-- Sliders Management -->
<div class="admin-section">
    <?php if (!($showForm ?? false)): ?>
    <div class="admin-section-header">
        <a href="<?= $basePath ?>/admin/sliders/create" class="admin-btn admin-btn-primary">
            <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 New Slider
        </a>
    </div>
    <?php endif; ?>

    <?php if ($showForm ?? false): ?>
    <!-- Add/Edit Slider Form -->
    <div class="admin-card" style="margin-bottom: 24px;">
        <div class="admin-card-header">
            <h2 class="admin-card-title"><?= $editSlider ? 'Edit Slider' : 'Add New Slider' ?></h2>
        </div>
        <div class="admin-card-body">
            <form method="POST" action="<?= $editSlider ? $basePath . '/admin/sliders/' . (int)$editSlider['id'] : $basePath . '/admin/sliders' ?>" enctype="multipart/form-data" class="admin-form" 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="title" class="admin-form-label">Title <span class="admin-required">*</span></label>
                        <input type="text" id="title" name="title" class="admin-form-input" required
                               value="<?= htmlspecialchars($editSlider['title'] ?? '', ENT_QUOTES, 'UTF-8') ?>">
                    </div>
                    <div class="admin-form-group">
                        <label for="subtitle" class="admin-form-label">Subtitle</label>
                        <input type="text" id="subtitle" name="subtitle" class="admin-form-input"
                               value="<?= htmlspecialchars($editSlider['subtitle'] ?? '', ENT_QUOTES, 'UTF-8') ?>">
                    </div>
                </div>

                <div class="admin-form-row">
                    <div class="admin-form-group">
                        <label for="link" class="admin-form-label">Link URL</label>
                        <input type="url" id="link" name="link" class="admin-form-input"
                               value="<?= htmlspecialchars($editSlider['link'] ?? '', ENT_QUOTES, 'UTF-8') ?>">
                    </div>
                    <div class="admin-form-group">
                        <label for="sort_order" class="admin-form-label">Sort Order</label>
                        <input type="number" id="sort_order" name="sort_order" class="admin-form-input" min="0"
                               value="<?= htmlspecialchars($editSlider['sort_order'] ?? '0', ENT_QUOTES, 'UTF-8') ?>">
                    </div>
                </div>

                <div class="admin-form-row">
                    <div class="admin-form-group">
                        <label for="image" class="admin-form-label">Slider Image <?= $editSlider ? '' : '<span class="admin-required">*</span>' ?></label>
                        <input type="file" id="image" name="image" class="admin-form-input" accept="image/jpeg,image/png,image/gif,image/webp"
                               data-preview="imagePreview">
                        <?php if ($editSlider && !empty($editSlider['image'])): ?>
                        <div class="admin-current-image">
                            <span>Current:</span>
                            <img src="<?= htmlspecialchars(upload_url($editSlider['image']), ENT_QUOTES, 'UTF-8') ?>" alt="Current slider" height="60">
                        </div>
                        <?php endif; ?>
                        <div id="imagePreview" class="admin-image-preview"></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" <?= ($editSlider['active'] ?? 1) ? 'checked' : '' ?>>
                            <span class="admin-toggle-slider"></span>
                            <span class="admin-toggle-label">Active</span>
                        </label>
                    </div>
                </div>

                <div class="admin-form-actions">
                    <button type="submit" class="admin-btn admin-btn-primary"><?= $editSlider ? 'Update Slider' : 'Create Slider' ?></button>
                    <a href="<?= $basePath ?>/admin/sliders" class="admin-btn admin-btn-secondary">Cancel</a>
                </div>
            </form>
        </div>
    </div>
    <?php endif; ?>

    <!-- Sliders Table -->
    <?php if (!empty($sliders)): ?>
    <div class="admin-card">
        <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>Sort Order</th>
                            <th>Active</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($sliders as $slider): ?>
                        <tr>
                            <td>
                                <?php if (!empty($slider['image'])): ?>
                                <img src="<?= htmlspecialchars(!empty($slider['image']) ? upload_url($slider['image']) : $basePath . '/images/slider-default.jpg', ENT_QUOTES, 'UTF-8') ?>" alt="<?= htmlspecialchars($slider['title'], ENT_QUOTES, 'UTF-8') ?>" class="admin-table-thumb">
                                <?php else: ?>
                                <span class="admin-no-image">No image</span>
                                <?php endif; ?>
                            </td>
                            <td>
                                <strong><?= htmlspecialchars($slider['title'], ENT_QUOTES, 'UTF-8') ?></strong>
                                <?php if ($slider['subtitle']): ?>
                                <br><small><?= htmlspecialchars($slider['subtitle'], ENT_QUOTES, 'UTF-8') ?></small>
                                <?php endif; ?>
                            </td>
                            <td><?= (int) $slider['sort_order'] ?></td>
                            <td>
                                <label class="admin-toggle admin-toggle-sm">
                                    <input type="checkbox" name="active" value="1" <?= $slider['active'] ? 'checked' : '' ?>
                                           data-toggle-url="<?= $basePath ?>/admin/sliders/<?= (int) $slider['id'] ?>/toggle"
                                           data-toggle-field="active">
                                    <span class="admin-toggle-slider"></span>
                                </label>
                            </td>
                            <td>
                                <div class="admin-actions">
                                    <a href="<?= $basePath ?>/admin/sliders/<?= (int) $slider['id'] ?>/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>
                                    <form method="POST" action="<?= $basePath ?>/admin/sliders/<?= (int) $slider['id'] ?>/delete" style="display:inline" onsubmit="return confirm('Delete this slider? 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; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    <?php else: ?>
    <div class="admin-card">
        <div class="admin-card-body">
            <p class="admin-empty-text">No sliders found. <a href="<?= $basePath ?>/admin/sliders/create">Add your first slider</a>.</p>
        </div>
    </div>
    <?php endif; ?>
</div>
