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

<?php $__template->section('page_actions'); ?>
<?php if (can('sa.player.manage')): ?>
<a href="/fitness-tests/create" class="btn btn-primary"><i data-lucide="plus" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> اختبار جديد</a>
<?php endif; ?>
<?php $__template->endSection(); ?>

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

<?php $currentLevel = $filters['fitness_level'] ?? ''; ?>

<!-- Fitness Level Filter Tabs -->
<div class="card" style="margin-bottom:20px;padding:0;">
    <div style="display:flex;align-items:center;gap:0;overflow-x:auto;border-bottom:2px solid #E5E7EB;">
        <a href="/fitness-tests"
           style="padding:12px 20px;font-size:14px;font-weight:600;text-decoration:none;border-bottom:3px solid <?= $currentLevel === '' ? '#0D7377' : 'transparent' ?>;color:<?= $currentLevel === '' ? '#0D7377' : '#6B7280' ?>;white-space:nowrap;">
            الكل
        </a>
        <?php foreach ($fitnessLevels as $lKey => $lLabel): ?>
        <?php $levelColor = PlayerFitnessTest::getFitnessLevelColor($lKey); ?>
        <a href="/fitness-tests?fitness_level=<?= e($lKey) ?>"
           style="padding:12px 20px;font-size:14px;font-weight:600;text-decoration:none;border-bottom:3px solid <?= $currentLevel === $lKey ? $levelColor : 'transparent' ?>;color:<?= $currentLevel === $lKey ? $levelColor : '#6B7280' ?>;white-space:nowrap;">
            <?= e($lLabel) ?>
        </a>
        <?php endforeach; ?>
    </div>
</div>

<!-- Search & Filter Bar -->
<div class="card" style="margin-bottom:20px;padding:15px;">
    <form method="GET" action="/fitness-tests" style="display:flex;gap:10px;align-items:end;flex-wrap:wrap;">
        <div style="flex:1;min-width:200px;">
            <label class="form-label" style="font-size:12px;">بحث</label>
            <input type="text" name="q" value="<?= e($filters['q'] ?? '') ?>" placeholder="ابحث بالاسم..." class="form-input">
        </div>
        <div style="min-width:130px;">
            <label class="form-label" style="font-size:12px;">نوع الاختبار</label>
            <select name="test_type" class="form-input">
                <option value="">الكل</option>
                <?php foreach ($testTypes as $key => $label): ?>
                <option value="<?= e($key) ?>" <?= ($filters['test_type'] ?? '') === $key ? 'selected' : '' ?>><?= e($label) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div style="min-width:130px;">
            <label class="form-label" style="font-size:12px;">من تاريخ</label>
            <input type="date" name="date_from" value="<?= e($filters['date_from'] ?? '') ?>" class="form-input">
        </div>
        <div style="min-width:130px;">
            <label class="form-label" style="font-size:12px;">إلى تاريخ</label>
            <input type="date" name="date_to" value="<?= e($filters['date_to'] ?? '') ?>" class="form-input">
        </div>
        <?php if ($currentLevel !== ''): ?>
        <input type="hidden" name="fitness_level" value="<?= e($currentLevel) ?>">
        <?php endif; ?>
        <button type="submit" class="btn btn-outline"><i data-lucide="search" style="width:16px;height:16px;vertical-align:middle;"></i> بحث</button>
        <a href="/fitness-tests" class="btn btn-sm btn-outline" style="color:#6B7280;">مسح</a>
    </form>
</div>

<!-- Tests Table -->
<?php if (!empty($tests)): ?>
<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;">#</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;">BMI</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
            $startIndex = (($pagination['current_page'] ?? 1) - 1) * ($pagination['per_page'] ?? 25);
            foreach ($tests as $i => $t):
                $level = $t['fitness_level'] ?? '';
                $levelColor = PlayerFitnessTest::getFitnessLevelColor($level);
                $levelLabel = $fitnessLevels[$level] ?? $level;
                $typeLabel = $testTypes[$t['test_type'] ?? ''] ?? ($t['test_type'] ?? '');
                $cleared = (int) ($t['cleared_for_activity'] ?? 0);
            ?>
            <tr style="border-bottom:1px solid #F3F4F6;" onmouseover="this.style.background='#F9FAFB'" onmouseout="this.style.background='transparent'">
                <td style="padding:12px 15px;color:#6B7280;"><?= $startIndex + $i + 1 ?></td>
                <td style="padding:12px 15px;font-weight:600;color:#1A1A2E;"><?= e($t['player_name'] ?? '') ?></td>
                <td style="padding:12px 15px;color:#374151;"><?= e($t['test_date'] ?? '') ?></td>
                <td style="padding:12px 15px;"><?= e($typeLabel) ?></td>
                <td style="padding:12px 15px;font-weight:600;">
                    <?= $t['overall_fitness_score'] !== null ? number_format((float) $t['overall_fitness_score'], 1) : '—' ?>
                </td>
                <td style="padding:12px 15px;">
                    <?php if ($level): ?>
                    <span style="display:inline-block;padding:2px 10px;border-radius:10px;font-size:11px;font-weight:600;background:<?= $levelColor ?>15;color:<?= $levelColor ?>;"><?= e($levelLabel) ?></span>
                    <?php else: ?>—<?php endif; ?>
                </td>
                <td style="padding:12px 15px;color:#374151;"><?= $t['bmi'] !== null ? number_format((float) $t['bmi'], 1) : '—' ?></td>
                <td style="padding:12px 15px;">
                    <?php if ($cleared): ?>
                    <span style="color:#059669;"><i data-lucide="check-circle" style="width:16px;height:16px;vertical-align:middle;"></i></span>
                    <?php else: ?>
                    <span style="color:#DC2626;"><i data-lucide="x-circle" style="width:16px;height:16px;vertical-align:middle;"></i></span>
                    <?php endif; ?>
                </td>
                <td style="padding:12px 15px;white-space:nowrap;">
                    <a href="/fitness-tests/<?= (int) $t['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 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="activity" 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;">
        <?php if (!empty($filters['q']) || !empty($filters['test_type']) || !empty($filters['fitness_level'])): ?>
            لا توجد نتائج مطابقة لبحثك.
        <?php else: ?>
            لم يتم تسجيل أي اختبارات لياقة حتى الآن.
        <?php endif; ?>
    </p>
    <a href="/fitness-tests/create" 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(); ?>
