<?php
$__template->layout('Layout.main');
?>
<?php $__template->section('title'); ?>تقييمات المدربين<?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<a href="/coach-ratings/create" class="btn btn-primary"><i data-lucide="plus" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> تقييم جديد</a>
<?php $__template->endSection(); ?>

<?php $__template->section('content'); ?>

<!-- Coach Selector & Averages -->
<div class="card" style="margin-bottom:20px;padding:15px;">
    <form method="GET" action="/coach-ratings" style="display:flex;gap:10px;align-items:end;flex-wrap:wrap;">
        <div style="min-width:250px;">
            <label class="form-label" style="font-size:12px;">المدرب</label>
            <select name="coach_id" class="form-input" onchange="this.form.submit()">
                <option value="">-- جميع المدربين --</option>
                <?php foreach ($coaches as $c): ?>
                <option value="<?= (int) $c['id'] ?>" <?= $coachId == $c['id'] ? 'selected' : '' ?>><?= e($c['full_name_ar']) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div style="min-width:120px;">
            <label class="form-label" style="font-size:12px;">الحالة</label>
            <select name="status" class="form-input">
                <option value="">الكل</option>
                <option value="published" <?= ($filters['status'] ?? '') === 'published' ? 'selected' : '' ?>>منشور</option>
                <option value="flagged" <?= ($filters['status'] ?? '') === 'flagged' ? 'selected' : '' ?>>مبلغ عنه</option>
                <option value="hidden" <?= ($filters['status'] ?? '') === 'hidden' ? 'selected' : '' ?>>مخفي</option>
            </select>
        </div>
        <button type="submit" class="btn btn-outline">تصفية</button>
    </form>
</div>

<!-- Coach Average Scores -->
<?php if ($averages && $averages['total_ratings'] > 0): ?>
<div class="card" style="margin-bottom:20px;">
    <div style="padding:20px;display:flex;justify-content:space-around;align-items:center;flex-wrap:wrap;gap:15px;">
        <div style="text-align:center;">
            <div style="font-size:32px;font-weight:700;color:#F59E0B;"><?= number_format($averages['avg_rating'], 1) ?></div>
            <div style="font-size:12px;color:#6B7280;">التقييم العام</div>
            <div style="color:#F59E0B;font-size:18px;">
                <?php for ($s = 1; $s <= 5; $s++): ?>
                    <?= $s <= round($averages['avg_rating']) ? '&#9733;' : '&#9734;' ?>
                <?php endfor; ?>
            </div>
            <div style="font-size:11px;color:#9CA3AF;">(<?= $averages['total_ratings'] ?> تقييم)</div>
        </div>
        <div style="display:grid;grid-template-columns:repeat(4, 1fr);gap:20px;">
            <div style="text-align:center;">
                <div style="font-size:20px;font-weight:700;color:#1A1A2E;"><?= $averages['avg_punctuality'] ? number_format($averages['avg_punctuality'], 1) : '—' ?></div>
                <div style="font-size:11px;color:#6B7280;">الالتزام</div>
            </div>
            <div style="text-align:center;">
                <div style="font-size:20px;font-weight:700;color:#1A1A2E;"><?= $averages['avg_communication'] ? number_format($averages['avg_communication'], 1) : '—' ?></div>
                <div style="font-size:11px;color:#6B7280;">التواصل</div>
            </div>
            <div style="text-align:center;">
                <div style="font-size:20px;font-weight:700;color:#1A1A2E;"><?= $averages['avg_technique'] ? number_format($averages['avg_technique'], 1) : '—' ?></div>
                <div style="font-size:11px;color:#6B7280;">الأسلوب</div>
            </div>
            <div style="text-align:center;">
                <div style="font-size:20px;font-weight:700;color:#1A1A2E;"><?= $averages['avg_motivation'] ? number_format($averages['avg_motivation'], 1) : '—' ?></div>
                <div style="font-size:11px;color:#6B7280;">التحفيز</div>
            </div>
        </div>
    </div>
</div>
<?php endif; ?>

