<?php
$__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'); ?>

<?php
$levelColor = $summary['level_color'] ?? '#6B7280';
$levelLabel = $summary['level_label'] ?? '';
$compositeScore = $summary['composite_score'] ?? 0;
$latest = $summary['latest_metrics'];
?>

<!-- Coach Header -->
<div class="card" style="margin-bottom:20px;">
    <div style="padding:20px;display:flex;justify-content:space-between;align-items:center;">
        <div>
            <h2 style="margin:0 0 6px;font-size:20px;color:#1A1A2E;">
                <i data-lucide="bar-chart-3" style="width:22px;height:22px;vertical-align:middle;margin-left:8px;color:<?= $levelColor ?>;"></i>
                <?= e($coach->full_name_ar ?? '') ?>
            </h2>
            <div style="display:flex;gap:10px;align-items:center;">
                <span style="display:inline-block;padding:3px 12px;border-radius:10px;font-size:12px;font-weight:600;background:<?= $levelColor ?>15;color:<?= $levelColor ?>;"><?= e($levelLabel) ?></span>
                <span style="font-size:13px;color:#6B7280;">
                    <?= e($fromMonth) ?> — <?= e($toMonth) ?>
                </span>
            </div>
        </div>
        <div style="text-align:center;">
            <div style="width:90px;height:90px;border-radius:50%;border:5px solid <?= $levelColor ?>;display:flex;align-items:center;justify-content:center;">
                <span style="font-size:26px;font-weight:700;color:<?= $levelColor ?>;"><?= number_format($compositeScore, 0) ?>%</span>
            </div>
            <div style="font-size:11px;color:#6B7280;margin-top:6px;">الأداء الكلي</div>
        </div>
    </div>
</div>

<!-- Key Metrics Cards -->
<?php if ($latest): ?>
<div style="display:grid;grid-template-columns:repeat(auto-fit, minmax(150px, 1fr));gap:15px;margin-bottom:20px;">
    <div class="card" style="padding:15px;text-align:center;">
        <div style="font-size:24px;font-weight:700;color:#1A1A2E;"><?= (int) ($latest['total_sessions_conducted'] ?? 0) ?></div>
        <div style="font-size:11px;color:#6B7280;">حصص منفذة</div>
        <div style="font-size:10px;color:#9CA3AF;">من <?= (int) ($latest['total_sessions_assigned'] ?? 0) ?></div>
    </div>
    <div class="card" style="padding:15px;text-align:center;">
        <div style="font-size:24px;font-weight:700;color:#1e3a5f;"><?= number_format((float) ($latest['avg_attendance_rate'] ?? 0), 0) ?>%</div>
        <div style="font-size:11px;color:#6B7280;">نسبة الحضور</div>
    </div>
    <div class="card" style="padding:15px;text-align:center;">
        <div style="font-size:24px;font-weight:700;color:#F59E0B;">
            <?= $latest['avg_rating'] !== null ? number_format((float) $latest['avg_rating'], 1) : '—' ?>
        </div>
        <div style="font-size:11px;color:#6B7280;">التقييم</div>
        <div style="font-size:10px;color:#9CA3AF;">(<?= (int) ($latest['total_ratings_count'] ?? 0) ?> تقييم)</div>
    </div>
    <div class="card" style="padding:15px;text-align:center;">
        <div style="font-size:24px;font-weight:700;color:#2563EB;"><?= (int) ($latest['total_players_trained'] ?? 0) ?></div>
        <div style="font-size:11px;color:#6B7280;">لاعبين</div>
    </div>
    <div class="card" style="padding:15px;text-align:center;">
        <div style="font-size:24px;font-weight:700;color:#059669;"><?= number_format((float) ($latest['player_retention_rate'] ?? 0), 0) ?>%</div>
        <div style="font-size:11px;color:#6B7280;">الاستبقاء</div>
    </div>
    <div class="card" style="padding:15px;text-align:center;">
        <div style="font-size:24px;font-weight:700;color:#7C3AED;"><?= number_format((float) ($latest['revenue_generated'] ?? 0), 0) ?></div>
        <div style="font-size:11px;color:#6B7280;">الإيرادات (ج.م)</div>
    </div>
</div>
<?php endif; ?>

