<?php
use App\Modules\Coaches\Models\CoachPerformanceMetric;
$__template->layout('Layout.main');
?>
<?php $__template->section('title'); ?>ترتيب المدربين<?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<a href="/coach-performance" class="btn btn-outline"><i data-lucide="arrow-right" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> لوحة الأداء</a>
<?php $__template->endSection(); ?>

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

<!-- Month Selector -->
<div class="card" style="margin-bottom:20px;padding:15px;">
    <form method="GET" action="/coach-performance/ranking" style="display:flex;gap:10px;align-items:end;">
        <div style="min-width:180px;">
            <label class="form-label" style="font-size:12px;">الشهر</label>
            <select name="month" class="form-input" onchange="this.form.submit()">
                <?php foreach ($months as $m): ?>
                <option value="<?= e($m) ?>" <?= $yearMonth === $m ? 'selected' : '' ?>><?= e($m) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
    </form>
</div>

<?php if (!empty($ranking)): ?>
<div class="card" style="padding:0;overflow-x:auto;">
    <table style="width:100%;border-collapse:collapse;font-size:14px;">
        <thead>
            <tr style="background:#F9FAFB;border-bottom:2px solid #E5E7EB;">
                <th style="padding:12px 15px;text-align:right;font-weight:600;color:#374151;width:60px;">المركز</th>
                <th style="padding:12px 15px;text-align:right;font-weight:600;color:#374151;">المدرب</th>
                <th style="padding:12px 15px;text-align:right;font-weight:600;color:#374151;">الدرجة الكلية</th>
                <th style="padding:12px 15px;text-align:right;font-weight:600;color:#374151;">التقييم</th>
                <th style="padding:12px 15px;text-align:right;font-weight:600;color:#374151;">الحصص</th>
                <th style="padding:12px 15px;text-align:right;font-weight:600;color:#374151;">نسبة الحضور</th>
                <th style="padding:12px 15px;text-align:right;font-weight:600;color:#374151;">اللاعبين</th>
                <th style="padding:12px 15px;text-align:right;font-weight:600;color:#374151;">الاستبقاء</th>
                <th style="padding:12px 15px;text-align:right;font-weight:600;color:#374151;">إجراءات</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($ranking as $i => $coach):
                $rank = $i + 1;
                $compositeScore = (float) ($coach['composite_score'] ?? 0);
                $scoreColor = match(true) {
                    $compositeScore >= 85 => '#059669',
                    $compositeScore >= 70 => '#0D7377',
                    $compositeScore >= 55 => '#D97706',
                    default               => '#DC2626',
                };
                $rankColor = match(true) {
                    $rank === 1 => '#F59E0B',
                    $rank === 2 => '#9CA3AF',
                    $rank === 3 => '#B45309',
                    default     => '#6B7280',
                };
            ?>
            <tr style="border-bottom:1px solid #F3F4F6;<?= $rank <= 3 ? 'background:#FFFBEB;' : '' ?>" onmouseover="this.style.background='#F9FAFB'" onmouseout="this.style.background='<?= $rank <= 3 ? '#FFFBEB' : 'transparent' ?>'">
                <td style="padding:12px 15px;">
                    <span style="display:inline-flex;align-items:center;justify-content:center;width:28px;height:28px;border-radius:50%;background:<?= $rankColor ?>15;color:<?= $rankColor ?>;font-weight:700;font-size:13px;"><?= $rank ?></span>
                </td>
                <td style="padding:12px 15px;">
                    <div style="font-weight:600;color:#1A1A2E;"><?= e($coach['coach_name'] ?? '') ?></div>
                    <div style="font-size:11px;color:#9CA3AF;"><?= e($coach['coach_code'] ?? '') ?></div>
                </td>
                <td style="padding:12px 15px;">
                    <span style="font-weight:700;color:<?= $scoreColor ?>;font-size:16px;"><?= number_format($compositeScore, 0) ?>%</span>
                </td>
                <td style="padding:12px 15px;">
                    <?php $avgRating = $coach['avg_rating'] !== null ? (float) $coach['avg_rating'] : 0; ?>
                    <span style="color:#F59E0B;"><?= $avgRating > 0 ? number_format($avgRating, 1) . ' &#9733;' : '—' ?></span>
                    <div style="font-size:10px;color:#9CA3AF;">(<?= (int) ($coach['total_ratings_count'] ?? 0) ?>)</div>
                </td>
                <td style="padding:12px 15px;color:#374151;"><?= (int) ($coach['total_sessions_conducted'] ?? 0) ?>/<?= (int) ($coach['total_sessions_assigned'] ?? 0) ?></td>
                <td style="padding:12px 15px;color:#374151;"><?= number_format((float) ($coach['avg_attendance_rate'] ?? 0), 0) ?>%</td>
                <td style="padding:12px 15px;color:#374151;"><?= (int) ($coach['total_players_trained'] ?? 0) ?></td>
                <td style="padding:12px 15px;color:#374151;"><?= number_format((float) ($coach['player_retention_rate'] ?? 0), 0) ?>%</td>
                <td style="padding:12px 15px;">
                    <a href="/coach-performance/<?= (int) $coach['coach_id'] ?>" class="btn btn-sm btn-outline" style="font-size:12px;padding:4px 10px;">
                        <i data-lucide="eye" style="width:13px;height:13px;vertical-align:middle;"></i> تفاصيل
                    </a>
                </td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
</div>

<?php else: ?>
<div class="card" style="padding:60px 20px;text-align:center;">
    <div style="margin-bottom:15px;">
        <i data-lucide="trophy" 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;">لم يتم حساب الأداء لهذا الشهر.</p>
</div>
<?php endif; ?>

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