<!-- Ratings List -->
<?php if (!empty($ratings)): ?>
<div class="card" style="padding:0;">
    <?php foreach ($ratings as $r):
        $ratingValue = (int) ($r['rating'] ?? 0);
        $status = $r['status'] ?? 'published';
        $isAnonymous = (int) ($r['is_anonymous'] ?? 0);
    ?>
    <div style="padding:15px 20px;border-bottom:1px solid #F3F4F6;">
        <div style="display:flex;justify-content:space-between;align-items:start;margin-bottom:8px;">
            <div>
                <div style="font-weight:600;color:#1A1A2E;font-size:14px;">
                    <?php if (!$coachId): ?>
                    <?= e($r['coach_name'] ?? '') ?> —
                    <?php endif; ?>
                    <?= $isAnonymous ? 'مجهول' : e($r['player_name'] ?? '') ?>
                </div>
                <div style="font-size:11px;color:#9CA3AF;"><?= e(substr($r['created_at'] ?? '', 0, 16)) ?></div>
            </div>
            <div style="display:flex;align-items:center;gap:8px;">
                <div style="color:#F59E0B;font-size:16px;">
                    <?php for ($s = 1; $s <= 5; $s++): ?>
                        <?= $s <= $ratingValue ? '&#9733;' : '&#9734;' ?>
                    <?php endfor; ?>
                </div>
                <?php if ($status === 'flagged'): ?>
                <span style="padding:2px 8px;border-radius:8px;font-size:10px;font-weight:600;background:#FEE2E2;color:#DC2626;">مبلغ عنه</span>
                <?php endif; ?>
            </div>
        </div>
        <?php if (!empty($r['feedback_ar'])): ?>
        <p style="margin:0 0 8px;font-size:13px;color:#4B5563;line-height:1.5;"><?= e($r['feedback_ar']) ?></p>
        <?php endif; ?>
        <?php if (!empty($r['admin_response_ar'])): ?>
        <div style="background:#F0F9FF;border-radius:6px;padding:8px 12px;font-size:12px;color:#0369A1;margin-top:6px;">
            <strong>رد الإدارة:</strong> <?= e($r['admin_response_ar']) ?>
        </div>
        <?php endif; ?>
        <div style="display:flex;gap:12px;margin-top:8px;font-size:11px;color:#6B7280;">
            <span>الالتزام: <?= (int) ($r['punctuality_score'] ?? 0) ?>/5</span>
            <span>التواصل: <?= (int) ($r['communication_score'] ?? 0) ?>/5</span>
            <span>الأسلوب: <?= (int) ($r['technique_score'] ?? 0) ?>/5</span>
            <span>التحفيز: <?= (int) ($r['motivation_score'] ?? 0) ?>/5</span>
        </div>
    </div>
    <?php endforeach; ?>
</div>

<?php if (isset($pagination) && ($pagination['last_page'] ?? 1) > 1): ?>
<div style="padding:15px;">
    <?php $__template->include('Shared.Components.pagination', ['pagination' => $pagination]); ?>
</div>
<?php endif; ?>

<?php else: ?>
<div class="card" style="padding:60px 20px;text-align:center;">
    <div style="margin-bottom:15px;">
        <i data-lucide="star" style="width:48px;height:48px;color:#D1D5DB;"></i>
    </div>
    <h3 style="color:#6B7280;margin:0 0 8px;">لا توجد تقييمات</h3>
    <p style="color:#9CA3AF;font-size:14px;margin:0 0 20px;">
        <?= $coachId ? 'لم يتم تقييم هذا المدرب بعد.' : 'لا توجد تقييمات مسجلة.' ?>
    </p>
    <a href="/coach-ratings/create<?= $coachId ? '?coach_id=' . $coachId : '' ?>" class="btn btn-primary">
        <i data-lucide="plus" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> تقييم جديد
    </a>
</div>
<?php endif; ?>

<script>
document.addEventListener('DOMContentLoaded', function() {
    if (typeof lucide !== 'undefined') lucide.createIcons();
});
</script>
<?php $__template->endSection(); ?>
