<?php
/**
 * Reviews Page — Slider (right to left) + Submit Form
 * Path: reviews.php
 */
include_once 'header.php';

$review_success = '';
$review_error   = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'submit_review') {
    $name   = trim($_POST['client_name'] ?? '');
    $rating = intval($_POST['rating'] ?? 5);
    $text   = trim($_POST['review_text'] ?? '');
    if ($name && $text) {
        save_customer_review($name, $rating, $text) ? $review_success = 'ধন্যবাদ! আপনার রিভিউ প্রকাশিত হয়েছে।' : $review_error = 'সেভ করতে সমস্যা হয়েছে।';
    } else {
        $review_error = 'নাম ও রিভিউ লিখুন।';
    }
}

$testimonials = get_testimonials();
?>

<section class="relative bg-gradient-to-r from-slate-950 to-blue-900 text-white py-14 md:py-20">
    <div class="max-w-7xl mx-auto px-4 text-center">
        <nav class="text-xs text-sky-400 font-bold uppercase mb-3">
            <a href="index.php" class="hover:text-white">Home</a> <span class="mx-2">/</span> Reviews
        </nav>
        <h1 class="text-3xl md:text-5xl font-black uppercase">Client Reviews</h1>
        <div class="w-16 h-1 bg-secondary mx-auto mt-4"></div>
    </div>
</section>

<!-- Review Slider — ডান থেকে বামে -->
<section class="py-12 bg-gray-50 overflow-hidden">
    <div class="max-w-7xl mx-auto px-4 mb-6">
        <h2 class="text-xl font-black text-primary uppercase text-center">What Our Clients Say</h2>
    </div>

    <?php if (!empty($testimonials)): ?>
    <div class="review-slider overflow-hidden relative">
        <div class="review-track flex gap-5" style="animation: reviewSlide 30s linear infinite; width:max-content;">
            <?php
            $slider_items = array_merge($testimonials, $testimonials);
            foreach ($slider_items as $t):
            ?>
            <div class="flex-shrink-0 w-72 md:w-80 bg-white rounded-2xl p-6 border border-gray-100 shadow-md hover:shadow-xl transition-shadow">
                <div class="flex text-amber-400 text-sm mb-3">
                    <?php for ($i=0;$i<intval($t['rating']);$i++): ?><i class="fa-solid fa-star"></i><?php endfor; ?>
                    <?php for ($i=intval($t['rating']);$i<5;$i++): ?><i class="fa-regular fa-star opacity-30"></i><?php endfor; ?>
                </div>
                <p class="text-sm text-gray-600 italic leading-relaxed mb-4">"<?php echo htmlspecialchars($t['review_text']); ?>"</p>
                <div class="flex items-center justify-between pt-3 border-t border-gray-100">
                    <div>
                        <p class="font-bold text-gray-900 text-sm"><?php echo htmlspecialchars($t['client_name']); ?></p>
                        <p class="text-[10px] text-gray-400"><?php echo isset($t['review_date']) ? date('d M Y', strtotime($t['review_date'])) : 'Recent'; ?></p>
                    </div>
                    <span class="text-[10px] font-bold text-primary bg-blue-50 px-2 py-1 rounded-full"><?php echo htmlspecialchars($t['source'] ?? 'Website'); ?></span>
                </div>
            </div>
            <?php endforeach; ?>
        </div>
    </div>
    <style>
        @keyframes reviewSlide {
            0%   { transform: translateX(0); }
            100% { transform: translateX(-50%); }
        }
        .review-slider:hover .review-track { animation-play-state: paused; }
    </style>
    <?php else: ?>
    <p class="text-center text-gray-400 py-8">এখনো কোনো রিভিউ নেই। নিচে প্রথম রিভিউ দিন!</p>
    <?php endif; ?>
</section>

<!-- Submit Review Form -->
<section class="py-16 bg-white" id="write-review">
    <div class="max-w-xl mx-auto px-4 sm:px-6">
        <div class="text-center mb-8 space-y-2">
            <h2 class="text-2xl font-black text-primary uppercase">Write a Review</h2>
            <p class="text-sm text-gray-500">আপনার মতামত আমাদের সাথে শেয়ার করুন</p>
        </div>

        <?php if ($review_success): ?>
        <div class="mb-6 p-4 bg-green-50 border border-green-200 text-green-700 rounded-xl text-sm flex items-center">
            <i class="fa-solid fa-circle-check mr-3"></i><?php echo $review_success; ?>
        </div>
        <?php endif; ?>
        <?php if ($review_error): ?>
        <div class="mb-6 p-4 bg-red-50 border border-red-200 text-red-700 rounded-xl text-sm flex items-center">
            <i class="fa-solid fa-triangle-exclamation mr-3"></i><?php echo $review_error; ?>
        </div>
        <?php endif; ?>

        <form method="post" action="#write-review" class="bg-gray-50 rounded-2xl p-6 md:p-8 border border-gray-100 shadow-sm space-y-5">
            <input type="hidden" name="action" value="submit_review">
            <div>
                <label class="block text-xs font-bold uppercase text-gray-600 mb-2">Your Name *</label>
                <input type="text" name="client_name" required placeholder="আপনার নাম"
                    class="w-full bg-white border border-gray-200 rounded-xl px-4 py-3 text-sm focus:ring-2 focus:ring-primary">
            </div>
            <div>
                <label class="block text-xs font-bold uppercase text-gray-600 mb-2">Rating *</label>
                <div class="flex gap-2" id="star-rating">
                    <?php for ($i=1;$i<=5;$i++): ?>
                    <button type="button" onclick="setStar(<?php echo $i; ?>)" data-star="<?php echo $i; ?>"
                        class="star-r text-2xl text-gray-300 hover:text-amber-400 transition-colors">
                        <i class="fa-solid fa-star"></i>
                    </button>
                    <?php endfor; ?>
                </div>
                <input type="hidden" name="rating" id="rating-val" value="5">
            </div>
            <div>
                <label class="block text-xs font-bold uppercase text-gray-600 mb-2">Your Review *</label>
                <textarea name="review_text" rows="4" required placeholder="আপনার অভিজ্ঞতা লিখুন..."
                    class="w-full bg-white border border-gray-200 rounded-xl px-4 py-3 text-sm focus:ring-2 focus:ring-primary"></textarea>
            </div>
            <button type="submit" class="w-full bg-primary hover:bg-secondary text-white font-bold py-4 rounded-xl transition-all shadow-md text-sm uppercase tracking-wider">
                <i class="fa-solid fa-paper-plane mr-2"></i> Submit Review
            </button>
        </form>
    </div>
</section>

<script>
    function setStar(n) {
        document.getElementById('rating-val').value = n;
        document.querySelectorAll('.star-r').forEach((btn, i) => {
            btn.classList.toggle('text-amber-400', i < n);
            btn.classList.toggle('text-gray-300', i >= n);
        });
    }
    setStar(5);
</script>

<?php include_once 'footer.php'; ?>
