<?php if (!defined('STORE_ROOT')) { http_response_code(403); exit; } $reviews = []; $totalReviews = 0; try { $reviews = Database::all( "SELECT r.* FROM reviews r WHERE r.store_id = :sid AND r.is_approved = 1 ORDER BY r.created_at DESC", [':sid' => $store_id] ); $totalReviews = count($reviews); } catch (\Throwable $e) { error_log('[Reviews] DB query failed'); } $isLoggedIn = Auth::isLoggedIn(); ?>
<div class="page-reviews">
<div class="page-banner sf-page-banner">
<img src="/uploads/banners/store.png" alt="" class="sf-page-banner-bg">
<div class="sf-page-banner-content">
<div class="page-banner-icon">
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="#fff" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M12 2l3.09 6.26L22 9.27l-5 4.87 1.18 6.88L12 17.77l-6.18 3.25L7 14.14 2 9.27l6.91-1.01L12 2z"/></svg>
</div>
<div class="page-banner-text">
<h2><?= $L('reviews_title', 'Customer Reviews') ?></h2>
<p><?= $L('reviews_subtitle', 'Real reviews from real customers. Leave a review after delivery and earn $5 credit.') ?></p>
</div>
</div>
</div>
<div class="reviews-tabs">
<button class="reviews-tab" data-target="rv-how"><?= $L('reviews_tab_how', 'How Reviews Work') ?></button>
<button class="reviews-tab active" data-target="rv-list"><?= $L('reviews_tab_list', 'Customer Reviews') ?></button>
<button class="reviews-tab" data-target="rv-submit"><?= $L('reviews_tab_submit', 'Leave a Review') ?></button>
<span class="reviews-total"><?= $totalReviews ?> Total</span>
</div>
<div class="reviews-section" id="rv-how">
<div class="rv-how-section">
<div class="rv-how-grid">
<div class="rv-how-card">
<div class="rv-how-step">1</div>
<h4><?= $L('reviews_step1_title', 'Complete an Order') ?></h4>
<p><?= $L('reviews_step1_desc', 'Place your first order and wait for successful delivery confirmation.') ?></p>
</div>
<div class="rv-how-card">
<div class="rv-how-step">2</div>
<h4><?= $L('reviews_step2_title', 'Write Your Review') ?></h4>
<p><?= $L('reviews_step2_desc', 'Share your honest experience. Rate the product, shipping, and overall quality.') ?></p>
</div>
<div class="rv-how-card">
<div class="rv-how-step">3</div>
<h4><?= $L('reviews_step3_title', 'Earn $5 Credit') ?></h4>
<p><?= $L('reviews_step3_desc', 'Once approved, you receive $5.00 store credit. Credits stack with each completed review.') ?></p>
</div>
</div>
</div>
</div>
<div class="reviews-section active" id="rv-list">
<?php if (empty($reviews)): ?>
<div class="page-message"><p><?= $L('reviews_empty', 'No reviews yet. Be the first to share your experience.') ?></p></div>
<?php else: ?>
<?php foreach ($reviews as $review): $voteCount = max(1, (int)($review['helpful_count'] ?? 1)); ?>
<div class="rv-card">
<div class="rv-card-top">
<?php if ($review['is_verified']): ?>
<div class="rv-verified">
<svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"><path d="M22 11.08V12a10 10 0 11-5.93-9.14"/><polyline points="22 4 12 14.01 9 11.01"/></svg>
<span class="rv-verified-label"><?= $L('reviews_verified', 'VERIFIED CUSTOMER') ?></span>
</div>
<?php endif; ?>
<div class="rv-card-title-date">
<div class="rv-stars rv-vote-stars" data-review-id="<?= (int)$review['id'] ?>">
<?php for ($i = 1; $i <= 5; $i++): ?><span class="rv-star<?= $i <= $review['rating'] ? ' filled' : '' ?>" data-star="<?= $i ?>"></span><?php endfor; ?>
</div>
<span class="rv-vote-count-badge" data-review-id="<?= (int)$review['id'] ?>">(<?= $voteCount ?> vote<?= $voteCount !== 1 ? 's' : '' ?>)</span>
<span class="rv-date"><?= date('M j, Y', strtotime($review['created_at'])) ?></span>
</div>
</div>
<?php if ($review['title']): ?><div class="rv-title"><?= htmlspecialchars(Crypt::render($review['title'])) ?></div><?php endif; ?>
<div class="rv-body"><?= htmlspecialchars(Crypt::render($review['body'])) ?></div>
<?php if ($review['admin_reply']): ?>
<div class="review-reply">
<strong><?= $L('reviews_store_response', 'Store Response:') ?></strong>
<p><?= nl2br(htmlspecialchars(Crypt::render($review['admin_reply']))) ?></p>
</div>
<?php endif; ?>
</div>
<?php endforeach; ?>
<?php endif; ?>
</div>
<div class="reviews-section" id="rv-submit">
<?php if (!$isLoggedIn): ?>
<div class="page-message">
<h2><?= $L('reviews_login_title', 'Login Required') ?></h2>
<p><?= $L('reviews_login_desc', 'You must be logged in with at least one completed order to leave a review.') ?></p>
<a href="#account" data-route="account" class="btn btn-primary" style="margin-top:16px"><?= $L('reviews_login_btn', 'Login or Register') ?></a>
</div>
<?php else: ?>
<div class="review-form-container">
<h3><?= $L('reviews_form_title', 'Write Your Review') ?></h3>
<form id="review-form" class="form-standard">
<div class="form-group">
<label><?= $L('reviews_label_rating', 'Rating') ?></label>
<div class="rv-stars" id="rating-selector" style="cursor:pointer;gap:4px">
<?php for ($i = 1; $i <= 5; $i++): ?>
<span class="rv-star filled" style="width:22px;height:22px;cursor:pointer" data-rating="<?= $i ?>"></span>
<?php endfor; ?>
</div>
<input type="hidden" id="review-rating" value="5">
</div>
<div class="form-group">
<label for="review-title"><?= $L('reviews_label_title', 'Title') ?></label>
<input type="text" id="review-title" class="form-input" placeholder="<?= $L('reviews_placeholder_title', 'Brief summary of your experience') ?>">
</div>
<div class="form-group">
<label for="review-body"><?= $L('reviews_label_body', 'Review') ?></label>
<textarea id="review-body" class="form-input form-textarea" placeholder="<?= $L('reviews_placeholder_body', 'Share the details of your experience') ?>" rows="4" required></textarea>
</div>
<button type="submit" class="btn btn-primary"><?= $L('reviews_submit_btn', 'Submit Review') ?></button>
</form>
<div id="review-form-status" class="form-status"></div>
</div>
<?php endif; ?>
</div>
</div>
<script>
var currentRating=5;
function esc(str){if(!str)return '';var d=document.createElement('div');d.textContent=str;return d.innerHTML;}
function submitReview(e){e.preventDefault();var status=document.getElementById('review-form-status');Bridge.post('/api/router.php',{action:'submit_review',rating:currentRating,title:document.getElementById('review-title').value,body:document.getElementById('review-body').value}).then(function(r){if(r.success){status.innerHTML='<p class="form-success">Review submitted. It will appear after approval.</p>';document.getElementById('review-form').reset();}else{status.innerHTML='<p class="form-error">'+esc(r.error||'Failed to submit.')+'</p>';}});}
var rf=document.getElementById('review-form');if(rf)rf.addEventListener('submit',function(e){submitReview(e);});
</script>
<script src="/assets/js/reviews-events.js"></script>
