<?php
$__template->layout('Layout.main');
?>
<?php $__template->section('title'); ?>اختبار لياقة جديد<?php $__template->endSection(); ?>

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

<div class="card" style="max-width:800px;margin:0 auto;">
    <div style="padding:20px;border-bottom:1px solid #E5E7EB;">
        <h2 style="margin:0;font-size:18px;color:#1A1A2E;">
            <i data-lucide="activity" style="width:20px;height:20px;vertical-align:middle;margin-left:8px;color:#0D7377;"></i>
            تسجيل اختبار لياقة جديد
        </h2>
    </div>

    <form method="POST" action="/fitness-tests" style="padding:20px;">
        <?= csrf_field() ?>

        <!-- Player Selection -->
        <div style="margin-bottom:20px;">
            <label class="form-label">اللاعب <span style="color:#DC2626;">*</span></label>
            <select name="player_id" class="form-input" required>
                <option value="">-- اختر اللاعب --</option>
                <?php foreach ($players as $p): ?>
                <option value="<?= (int) $p['id'] ?>" <?= (old('player_id') ?? $selectedPlayerId) == $p['id'] ? 'selected' : '' ?>><?= e($p['full_name_ar']) ?> (<?= e($p['registration_serial'] ?? '') ?>)</option>
                <?php endforeach; ?>
            </select>
        </div>

        <div style="display:grid;grid-template-columns:1fr 1fr;gap:15px;margin-bottom:20px;">
            <!-- Test Date -->
            <div>
                <label class="form-label">تاريخ الاختبار <span style="color:#DC2626;">*</span></label>
                <input type="date" name="test_date" value="<?= e(old('test_date') ?? date('Y-m-d')) ?>" class="form-input" required>
            </div>

            <!-- Test Type -->
            <div>
                <label class="form-label">نوع الاختبار <span style="color:#DC2626;">*</span></label>
                <select name="test_type" class="form-input" required>
                    <option value="">-- اختر --</option>
                    <?php foreach ($testTypes as $key => $label): ?>
                    <option value="<?= e($key) ?>" <?= old('test_type') === $key ? 'selected' : '' ?>><?= e($label) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
        </div>

        <div style="display:grid;grid-template-columns:1fr 1fr;gap:15px;margin-bottom:20px;">
            <!-- Discipline -->
            <div>
                <label class="form-label">النشاط الرياضي</label>
                <select name="discipline_id" class="form-input">
                    <option value="">-- اختياري --</option>
                    <?php foreach ($disciplines as $d): ?>
                    <option value="<?= (int) $d['id'] ?>" <?= old('discipline_id') == $d['id'] ? 'selected' : '' ?>><?= e($d['name_ar']) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>

            <!-- Coach -->
            <div>
                <label class="form-label">المدرب المسؤول</label>
                <select name="coach_id" class="form-input">
                    <option value="">-- اختياري --</option>
                    <?php foreach ($coaches as $c): ?>
                    <option value="<?= (int) $c['id'] ?>" <?= old('coach_id') == $c['id'] ? 'selected' : '' ?>><?= e($c['full_name_ar']) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
        </div>

        <!-- Body Measurements -->
        <div style="background:#F9FAFB;border-radius:8px;padding:15px;margin-bottom:20px;">
            <h4 style="margin:0 0 12px;font-size:14px;color:#374151;">القياسات الجسدية</h4>
            <div style="display:grid;grid-template-columns:1fr 1fr 1fr 1fr;gap:12px;">
                <div>
                    <label class="form-label" style="font-size:12px;">الوزن (كجم)</label>
                    <input type="number" name="weight_kg" value="<?= e(old('weight_kg') ?? '') ?>" class="form-input" step="0.1" min="10" max="200">
                </div>
                <div>
                    <label class="form-label" style="font-size:12px;">الطول (سم)</label>
                    <input type="number" name="height_cm" value="<?= e(old('height_cm') ?? '') ?>" class="form-input" step="0.1" min="50" max="250">
                </div>
                <div>
                    <label class="form-label" style="font-size:12px;">معدل نبض الراحة</label>
                    <input type="number" name="resting_heart_rate" value="<?= e(old('resting_heart_rate') ?? '') ?>" class="form-input" min="30" max="200">
                </div>
                <div>
                    <label class="form-label" style="font-size:12px;">ضغط الدم</label>
                    <input type="text" name="blood_pressure" value="<?= e(old('blood_pressure') ?? '') ?>" class="form-input" placeholder="120/80" dir="ltr">
                </div>
            </div>
        </div>

        <!-- Overall Score -->
        <div style="display:grid;grid-template-columns:1fr 1fr;gap:15px;margin-bottom:20px;">
            <div>
                <label class="form-label">الدرجة الكلية (0-100)</label>
                <input type="number" name="overall_fitness_score" value="<?= e(old('overall_fitness_score') ?? '') ?>" class="form-input" min="0" max="100" step="0.1">
            </div>
            <div>
                <label class="form-label">لائق للنشاط</label>
                <select name="cleared_for_activity" class="form-input">
                    <option value="">-- تحديد تلقائي --</option>
                    <option value="1" <?= old('cleared_for_activity') === '1' ? 'selected' : '' ?>>نعم - لائق</option>
                    <option value="0" <?= old('cleared_for_activity') === '0' ? 'selected' : '' ?>>لا - غير لائق</option>
                </select>
            </div>
        </div>

        <!-- Dynamic Test Results -->
        <div style="background:#F9FAFB;border-radius:8px;padding:15px;margin-bottom:20px;">
            <h4 style="margin:0 0 12px;font-size:14px;color:#374151;">نتائج الاختبارات التفصيلية</h4>
            <div id="resultsContainer">
                <div class="result-row" style="display:flex;gap:10px;margin-bottom:8px;">
                    <input type="text" name="result_keys[]" placeholder="اسم الاختبار (مثل: جري 100م)" class="form-input" style="flex:1;">
                    <input type="text" name="result_values[]" placeholder="النتيجة (مثل: 12.5 ثانية)" class="form-input" style="flex:1;">
                    <button type="button" onclick="this.parentElement.remove()" class="btn btn-sm btn-outline" style="color:#DC2626;padding:8px;">
                        <i data-lucide="x" style="width:14px;height:14px;"></i>
                    </button>
                </div>
            </div>
            <button type="button" onclick="addResultRow()" class="btn btn-sm btn-outline" style="margin-top:8px;">
                <i data-lucide="plus" style="width:14px;height:14px;vertical-align:middle;"></i> إضافة اختبار
            </button>
        </div>

        <!-- Tester & Next Date -->
        <div style="display:grid;grid-template-columns:1fr 1fr;gap:15px;margin-bottom:20px;">
            <div>
                <label class="form-label">اسم الفاحص</label>
                <input type="text" name="tester_name" value="<?= e(old('tester_name') ?? '') ?>" class="form-input" placeholder="اسم الطبيب / المختص">
            </div>
            <div>
                <label class="form-label">موعد الاختبار القادم</label>
                <input type="date" name="next_test_date" value="<?= e(old('next_test_date') ?? '') ?>" class="form-input">
            </div>
        </div>

        <!-- Recommendations -->
        <div style="margin-bottom:20px;">
            <label class="form-label">التوصيات</label>
            <textarea name="recommendations_ar" class="form-input" rows="3" placeholder="توصيات وملاحظات..."><?= e(old('recommendations_ar') ?? '') ?></textarea>
        </div>

        <!-- Actions -->
        <div style="display:flex;gap:10px;justify-content:flex-start;padding-top:15px;border-top:1px solid #E5E7EB;">
            <button type="submit" class="btn btn-primary">
                <i data-lucide="save" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> حفظ الاختبار
            </button>
            <a href="/fitness-tests" class="btn btn-outline">إلغاء</a>
        </div>
    </form>
</div>

<script>
function addResultRow() {
    var container = document.getElementById('resultsContainer');
    var row = document.createElement('div');
    row.className = 'result-row';
    row.style.cssText = 'display:flex;gap:10px;margin-bottom:8px;';
    row.innerHTML = '<input type="text" name="result_keys[]" placeholder="اسم الاختبار" class="form-input" style="flex:1;">' +
        '<input type="text" name="result_values[]" placeholder="النتيجة" class="form-input" style="flex:1;">' +
        '<button type="button" onclick="this.parentElement.remove()" class="btn btn-sm btn-outline" style="color:#DC2626;padding:8px;"><i data-lucide="x" style="width:14px;height:14px;"></i></button>';
    container.appendChild(row);
    if (typeof lucide !== 'undefined') lucide.createIcons();
}

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