<?php $bp = defined('BASE_PATH') ? BASE_PATH : '/gmind'; ?>
<div class="admin-page-header">
    <h2>Branch Reviews</h2>
    <a href="<?= $bp ?>/branch/facilities" class="admin-btn admin-btn-outline">&larr; Back to Facilities</a>
</div>

<!-- Summary -->
<div class="admin-stats-grid">
    <div class="admin-stat-card">
        <div class="admin-stat-icon admin-stat-icon-orange">&#9733;</div>
        <div class="admin-stat-info">
            <span class="admin-stat-value"><?= htmlspecialchars(number_format($avgRating ?? 0, 1), ENT_QUOTES, 'UTF-8') ?> / 5</span>
            <span class="admin-stat-label">Average Rating</span>
        </div>
    </div>
    <div class="admin-stat-card">
        <div class="admin-stat-icon admin-stat-icon-blue">&#9998;</div>
        <div class="admin-stat-info">
            <span class="admin-stat-value"><?= (int)($totalReviews ?? 0) ?></span>
            <span class="admin-stat-label">Total Reviews</span>
        </div>
    </div>
</div>

<!-- Reviews List -->
<div class="admin-card">
    <div class="admin-card-header">
        <h3>All Reviews</h3>
    </div>
    <div class="admin-card-body">
        <?php if (!empty($reviews)): ?>
        <div class="admin-reviews-list">
            <?php foreach ($reviews as $r): ?>
            <div class="admin-review-card <?= !empty($r['is_approved']) ? 'admin-review-approved' : 'admin-review-pending' ?>">
                <div class="admin-review-header">
                    <div class="admin-review-user">
                        <strong><?= htmlspecialchars($r['user_name'] ?? 'Anonymous', ENT_QUOTES, 'UTF-8') ?></strong>
                        <span class="admin-review-date"><?= htmlspecialchars(date('d M Y', strtotime($r['created_at'] ?? 'now')), ENT_QUOTES, 'UTF-8') ?></span>
                    </div>
                    <div class="admin-review-rating">
                        <?php for ($i = 1; $i <= 5; $i++): ?>
                            <?php if ($i <= (int)($r['rating'] ?? 0)): ?>
                                <span class="admin-star admin-star-filled">&#9733;</span>
                            <?php else: ?>
                                <span class="admin-star admin-star-empty">&#9733;</span>
                            <?php endif; ?>
                        <?php endfor; ?>
                    </div>
                </div>
                <div class="admin-review-text">
                    <?= htmlspecialchars($r['review_text'] ?? '', ENT_QUOTES, 'UTF-8') ?>
                </div>
                <div class="admin-review-status">
                    <span class="admin-badge admin-badge-<?= !empty($r['is_approved']) ? 'success' : 'warning' ?>">
                        <?= !empty($r['is_approved']) ? 'Approved' : 'Pending' ?>
                    </span>
                </div>
                <div class="admin-review-actions">
                    <form method="POST" action="<?= $bp ?>/branch/facilities/reviews/<?= (int)($r['id'] ?? 0) ?>/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 <?= !empty($r['is_approved']) ? 'admin-btn-outline' : 'admin-btn-success' ?>">
                            <?= !empty($r['is_approved']) ? 'Unapprove' : 'Approve' ?>
                        </button>
                    </form>
                    <form method="POST" action="<?= $bp ?>/branch/facilities/reviews/<?= (int)($r['id'] ?? 0) ?>/delete" style="display:inline;"
                          onsubmit="return confirm('Are you sure you want to delete this review?');">
                        <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">Delete</button>
                    </form>
                </div>
            </div>
            <?php endforeach; ?>
        </div>
        <?php else: ?>
        <p class="admin-empty-text">No reviews yet.</p>
        <?php endif; ?>
    </div>
</div>
