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

<?php $__template->section('page_actions'); ?>
<form method="POST" action="/coach-performance/calculate" style="display:inline;">
    <?= csrf_field() ?>
    <input type="hidden" name="month" value="<?= e($yearMonth) ?>">
    <button type="submit" class="btn btn-outline" onclick="return confirm('هل تريد إعادة حساب الأداء لجميع المدربين؟')">
        <i data-lucide="calculator" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> إعادة الحساب
    </button>
</form>
<a href="/coach-performance/ranking?month=<?= e($yearMonth) ?>" class="btn btn-primary">
    <i data-lucide="trophy" 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" 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)): ?>
<!-- Top Performers -->
<div style="display:grid;grid-template-columns:repeat(auto-fill, minmax(300px, 1fr));gap:20px;margin-bottom:20px;">
    <?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',
        };
        $avgRating = $coach['avg_rating'] !== null ? (float) $coach['avg_rating'] : 0;
    ?>
    <div class="card" style="position:relative;overflow:hidden;">
        <!-- Rank Badge -->
        <div style="position:absolute;top:12px;left:12px;width:28px;height:28px;border-radius:50%;background:<?= $rank <= 3 ? '#F59E0B' : '#E5E7EB' ?>;color:<?= $rank <= 3 ? '#fff' : '#6B7280' ?>;display:flex;align-items:center;justify-content:center;font-size:12px;font-weight:700;">
            <?= $rank ?>
        </div>

        <a href="/coach-performance/<?= (int) $coach['coach_id'] ?>" style="text-decoration:none;color:inherit;display:block;padding:20px;">
            <div style="display:flex;align-items:center;gap:12px;margin-bottom:12px;">
                <div style="width:48px;height:48px;border-radius:50%;background:<?= $scoreColor ?>15;display:flex;align-items:center;justify-content:center;">
                    <i data-lucide="user" style="width:24px;height:24px;color:<?= $scoreColor ?>;"></i>
                </div>
                <div>
                    <div style="font-weight:600;font-size:15px;color:#1A1A2E;"><?= e($coach['coach_name'] ?? '') ?></div>
                    <code style="font-size:10px;color:#9CA3AF;"><?= e($coach['coach_code'] ?? '') ?></code>
                </div>
            </div>

            <div style="display:grid;grid-template-columns:repeat(3, 1fr);gap:10px;text-align:center;">
                <div style="background:#F9FAFB;border-radius:6px;padding:8px;">
                    <div style="font-size:18px;font-weight:700;color:<?= $scoreColor ?>;"><?= number_format($compositeScore, 0) ?>%</div>
                    <div style="font-size:10px;color:#6B7280;">الأداء</div>
                </div>
                <div style="background:#F9FAFB;border-radius:6px;padding:8px;">
                    <div style="font-size:18px;font-weight:700;color:#F59E0B;"><?= $avgRating > 0 ? number_format($avgRating, 1) : '—' ?></div>
                    <div style="font-size:10px;color:#6B7280;">التقييم</div>
                </div>
                <div style="background:#F9FAFB;border-radius:6px;padding:8px;">
                    <div style="font-size:18px;font-weight:700;color:#1A1A2E;"><?= (int) ($coach['total_sessions_conducted'] ?? 0) ?></div>
                    <div style="font-size:10px;color:#6B7280;">حصص</div>
                </div>
            </div>

            <div style="margin-top:10px;display:flex;gap:10px;font-size:11px;color:#6B7280;">
                <span>حضور: <?= number_format((float) ($coach['avg_attendance_rate'] ?? 0), 0) ?>%</span>
                <span>لاعبين: <?= (int) ($coach['total_players_trained'] ?? 0) ?></span>
                <span>استبقاء: <?= number_format((float) ($coach['player_retention_rate'] ?? 0), 0) ?>%</span>
            </div>
        </a>
    </div>
    <?php endforeach; ?>
</div>

<?php else: ?>
<div class="card" style="padding:60px 20px;text-align:center;">
    <div style="margin-bottom:15px;">
        <i data-lucide="bar-chart-3" 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;">لم يتم حساب أداء المدربين لهذا الشهر بعد.</p>
    <form method="POST" action="/coach-performance/calculate" style="display:inline;">
        <?= csrf_field() ?>
        <input type="hidden" name="month" value="<?= e($yearMonth) ?>">
        <button type="submit" class="btn btn-primary">
            <i data-lucide="calculator" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> حساب الأداء الآن
        </button>
    </form>
</div>
<?php endif; ?>

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