<?php $basePath = defined('BASE_PATH') ? BASE_PATH : '/gmind'; ?>
<!-- Testimonials Management -->
<div class="admin-section">
    <!-- Add New Testimonial Form -->
    <div class="admin-card" style="margin-bottom: 24px;">
        <div class="admin-card-header">
            <h2 class="admin-card-title">Add Testimonial</h2>
        </div>
        <div class="admin-card-body">
            <form method="POST" action="<?= $basePath ?>/admin/testimonials" 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="testimonial_name" class="admin-form-label">Student Name <span class="admin-required">*</span></label>
                        <input type="text" id="testimonial_name" name="name" class="admin-form-input" required placeholder="Enter student name">
                    </div>
                    <div class="admin-form-group">
                        <label for="testimonial_rating" class="admin-form-label">Rating</label>
                        <select id="testimonial_rating" name="rating" class="admin-form-input">
                            <option value="5">5 Stars - Excellent</option>
                            <option value="4">4 Stars - Very Good</option>
                            <option value="3">3 Stars - Good</option>
                            <option value="2">2 Stars - Fair</option>
                            <option value="1">1 Star - Poor</option>
                        </select>
                    </div>
                </div>

                <div class="admin-form-row">
                    <div class="admin-form-group">
                        <label for="testimonial_course" class="admin-form-label">Course Name</label>
                        <input type="text" id="testimonial_course" name="course_name" class="admin-form-input" placeholder="e.g. ADCA, Tally Prime, Python">
                    </div>
                    <div class="admin-form-group">
                        <label for="testimonial_rating" class="admin-form-label">Rating</label>
                        <select id="testimonial_rating" name="rating" class="admin-form-input">
                            <option value="5">5 Stars - Excellent</option>
                            <option value="4">4 Stars - Very Good</option>
                            <option value="3">3 Stars - Good</option>
                            <option value="2">2 Stars - Fair</option>
                            <option value="1">1 Star - Poor</option>
                        </select>
                    </div>
                </div>

                <div class="admin-form-group">
                    <label for="testimonial_review" class="admin-form-label">Review / Testimonial Text</label>
                    <textarea id="testimonial_review" name="review" class="admin-form-textarea" rows="3" placeholder="Enter the student's testimonial or review..."></textarea>
                </div>

                <div class="admin-form-row">
                    <div class="admin-form-group">
                        <label for="testimonial_photo" class="admin-form-label">Student Photo</label>
                        <input type="file" id="testimonial_photo" name="photo" class="admin-form-input" accept="image/jpeg,image/png,image/gif,image/webp"
                               data-preview="testimonialPhotoPreview">
                        <div id="testimonialPhotoPreview" class="admin-image-preview"></div>
                    </div>
                    <div class="admin-form-group">
                        <p style="color:#666;font-size:0.85rem;margin-top:24px;">
                            Testimonials added by admin are automatically approved and will appear on the website immediately.
                        </p>
                    </div>
                </div>

                <div class="admin-form-actions">
                    <button type="submit" class="admin-btn admin-btn-primary">Add Testimonial</button>
                </div>
            </form>
        </div>
    </div>

    <!-- Testimonials Table -->
    <div class="admin-card">
        <div class="admin-card-header">
            <h2 class="admin-card-title">All Testimonials (<?= count($testimonials ?? []) ?>)</h2>
        </div>
        <div class="admin-card-body">
            <?php if (!empty($testimonials)): ?>
            <div class="admin-table-responsive">
                <table class="admin-table" data-search="true">
                    <thead>
                        <tr>
                            <th>Photo</th>
                            <th>Student</th>
                            <th>Course</th>
                            <th>Rating</th>
                            <th>Review</th>
                            <th>Status</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($testimonials as $t): ?>
                        <tr>
                            <td>
                                <?php if (!empty($t['photo'])): ?>
                                <img src="<?= htmlspecialchars(upload_url($t['photo']), ENT_QUOTES, 'UTF-8') ?>" alt="" class="admin-table-thumb" style="border-radius:50%;">
                                <?php else: ?>
                                <div class="admin-table-thumb" style="border-radius:50%;background:#eee;display:flex;align-items:center;justify-content:center;color:#999;font-size:1rem;"><?= strtoupper(mb_substr($t['name'] ?? 'S', 0, 1)) ?></div>
                                <?php endif; ?>
                            </td>
                            <td>
                                <strong><?= htmlspecialchars($t['name'], ENT_QUOTES, 'UTF-8') ?></strong>
                            </td>
                            <td><?= htmlspecialchars($t['course_name'] ?? '&mdash;', ENT_QUOTES, 'UTF-8') ?></td>
                            <td>
                                <span class="admin-stars">
                                    <?php for ($i = 1; $i <= 5; $i++): ?>
                                    <?php if ($i <= (int) ($t['rating'] ?? 5)): ?>
                                    <span class="admin-star admin-star-filled">&#9733;</span>
                                    <?php else: ?>
                                    <span class="admin-star">&#9733;</span>
                                    <?php endif; ?>
                                    <?php endfor; ?>
                                </span>
                            </td>
                            <td>
                                <span class="admin-review-preview" title="<?= htmlspecialchars($t['review'] ?? '', ENT_QUOTES, 'UTF-8') ?>">
                                    <?= htmlspecialchars(mb_substr($t['review'] ?? '', 0, 80) . (mb_strlen($t['review'] ?? '') > 80 ? '...' : ''), ENT_QUOTES, 'UTF-8') ?>
                                </span>
                            </td>
                            <td>
                                <?php
                                $isApproved = false;
                                if (isset($t['status'])) {
                                    $isApproved = $t['status'] === 'approved';
                                } elseif (!empty($t['is_approved'])) {
                                    $isApproved = true;
                                }
                                ?>
                                <?php if ($isApproved): ?>
                                <span class="admin-badge admin-badge-success">Approved</span>
                                <?php else: ?>
                                <span class="admin-badge admin-badge-warning">Pending</span>
                                <?php endif; ?>
                            </td>
                            <td>
                                <div class="admin-actions">
                                    <?php if (!$isApproved): ?>
                                    <form method="POST" action="<?= $basePath ?>/admin/testimonials/<?= (int) $t['id'] ?>/approve" style="display:inline">
                                        <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8') ?>">
                                        <button type="submit" class="admin-btn admin-btn-sm admin-btn-success" title="Approve">
                                            &#10003; Approve
                                        </button>
                                    </form>
                                    <?php else: ?>
                                    <form method="POST" action="<?= $basePath ?>/admin/testimonials/<?= (int) $t['id'] ?>/reject" style="display:inline">
                                        <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8') ?>">
                                        <button type="submit" class="admin-btn admin-btn-sm admin-btn-warning" title="Reject">
                                            &#10007; Reject
                                        </button>
                                    </form>
                                    <?php endif; ?>
                                    <form method="POST" action="<?= $basePath ?>/admin/testimonials/<?= (int) $t['id'] ?>/delete" style="display:inline">
                                        <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8') ?>">
                                        <button type="submit" class="admin-btn admin-btn-sm admin-btn-danger" title="Delete"
                                                data-confirm-delete>
                                            Delete
                                        </button>
                                    </form>
                                </div>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
            <?php else: ?>
            <p class="admin-empty-text">No testimonials found. Add your first testimonial above.</p>
            <?php endif; ?>
        </div>
    </div>
</div>