<!-- Trend Chart (Bar visualization) -->
<?php $trend = $report['trend'] ?? []; ?>
<?php if (!empty($trend['months'])): ?>
<div class="card" style="margin-bottom:20px;">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;">
        <h3 style="margin:0;font-size:15px;color:#374151;">تطور الأداء</h3>
    </div>
    <div style="padding:20px;">
        <!-- Sessions Trend -->
        <div style="margin-bottom:20px;">
            <div style="font-size:12px;color:#6B7280;margin-bottom:8px;">الحصص المنفذة</div>
            <div style="display:flex;align-items:end;gap:6px;height:60px;">
                <?php
                $maxSessions = !empty($trend['sessions']) ? max($trend['sessions']) : 1;
                foreach ($trend['months'] as $idx => $month):
                    $sessions = (int) ($trend['sessions'][$idx] ?? 0);
                    $height = $maxSessions > 0 ? ($sessions / $maxSessions) * 50 : 0;
                ?>
                <div style="flex:1;display:flex;flex-direction:column;align-items:center;gap:2px;">
                    <div style="font-size:9px;color:#6B7280;"><?= $sessions ?></div>
                    <div style="width:100%;height:<?= $height ?>px;background:#1e3a5f30;border-radius:3px 3px 0 0;border-top:2px solid #1e3a5f;min-height:2px;"></div>
                    <div style="font-size:8px;color:#9CA3AF;"><?= substr($month, 5) ?></div>
                </div>
                <?php endforeach; ?>
            </div>
        </div>

        <!-- Composite Score Trend -->
        <div>
            <div style="font-size:12px;color:#6B7280;margin-bottom:8px;">الدرجة الكلية (%)</div>
            <div style="display:flex;align-items:end;gap:6px;height:60px;">
                <?php foreach ($trend['months'] as $idx => $month):
                    $score = (float) ($trend['composite_scores'][$idx] ?? 0);
                    $height = ($score / 100) * 50;
                    $barColor = match(true) {
                        $score >= 85 => '#059669',
                        $score >= 70 => '#1e3a5f',
                        $score >= 55 => '#D97706',
                        default      => '#DC2626',
                    };
                ?>
                <div style="flex:1;display:flex;flex-direction:column;align-items:center;gap:2px;">
                    <div style="font-size:9px;color:#6B7280;"><?= number_format($score, 0) ?></div>
                    <div style="width:100%;height:<?= $height ?>px;background:<?= $barColor ?>30;border-radius:3px 3px 0 0;border-top:2px solid <?= $barColor ?>;min-height:2px;"></div>
                    <div style="font-size:8px;color:#9CA3AF;"><?= substr($month, 5) ?></div>
                </div>
                <?php endforeach; ?>
            </div>
        </div>
    </div>
</div>
<?php endif; ?>

<!-- Summary Period -->
<?php $summaryData = $report['summary'] ?? []; ?>
<?php if (!empty($summaryData)): ?>
<div class="card" style="margin-bottom:20px;">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;">
        <h3 style="margin:0;font-size:15px;color:#374151;">ملخص الفترة (<?= (int) ($summaryData['months_covered'] ?? 0) ?> أشهر)</h3>
    </div>
    <div style="padding:20px;">
        <table style="width:100%;font-size:14px;">
            <tr>
                <td style="padding:8px 0;color:#6B7280;">إجمالي الحصص المنفذة:</td>
                <td style="padding:8px 0;font-weight:600;"><?= (int) ($summaryData['total_sessions'] ?? 0) ?></td>
            </tr>
            <tr>
                <td style="padding:8px 0;color:#6B7280;">متوسط نسبة الحضور:</td>
                <td style="padding:8px 0;font-weight:600;"><?= number_format((float) ($summaryData['avg_attendance'] ?? 0), 1) ?>%</td>
            </tr>
            <tr>
                <td style="padding:8px 0;color:#6B7280;">متوسط التقييم:</td>
                <td style="padding:8px 0;font-weight:600;color:#F59E0B;">
                    <?= $summaryData['avg_rating'] !== null ? number_format((float) $summaryData['avg_rating'], 2) . ' / 5' : '—' ?>
                </td>
            </tr>
            <tr>
                <td style="padding:8px 0;color:#6B7280;">إجمالي الإيرادات:</td>
                <td style="padding:8px 0;font-weight:600;"><?= number_format((float) ($summaryData['total_revenue'] ?? 0), 2) ?> ج.م</td>
            </tr>
        </table>
    </div>
</div>
<?php endif; ?>

<!-- Recalculate button -->
<div class="card" style="padding:15px;text-align:center;">
    <form method="POST" action="/coach-performance/calculate" style="display:inline;">
        <?= csrf_field() ?>
        <input type="hidden" name="coach_id" value="<?= (int) $coach->id ?>">
        <input type="hidden" name="month" value="<?= e(date('Y-m')) ?>">
        <button type="submit" class="btn btn-outline">
            <i data-lucide="refresh-cw" style="width:14px;height:14px;vertical-align:middle;margin-left:4px;"></i> إعادة حساب الشهر الحالي
        </button>
    </form>
</div>

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