<?php
// Partial view for displaying product reviews
// Expected variables: $reviews, $currentUserId, $isAdmin
?>

<div class="reviews-list" id="reviewsList">
    <?php if (empty($reviews)): ?>
        <div class="no-reviews">
            <p>Пока нет отзывов об этом товаре. Будьте первым!</p>
        </div>
    <?php else: ?>
        <?php foreach ($reviews as $review): ?>
            <div class="review-item" data-review-id="<?= $review['review_id'] ?>" data-user-id="<?= $review['user_id'] ?>">
                <div class="review-header">
                    <div class="review-author-info">
                        <div class="review-author-avatar">
                            <?= strtoupper(mb_substr($review['full_name'], 0, 1)) ?>
                        </div>
                        <div>
                            <div class="review-author-name"><?= htmlspecialchars($review['full_name']) ?></div>
                            <div class="review-date">
                                <?= date('d.m.Y', strtotime($review['created_at'])) ?>
                            </div>
                        </div>
                    </div>
                    <div class="review-rating">
                        <?php for ($i = 1; $i <= 5; $i++): ?>
                            <span class="star <?= $i <= $review['rating'] ? 'filled' : '' ?>">★</span>
                        <?php endfor; ?>
                    </div>
                </div>
                
                <?php if (!empty($review['comment'])): ?>
                    <div class="review-comment">
                        <?= nl2br(htmlspecialchars($review['comment'])) ?>
                    </div>
                <?php endif; ?>

                <?php if (isset($currentUserId) && ($review['user_id'] == $currentUserId || $isAdmin)): ?>
                    <div class="review-actions">
                        <?php if ($review['user_id'] == $currentUserId): ?>
                            <button class="btn-small btn-edit-review" data-review-id="<?= $review['review_id'] ?>" 
                                    data-rating="<?= $review['rating'] ?>" 
                                    data-comment="<?= htmlspecialchars($review['comment']) ?>">
                                <i class="fas fa-edit"></i> Редактировать
                            </button>
                        <?php endif; ?>
                        <button class="btn-small btn-danger btn-delete-review" data-review-id="<?= $review['review_id'] ?>">
                            <i class="fas fa-trash"></i> Удалить
                        </button>
                    </div>
                <?php endif; ?>

                <?php if ($isAdmin && !$review['is_approved']): ?>
                    <div class="review-approval-notice">
                        <i class="fas fa-exclamation-circle"></i> Отзыв ожидает модерации
                    </div>
                <?php endif; ?>
            </div>
        <?php endforeach; ?>
    <?php endif; ?>
</div>

<style>
.reviews-list {
    margin-top: 20px;
}

.no-reviews {
    text-align: center;
    padding: 40px 20px;
    background: #f8f9fa;
    border-radius: 8px;
    color: #666;
}

.review-item {
    background: #fff;
    border: 1px solid #e0e0e0;
    border-radius: 8px;
    padding: 20px;
    margin-bottom: 15px;
    transition: box-shadow 0.3s;
}

.review-item:hover {
    box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}

.review-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    margin-bottom: 15px;
}

.review-author-info {
    display: flex;
    gap: 12px;
    align-items: center;
}

.review-author-avatar {
    width: 45px;
    height: 45px;
    border-radius: 50%;
    background: linear-gradient(135deg, #617365, #453227);
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    font-weight: bold;
}

.review-author-name {
    font-weight: bold;
    color: #453227;
    font-size: 16px;
}

.review-date {
    font-size: 13px;
    color: #999;
    margin-top: 2px;
}

.review-rating {
    display: flex;
    gap: 2px;
}

.review-rating .star {
    font-size: 18px;
    color: #ddd;
}

.review-rating .star.filled {
    color: #ffc107;
}

.review-comment {
    color: #333;
    line-height: 1.6;
    padding: 15px;
    background: #f8f9fa;
    border-radius: 6px;
    margin-bottom: 10px;
}

.review-actions {
    display: flex;
    gap: 10px;
    margin-top: 10px;
}

.btn-small {
    padding: 6px 12px;
    font-size: 13px;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    transition: all 0.3s;
}

.btn-edit-review {
    background: #617365;
    color: white;
}

.btn-edit-review:hover {
    background: #453227;
}

.btn-danger {
    background: #dc3545;
    color: white;
}

.btn-danger:hover {
    background: #c82333;
}

.review-approval-notice {
    background: #fff3cd;
    color: #856404;
    padding: 8px 12px;
    border-radius: 4px;
    font-size: 13px;
    margin-top: 10px;
}

@media (max-width: 768px) {
    .review-header {
        flex-direction: column;
        gap: 10px;
    }
    
    .review-actions {
        flex-direction: column;
    }
    
    .btn-small {
        width: 100%;
    }
}
</style>

