<?php
use App\Modules\PlayerAffairs\Models\PlayerFitnessTest;
$__template->layout('Layout.main');
?>
<?php $__template->section('title'); ?>تفاصيل اختبار اللياقة<?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<a href="/fitness-tests" 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
$level = $test->fitness_level ?? '';
$levelLabel = $fitnessLevels[$level] ?? $level;
$levelColor = PlayerFitnessTest::getFitnessLevelColor($level);
$typeLabel = $testTypes[$test->test_type ?? ''] ?? ($test->test_type ?? '');
$cleared = (int) ($test->cleared_for_activity ?? 0);
?>

<!-- Header Card -->
<div class="card" style="margin-bottom:20px;">
    <div style="padding:20px;display:flex;justify-content:space-between;align-items:start;">
        <div>
            <h2 style="margin:0 0 8px;font-size:20px;color:#1A1A2E;">
                <i data-lucide="activity" style="width:22px;height:22px;vertical-align:middle;margin-left:8px;color:#0D7377;"></i>
                اختبار لياقة - <?= e($typeLabel) ?>
            </h2>
            <div style="display:flex;gap:10px;align-items:center;flex-wrap:wrap;">
                <?php if ($level): ?>
                <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>
                <?php endif; ?>
                <span style="display:inline-block;padding:3px 12px;border-radius:10px;font-size:12px;font-weight:600;background:<?= $cleared ? '#05966915' : '#DC262615' ?>;color:<?= $cleared ? '#059669' : '#DC2626' ?>;">
                    <?= $cleared ? 'لائق للنشاط' : 'غير لائق' ?>
                </span>
                <?php if ($player): ?>
                <a href="/players/<?= (int) $player['id'] ?>" style="font-size:13px;color:#0D7377;text-decoration:none;">
                    <i data-lucide="user" style="width:14px;height:14px;vertical-align:middle;"></i>
                    <?= e($player['full_name_ar'] ?? '') ?>
                </a>
                <?php endif; ?>
            </div>
        </div>
        <?php if ($test->overall_fitness_score !== null): ?>
        <div style="text-align:center;">
            <div style="width:80px;height:80px;border-radius:50%;border:4px solid <?= $levelColor ?>;display:flex;align-items:center;justify-content:center;">
                <span style="font-size:22px;font-weight:700;color:<?= $levelColor ?>;"><?= number_format((float) $test->overall_fitness_score, 0) ?></span>
            </div>
            <div style="font-size:11px;color:#6B7280;margin-top:4px;">الدرجة الكلية</div>
        </div>
        <?php endif; ?>
    </div>
</div>

