<?php
require_once __DIR__ . '/auth-check.php';
require_once __DIR__ . '/../includes/functions.php';

$testimonials = getData('testimonials');

// Handle add
if ($_SERVER['REQUEST_METHOD'] === 'POST' && !empty($_POST['quote'])) {
    $testimonials[] = [
        'id'      => generateId($testimonials),
        'quote'   => trim($_POST['quote'] ?? ''),
        'company' => trim($_POST['company'] ?? ''),
        'country' => trim($_POST['country'] ?? ''),
        'rating'  => (int)($_POST['rating'] ?? 5),
    ];
    saveData('testimonials', $testimonials);
    setFlash('success', 'Testimonial added');
    header('Location: testimonials.php');
    exit;
}

// Handle delete
if (!empty($_GET['delete'])) {
    $testimonials = array_values(array_filter($testimonials, fn($t) => $t['id'] != $_GET['delete']));
    saveData('testimonials', $testimonials);
    setFlash('success', 'Testimonial deleted');
    header('Location: testimonials.php');
    exit;
}

$pageTitle = 'Testimonials';
include __DIR__ . '/sidebar.php';
?>

<div class="admin-header">
    <h1 class="admin-title">Testimonials</h1>
</div>

<!-- Add Form -->
<div class="admin-card mb-4">
    <h3 style="font-family:var(--font-heading);font-size:0.9rem;text-transform:uppercase;color:var(--text);margin-bottom:1rem;">Add Testimonial</h3>
    <form method="POST" class="row g-3">
        <div class="col-md-6">
            <textarea name="quote" class="form-control" rows="3" placeholder="Quote *" required style="background:var(--bg2);color:var(--text);border-color:var(--border);"></textarea>
        </div>
        <div class="col-md-2">
            <input type="text" name="company" class="form-control" placeholder="Company" style="background:var(--bg2);color:var(--text);border-color:var(--border);">
        </div>
        <div class="col-md-2">
            <input type="text" name="country" class="form-control" placeholder="Country" style="background:var(--bg2);color:var(--text);border-color:var(--border);">
        </div>
        <div class="col-md-1">
            <select name="rating" class="form-select" style="background:var(--bg2);color:var(--text);border-color:var(--border);font-size:0.85rem;">
                <option value="5">5 ★</option>
                <option value="4">4 ★</option>
                <option value="3">3 ★</option>
                <option value="2">2 ★</option>
                <option value="1">1 ★</option>
            </select>
        </div>
        <div class="col-md-1">
            <button type="submit" class="btn-admin-primary w-100" style="font-size:0.8rem;padding:0.5rem;">Add</button>
        </div>
    </form>
</div>

<!-- Testimonials List -->
<div class="admin-card">
    <?php if (!empty($testimonials)): ?>
    <div class="table-responsive">
        <table class="admin-table">
            <thead>
                <tr>
                    <th>Quote</th>
                    <th>Company</th>
                    <th>Country</th>
                    <th>Rating</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($testimonials as $t): ?>
                <tr>
                    <td style="font-size:0.85rem;color:var(--text);max-width:400px;">"<?php echo e(truncateText($t['quote'], 80)); ?>"</td>
                    <td style="font-size:0.85rem;"><strong><?php echo e($t['company'] ?? ''); ?></strong></td>
                    <td style="font-size:0.82rem;color:var(--muted);"><?php echo e($t['country'] ?? ''); ?></td>
                    <td style="color:#f59e0b;font-size:0.9rem;"><?php echo str_repeat('★', (int)($t['rating'] ?? 5)); ?></td>
                    <td>
                        <a href="testimonials.php?delete=<?php echo e($t['id']); ?>" class="btn-admin-outline" style="font-size:0.7rem;padding:0.3rem 0.6rem;color:var(--danger);border-color:var(--danger);" onclick="return confirm('Delete this testimonial?');">Delete</a>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <?php else: ?>
    <p style="color:var(--muted);text-align:center;padding:2rem;">No testimonials yet. Add your first testimonial above.</p>
    <?php endif; ?>
</div>

<?php include __DIR__ . '/../includes/footer.php'; ?>