<div style="display:grid;grid-template-columns:2fr 1fr;gap:20px;">
    <!-- Main Content -->
    <div>
        <!-- Test Details -->
        <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;">
                <table style="width:100%;font-size:14px;">
                    <tr>
                        <td style="padding:8px 0;color:#6B7280;width:150px;">تاريخ الاختبار:</td>
                        <td style="padding:8px 0;font-weight:600;"><?= e($test->test_date ?? '') ?></td>
                    </tr>
                    <tr>
                        <td style="padding:8px 0;color:#6B7280;">نوع الاختبار:</td>
                        <td style="padding:8px 0;"><?= e($typeLabel) ?></td>
                    </tr>
                    <?php if ($discipline): ?>
                    <tr>
                        <td style="padding:8px 0;color:#6B7280;">النشاط:</td>
                        <td style="padding:8px 0;"><?= e($discipline['name_ar'] ?? '') ?></td>
                    </tr>
                    <?php endif; ?>
                    <?php if ($coach): ?>
                    <tr>
                        <td style="padding:8px 0;color:#6B7280;">المدرب:</td>
                        <td style="padding:8px 0;"><?= e($coach['full_name_ar'] ?? '') ?></td>
                    </tr>
                    <?php endif; ?>
                    <?php if ($test->tester_name): ?>
                    <tr>
                        <td style="padding:8px 0;color:#6B7280;">الفاحص:</td>
                        <td style="padding:8px 0;"><?= e($test->tester_name) ?></td>
                    </tr>
                    <?php endif; ?>
                    <?php if ($test->next_test_date): ?>
                    <tr>
                        <td style="padding:8px 0;color:#6B7280;">الاختبار القادم:</td>
                        <td style="padding:8px 0;"><?= e($test->next_test_date) ?></td>
                    </tr>
                    <?php endif; ?>
                </table>
            </div>
        </div>

        <!-- Body Measurements -->
        <?php if ($test->weight_kg || $test->height_cm || $test->bmi || $test->resting_heart_rate || $test->blood_pressure): ?>
        <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;display:grid;grid-template-columns:repeat(auto-fit, minmax(120px, 1fr));gap:15px;text-align:center;">
                <?php if ($test->weight_kg): ?>
                <div style="background:#F9FAFB;border-radius:8px;padding:15px;">
                    <div style="font-size:22px;font-weight:700;color:#1A1A2E;"><?= number_format((float) $test->weight_kg, 1) ?></div>
                    <div style="font-size:11px;color:#6B7280;">الوزن (كجم)</div>
                </div>
                <?php endif; ?>
                <?php if ($test->height_cm): ?>
                <div style="background:#F9FAFB;border-radius:8px;padding:15px;">
                    <div style="font-size:22px;font-weight:700;color:#1A1A2E;"><?= number_format((float) $test->height_cm, 1) ?></div>
                    <div style="font-size:11px;color:#6B7280;">الطول (سم)</div>
                </div>
                <?php endif; ?>
                <?php if ($test->bmi): ?>
                <div style="background:#F9FAFB;border-radius:8px;padding:15px;">
                    <div style="font-size:22px;font-weight:700;color:#1A1A2E;"><?= number_format((float) $test->bmi, 1) ?></div>
                    <div style="font-size:11px;color:#6B7280;">BMI</div>
                </div>
                <?php endif; ?>
                <?php if ($test->resting_heart_rate): ?>
                <div style="background:#F9FAFB;border-radius:8px;padding:15px;">
                    <div style="font-size:22px;font-weight:700;color:#1A1A2E;"><?= (int) $test->resting_heart_rate ?></div>
                    <div style="font-size:11px;color:#6B7280;">نبض الراحة</div>
                </div>
                <?php endif; ?>
                <?php if ($test->blood_pressure): ?>
                <div style="background:#F9FAFB;border-radius:8px;padding:15px;">
                    <div style="font-size:22px;font-weight:700;color:#1A1A2E;" dir="ltr"><?= e($test->blood_pressure) ?></div>
                    <div style="font-size:11px;color:#6B7280;">ضغط الدم</div>
                </div>
                <?php endif; ?>
            </div>
        </div>
        <?php endif; ?>

        <!-- Detailed Results -->
        <?php
        $results = [];
        if (!empty($test->results_json)) {
            $results = json_decode($test->results_json, true) ?: [];
        }
        ?>
        <?php if (!empty($results)): ?>
        <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;">
                <table style="width:100%;font-size:14px;border-collapse:collapse;">
                    <?php foreach ($results as $key => $value): ?>
                    <tr style="border-bottom:1px solid #F3F4F6;">
                        <td style="padding:10px 0;font-weight:600;color:#374151;"><?= e($key) ?></td>
                        <td style="padding:10px 0;color:#1A1A2E;"><?= e($value) ?></td>
                    </tr>
                    <?php endforeach; ?>
                </table>
            </div>
        </div>
        <?php endif; ?>

        <!-- Recommendations -->
        <?php if ($test->recommendations_ar): ?>
        <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;font-size:14px;color:#4B5563;line-height:1.7;">
                <?= e($test->recommendations_ar) ?>
            </div>
        </div>
        <?php endif; ?>
    </div>

    <!-- Sidebar -->
    <div>
        <!-- Player Summary -->
        <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;">
                <div style="text-align:center;margin-bottom:15px;">
                    <div style="font-size:14px;font-weight:600;color:#1A1A2E;"><?= e($player['full_name_ar'] ?? '') ?></div>
                    <div style="font-size:12px;color:#6B7280;"><?= e($player['registration_serial'] ?? '') ?></div>
                </div>
                <div style="display:grid;grid-template-columns:1fr 1fr;gap:12px;text-align:center;">
                    <div style="background:#F9FAFB;border-radius:8px;padding:10px;">
                        <div style="font-size:18px;font-weight:700;color:#1A1A2E;"><?= (int) ($summary['total_tests'] ?? 0) ?></div>
                        <div style="font-size:10px;color:#6B7280;">اختبارات</div>
                    </div>
                    <div style="background:#F9FAFB;border-radius:8px;padding:10px;">
                        <div style="font-size:18px;font-weight:700;color:<?= ($summary['is_fit'] ?? false) ? '#059669' : '#DC2626' ?>;">
                            <?= ($summary['is_fit'] ?? false) ? 'لائق' : 'غير لائق' ?>
                        </div>
                        <div style="font-size:10px;color:#6B7280;">الحالة الحالية</div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Progression Chart (simplified) -->
        <?php if (count($progression['scores'] ?? []) > 1): ?>
        <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;">
                <div style="display:flex;align-items:end;gap:4px;height:100px;">
                    <?php
                    $scores = array_filter($progression['scores'], fn($s) => $s !== null);
                    $maxScore = !empty($scores) ? max($scores) : 100;
                    foreach ($progression['scores'] as $idx => $score):
                        if ($score === null) continue;
                        $height = ($score / $maxScore) * 80;
                        $barColor = PlayerFitnessTest::getFitnessLevelColor(PlayerFitnessTest::determineFitnessLevel($score));
                    ?>
                    <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:4px 4px 0 0;border-top:3px solid <?= $barColor ?>;"></div>
                    </div>
                    <?php endforeach; ?>
                </div>
                <div style="text-align:center;margin-top:8px;font-size:10px;color:#9CA3AF;">آخر <?= count($scores) ?> اختبارات</div>
            </div>
        </div>
        <?php endif; ?>

        <!-- Next Test -->
        <?php if ($summary['next_test_due'] ?? null): ?>
        <div class="card">
            <div style="padding:15px 20px;text-align:center;">
                <i data-lucide="calendar" style="width:24px;height:24px;color:#D97706;margin-bottom:8px;"></i>
                <div style="font-size:13px;color:#6B7280;">الاختبار القادم</div>
                <div style="font-size:16px;font-weight:600;color:#1A1A2E;margin-top:4px;"><?= e($summary['next_test_due']) ?></div>
            </div>
        </div>
        <?php endif; ?>
    </div>
</div>

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