<?php
use App\Modules\PlayerAffairs\Models\Player;
use App\Modules\PlayerAffairs\Models\PlayerMedicalRecord;
use App\Modules\PlayerAffairs\Models\AcademyEnrollment;
$__template->layout('Layout.main');
?>
<?php $__template->section('title'); ?><?= e($player->full_name_ar) ?><?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<a href="/players/<?= (int) $player->id ?>/edit" class="btn btn-outline"><i data-lucide="edit-3" style="width:15px;height:15px;vertical-align:middle;margin-left:4px;"></i> تعديل</a>
<a href="/players" class="btn btn-outline"><i data-lucide="arrow-right" style="width:15px;height:15px;vertical-align:middle;margin-left:4px;"></i> العودة للقائمة</a>
<?php $__template->endSection(); ?>

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

<?php
$cardStatus = $player->card_status ?? 'inactive';
$medicalStatus = $player->medical_status ?? 'pending';
$cardColor = Player::getCardStatusColor($cardStatus);
$cardLabel = Player::getCardStatusLabel($cardStatus);
$medicalColor = Player::getMedicalStatusColor($medicalStatus);
$medicalLabel = Player::getMedicalStatusLabel($medicalStatus);
$playerType = $player->player_type ?? '';
$typeLabel = ($playerTypes[$playerType] ?? $playerType);
$typeColor = $playerType === 'member' ? '#0284C7' : '#7C3AED';
$age = $player->getAge();
$recordTypes = PlayerMedicalRecord::getRecordTypes();
$resultTypes = PlayerMedicalRecord::getResults();
$enrollmentStatuses = AcademyEnrollment::getStatuses();
?>

<!-- Header Card -->
<div class="card" style="margin-bottom:20px;padding:25px;">
    <div style="display:flex;justify-content:space-between;align-items:start;">
        <div>
            <div style="display:flex;align-items:center;gap:12px;margin-bottom:8px;flex-wrap:wrap;">
                <h2 style="margin:0;font-size:22px;font-weight:700;color:#1A1A2E;"><?= e($player->full_name_ar) ?></h2>
                <span style="display:inline-block;padding:4px 12px;border-radius:10px;font-size:12px;font-weight:600;background:<?= $typeColor ?>15;color:<?= $typeColor ?>;"><?= e($typeLabel) ?></span>
                <span style="display:inline-block;padding:4px 12px;border-radius:10px;font-size:12px;font-weight:600;background:<?= $cardColor ?>15;color:<?= $cardColor ?>;"><?= e($cardLabel) ?></span>
            </div>
            <?php if ($player->full_name_en): ?>
            <div style="font-size:14px;color:#6B7280;margin-bottom:8px;"><?= e($player->full_name_en) ?></div>
            <?php endif; ?>
            <div style="display:flex;gap:15px;flex-wrap:wrap;font-size:13px;color:#6B7280;margin-top:10px;">
                <?php if ($player->registration_serial): ?>
                <span style="display:inline-flex;align-items:center;gap:4px;">
                    <i data-lucide="hash" style="width:14px;height:14px;"></i>
                    <code style="font-size:12px;background:#F3F4F6;padding:2px 8px;border-radius:4px;color:#0D7377;font-weight:600;"><?= e($player->registration_serial) ?></code>
                </span>
                <?php endif; ?>
                <?php if ($player->activity_id_number): ?>
                <span style="display:inline-flex;align-items:center;gap:4px;">
                    <i data-lucide="id-card" style="width:14px;height:14px;"></i>
                    رقم النشاط: <strong style="color:#374151;"><?= e($player->activity_id_number) ?></strong>
                </span>
                <?php endif; ?>
            </div>
        </div>
        <div style="text-align:left;">
            <span style="display:inline-block;padding:4px 12px;border-radius:10px;font-size:12px;font-weight:600;background:<?= $medicalColor ?>15;color:<?= $medicalColor ?>;"><?= e($medicalLabel) ?></span>
            <?php if ($player->medical_expiry_date): ?>
            <div style="margin-top:6px;font-size:11px;color:#9CA3AF;">انتهاء: <?= e($player->medical_expiry_date) ?></div>
            <?php endif; ?>
        </div>
    </div>
</div>

<!-- Stats Row -->
<div style="display:grid;grid-template-columns:repeat(4, 1fr);gap:15px;margin-bottom:20px;">
    <div class="card" style="padding:20px;text-align:center;">
        <div style="font-size:28px;font-weight:700;color:#0D7377;"><?= $age !== null ? $age : '—' ?></div>
        <div style="font-size:12px;color:#6B7280;margin-top:4px;">العمر (سنة)</div>
    </div>
    <div class="card" style="padding:20px;text-align:center;">
        <div style="font-size:14px;font-weight:700;color:<?= $cardColor ?>;"><?= e($cardLabel) ?></div>
        <div style="font-size:12px;color:#6B7280;margin-top:4px;">حالة الكارنيه</div>
    </div>
    <div class="card" style="padding:20px;text-align:center;">
        <div style="font-size:28px;font-weight:700;color:#7C3AED;"><?= count($disciplines) ?></div>
        <div style="font-size:12px;color:#6B7280;margin-top:4px;">الألعاب</div>
    </div>
    <div class="card" style="padding:20px;text-align:center;">
        <div style="font-size:28px;font-weight:700;color:#0284C7;"><?= count($enrollments) ?></div>
        <div style="font-size:12px;color:#6B7280;margin-top:4px;">التسجيلات</div>
    </div>
</div>

<!-- Personal Info -->
<div style="display:grid;grid-template-columns:1fr 1fr;gap:20px;margin-bottom:20px;">
    <div class="card">
        <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;display:flex;align-items:center;gap:8px;">
            <i data-lucide="user" style="width:18px;height:18px;color:#0D7377;"></i>
            <h3 style="margin:0;color:#0D7377;font-size:15px;">البيانات الشخصية</h3>
        </div>
        <div style="padding:15px 20px;">
            <table style="width:100%;font-size:13px;">
                <tr><td style="padding:6px 0;color:#6B7280;width:40%;">الرقم القومي</td><td style="padding:6px 0;direction:ltr;text-align:right;font-weight:600;"><?= e($player->national_id ?? '—') ?></td></tr>
                <tr><td style="padding:6px 0;color:#6B7280;">رقم جواز السفر</td><td style="padding:6px 0;direction:ltr;text-align:right;"><?= e($player->passport_number ?? '—') ?></td></tr>
                <tr><td style="padding:6px 0;color:#6B7280;">تاريخ الميلاد</td><td style="padding:6px 0;"><?= e($player->date_of_birth ?? '—') ?></td></tr>
                <tr><td style="padding:6px 0;color:#6B7280;">النوع</td><td style="padding:6px 0;"><?= e($genders[$player->gender] ?? $player->gender ?? '—') ?></td></tr>
            </table>
        </div>
    </div>
    <div class="card">
        <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;display:flex;align-items:center;gap:8px;">
            <i data-lucide="phone" style="width:18px;height:18px;color:#0D7377;"></i>
            <h3 style="margin:0;color:#0D7377;font-size:15px;">بيانات التواصل</h3>
        </div>
        <div style="padding:15px 20px;">
            <table style="width:100%;font-size:13px;">
                <tr><td style="padding:6px 0;color:#6B7280;width:40%;">الهاتف</td><td style="padding:6px 0;direction:ltr;text-align:right;"><?= e($player->phone ?? '—') ?></td></tr>
                <tr><td style="padding:6px 0;color:#6B7280;">البريد الإلكتروني</td><td style="padding:6px 0;direction:ltr;text-align:right;"><?= e($player->email ?? '—') ?></td></tr>
                <tr><td style="padding:6px 0;color:#6B7280;">العنوان</td><td style="padding:6px 0;"><?= e($player->address ?? '—') ?></td></tr>
            </table>
        </div>
    </div>
</div>

<!-- Guardian Info (if exists) -->
<?php if (!empty($player->guardian_name)): ?>
<div class="card" style="margin-bottom:20px;">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;display:flex;align-items:center;gap:8px;">
        <i data-lucide="shield" style="width:18px;height:18px;color:#D97706;"></i>
        <h3 style="margin:0;color:#D97706;font-size:15px;">بيانات ولي الأمر</h3>
    </div>
    <div style="padding:15px 20px;">
        <div style="display:grid;grid-template-columns:1fr 1fr;gap:15px;">
            <table style="width:100%;font-size:13px;">
                <tr><td style="padding:6px 0;color:#6B7280;width:40%;">اسم ولي الأمر</td><td style="padding:6px 0;font-weight:600;"><?= e($player->guardian_name) ?></td></tr>
                <tr><td style="padding:6px 0;color:#6B7280;">هاتف ولي الأمر</td><td style="padding:6px 0;direction:ltr;text-align:right;"><?= e($player->guardian_phone ?? '—') ?></td></tr>
            </table>
            <table style="width:100%;font-size:13px;">
                <tr><td style="padding:6px 0;color:#6B7280;width:40%;">الرقم القومي</td><td style="padding:6px 0;direction:ltr;text-align:right;"><?= e($player->guardian_national_id ?? '—') ?></td></tr>
                <tr><td style="padding:6px 0;color:#6B7280;">صلة القرابة</td><td style="padding:6px 0;">
                    <?php
                    $guardianRelationships = Player::getGuardianRelationships();
                    echo e($guardianRelationships[$player->guardian_relationship] ?? $player->guardian_relationship ?? '—');
                    ?>
                </td></tr>
            </table>
        </div>
    </div>
</div>
<?php endif; ?>

<!-- Education Info (if exists) -->
<?php if (!empty($player->school_name)): ?>
<div class="card" style="margin-bottom:20px;">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;display:flex;align-items:center;gap:8px;">
        <i data-lucide="graduation-cap" style="width:18px;height:18px;color:#0284C7;"></i>
        <h3 style="margin:0;color:#0284C7;font-size:15px;">البيانات التعليمية</h3>
    </div>
    <div style="padding:15px 20px;">
        <table style="width:100%;font-size:13px;">
            <tr><td style="padding:6px 0;color:#6B7280;width:25%;">اسم المدرسة</td><td style="padding:6px 0;font-weight:600;"><?= e($player->school_name) ?></td></tr>
            <?php if ($player->school_grade): ?>
            <tr><td style="padding:6px 0;color:#6B7280;">الصف الدراسي</td><td style="padding:6px 0;"><?= e($player->school_grade) ?></td></tr>
            <?php endif; ?>
        </table>
    </div>
</div>
<?php endif; ?>

<!-- Notes -->
<?php if (!empty($player->notes)): ?>
<div class="card" style="margin-bottom:20px;">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;display:flex;align-items:center;gap:8px;">
        <i data-lucide="file-text" style="width:18px;height:18px;color:#6B7280;"></i>
        <h3 style="margin:0;color:#6B7280;font-size:15px;">ملاحظات</h3>
    </div>
    <div style="padding:15px 20px;font-size:14px;color:#374151;line-height:1.7;"><?= e($player->notes) ?></div>
</div>
<?php endif; ?>

<!-- Card Management -->
<div class="card" style="margin-bottom:20px;">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;display:flex;align-items:center;gap:8px;">
        <i data-lucide="credit-card" style="width:18px;height:18px;color:#0D7377;"></i>
        <h3 style="margin:0;color:#0D7377;font-size:15px;">إدارة الكارنيه</h3>
    </div>
    <div style="padding:20px;">
        <div style="display:flex;align-items:center;gap:15px;margin-bottom:15px;">
            <span style="font-size:14px;color:#6B7280;">الحالة الحالية:</span>
            <span style="display:inline-block;padding:4px 12px;border-radius:10px;font-size:12px;font-weight:600;background:<?= $cardColor ?>15;color:<?= $cardColor ?>;"><?= e($cardLabel) ?></span>
        </div>

        <div style="display:flex;gap:10px;flex-wrap:wrap;">
            <?php if (in_array($cardStatus, ['inactive', 'suspended', 'revoked'])): ?>
            <form method="POST" action="/players/<?= (int) $player->id ?>/activate-card" style="display:inline;">
                <?= csrf_field() ?>
                <button type="submit" class="btn btn-primary" style="font-size:13px;" onclick="return confirm('هل أنت متأكد من تفعيل الكارنيه؟')">
                    <i data-lucide="check-circle" style="width:14px;height:14px;vertical-align:middle;margin-left:4px;"></i> تفعيل الكارنيه
                </button>
            </form>
            <?php endif; ?>

            <?php if ($cardStatus === 'active'): ?>
            <button type="button" class="btn btn-outline" style="font-size:13px;color:#D97706;border-color:#D97706;" onclick="document.getElementById('suspendCardForm').style.display = document.getElementById('suspendCardForm').style.display === 'none' ? 'block' : 'none';">
                <i data-lucide="pause-circle" style="width:14px;height:14px;vertical-align:middle;margin-left:4px;"></i> إيقاف الكارنيه
            </button>
            <form method="POST" action="/players/<?= (int) $player->id ?>/revoke-card" style="display:inline;">
                <?= csrf_field() ?>
                <button type="submit" class="btn btn-danger" style="font-size:13px;" onclick="return confirm('هل أنت متأكد من إلغاء الكارنيه؟ هذا الإجراء خطير.')">
                    <i data-lucide="x-circle" style="width:14px;height:14px;vertical-align:middle;margin-left:4px;"></i> إلغاء الكارنيه
                </button>
            </form>
            <?php endif; ?>
        </div>

        <?php if ($cardStatus === 'active'): ?>
        <div id="suspendCardForm" style="display:none;margin-top:15px;padding:15px;background:#FFF7ED;border:1px solid #FDE68A;border-radius:8px;">
            <form method="POST" action="/players/<?= (int) $player->id ?>/suspend-card">
                <?= csrf_field() ?>
                <div class="form-group" style="margin-bottom:10px;">
                    <label class="form-label" style="font-size:12px;">سبب الإيقاف</label>
                    <textarea name="reason" class="form-input" rows="2" placeholder="أدخل سبب إيقاف الكارنيه..."></textarea>
                </div>
                <button type="submit" class="btn btn-outline" style="font-size:13px;color:#D97706;border-color:#D97706;" onclick="return confirm('هل أنت متأكد من إيقاف الكارنيه؟')">
                    <i data-lucide="check" style="width:14px;height:14px;vertical-align:middle;margin-left:4px;"></i> تأكيد الإيقاف
                </button>
            </form>
        </div>
        <?php endif; ?>
    </div>
</div>

<!-- Disciplines Table -->
<div class="card" style="margin-bottom:20px;">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;display:flex;align-items:center;gap:8px;">
        <i data-lucide="activity" style="width:18px;height:18px;color:#7C3AED;"></i>
        <h3 style="margin:0;color:#7C3AED;font-size:15px;">الألعاب الرياضية</h3>
    </div>
    <?php if (!empty($disciplines)): ?>
    <div style="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>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($disciplines as $disc):
                    $dStatus = $disc['status'] ?? 'active';
                    $dStatusColor = $dStatus === 'active' ? '#059669' : '#D97706';
                    $dStatusLabel = $dStatus === 'active' ? 'نشط' : ($dStatus === 'suspended' ? 'موقف' : $dStatus);
                ?>
                <tr style="border-bottom:1px solid #F3F4F6;">
                    <td style="padding:12px 15px;font-weight:600;"><?= e($disc['discipline_name'] ?? '—') ?></td>
                    <td style="padding:12px 15px;"><?= e($disc['academy_name'] ?? '—') ?></td>
                    <td style="padding:12px 15px;direction:ltr;text-align:right;"><?= e($disc['season'] ?? '—') ?></td>
                    <td style="padding:12px 15px;">
                        <span style="display:inline-block;padding:4px 12px;border-radius:10px;font-size:12px;font-weight:600;background:<?= $dStatusColor ?>15;color:<?= $dStatusColor ?>;"><?= e($dStatusLabel) ?></span>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <?php else: ?>
    <div style="padding:30px;text-align:center;color:#9CA3AF;font-size:14px;">
        <i data-lucide="info" style="width:20px;height:20px;margin-bottom:8px;"></i>
        <div>لا توجد ألعاب رياضية مسجلة</div>
    </div>
    <?php endif; ?>
</div>

<!-- Enrollments Table -->
<div class="card" style="margin-bottom:20px;">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;display:flex;align-items:center;justify-content:space-between;">
        <div style="display:flex;align-items:center;gap:8px;">
            <i data-lucide="book-open" style="width:18px;height:18px;color:#0284C7;"></i>
            <h3 style="margin:0;color:#0284C7;font-size:15px;">التسجيلات في الأكاديميات</h3>
        </div>
        <button type="button" class="btn btn-sm btn-outline" onclick="toggleEnrollForm()">
            <i data-lucide="plus" style="width:14px;height:14px;vertical-align:middle;"></i> تسجيل جديد
        </button>
    </div>

    <!-- Enrollment Inline Form (hidden by default) -->
    <div id="enrollForm" style="display:none;padding:20px;background:#F9FAFB;border-bottom:1px solid #E5E7EB;">
        <form method="POST" action="/players/<?= (int) $player->id ?>/enroll">
            <?= csrf_field() ?>
            <div style="display:grid;grid-template-columns:1fr 1fr 1fr 1fr;gap:15px;">
                <div class="form-group" style="margin:0;">
                    <label class="form-label" style="font-size:12px;">رقم الأكاديمية <span style="color:#DC2626;">*</span></label>
                    <input type="number" name="academy_id" class="form-input" required min="1" style="direction:ltr;text-align:left;" placeholder="رقم الأكاديمية">
                </div>
                <div class="form-group" style="margin:0;">
                    <label class="form-label" style="font-size:12px;">رقم المستوى <span style="color:#DC2626;">*</span></label>
                    <input type="number" name="level_id" class="form-input" required min="1" style="direction:ltr;text-align:left;" placeholder="رقم المستوى">
                </div>
                <div class="form-group" style="margin:0;">
                    <label class="form-label" style="font-size:12px;">رقم الجدول</label>
                    <input type="number" name="schedule_id" class="form-input" min="1" style="direction:ltr;text-align:left;" placeholder="رقم الجدول">
                </div>
                <div class="form-group" style="margin:0;">
                    <label class="form-label" style="font-size:12px;">الموسم</label>
                    <input type="text" name="season" class="form-input" placeholder="مثال: 2025-2026" style="direction:ltr;text-align:left;">
                </div>
            </div>
            <div style="margin-top:15px;display:flex;gap:8px;">
                <button type="submit" class="btn btn-primary" style="font-size:13px;padding:8px 20px;">
                    <i data-lucide="check" style="width:14px;height:14px;vertical-align:middle;margin-left:4px;"></i> تسجيل
                </button>
                <button type="button" class="btn btn-outline" style="font-size:13px;padding:8px 20px;" onclick="toggleEnrollForm()">إلغاء</button>
            </div>
        </form>
    </div>

    <?php if (!empty($enrollments)): ?>
    <div style="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>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($enrollments as $enroll):
                    $eStatus = $enroll['status'] ?? 'active';
                    $eStatusLabel = AcademyEnrollment::getStatusLabel($eStatus);
                    $eStatusColor = AcademyEnrollment::getStatusColor($eStatus);
                ?>
                <tr style="border-bottom:1px solid #F3F4F6;">
                    <td style="padding:12px 15px;font-weight:600;"><?= e($enroll['academy_name'] ?? '—') ?></td>
                    <td style="padding:12px 15px;"><?= e($enroll['level_name'] ?? '—') ?></td>
                    <td style="padding:12px 15px;direction:ltr;text-align:right;"><?= e($enroll['season'] ?? '—') ?></td>
                    <td style="padding:12px 15px;"><?= e($enroll['enrolled_at'] ?? '—') ?></td>
                    <td style="padding:12px 15px;">
                        <span style="display:inline-block;padding:4px 12px;border-radius:10px;font-size:12px;font-weight:600;background:<?= $eStatusColor ?>15;color:<?= $eStatusColor ?>;"><?= e($eStatusLabel) ?></span>
                    </td>
                    <td style="padding:12px 15px;">
                        <?php if ($eStatus === 'active'): ?>
                        <button type="button" class="btn btn-sm" style="color:#DC2626;padding:4px 10px;border:1px solid #FCA5A5;border-radius:6px;background:#FEF2F2;cursor:pointer;font-size:12px;" onclick="toggleDropForm(<?= (int) ($enroll['id'] ?? 0) ?>)">
                            <i data-lucide="log-out" style="width:13px;height:13px;vertical-align:middle;"></i> انسحاب
                        </button>
                        <div id="dropForm-<?= (int) ($enroll['id'] ?? 0) ?>" style="display:none;margin-top:8px;">
                            <form method="POST" action="/players/<?= (int) $player->id ?>/drop-enrollment">
                                <?= csrf_field() ?>
                                <input type="hidden" name="enrollment_id" value="<?= (int) ($enroll['id'] ?? 0) ?>">
                                <textarea name="dropped_reason" class="form-input" rows="2" placeholder="سبب الانسحاب..." style="font-size:12px;margin-bottom:6px;"></textarea>
                                <button type="submit" class="btn btn-sm btn-danger" style="font-size:11px;" onclick="return confirm('تأكيد الانسحاب من الأكاديمية؟')">تأكيد الانسحاب</button>
                            </form>
                        </div>
                        <?php endif; ?>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <?php else: ?>
    <div style="padding:30px;text-align:center;color:#9CA3AF;font-size:14px;">
        <i data-lucide="info" style="width:20px;height:20px;margin-bottom:8px;"></i>
        <div>لا توجد تسجيلات في أكاديميات</div>
    </div>
    <?php endif; ?>
</div>

<!-- Medical Records -->
<div class="card" style="margin-bottom:20px;">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;display:flex;align-items:center;justify-content:space-between;">
        <div style="display:flex;align-items:center;gap:8px;">
            <i data-lucide="heart-pulse" style="width:18px;height:18px;color:#059669;"></i>
            <h3 style="margin:0;color:#059669;font-size:15px;">السجلات الطبية</h3>
        </div>
        <button type="button" class="btn btn-sm btn-outline" onclick="toggleMedicalForm()">
            <i data-lucide="plus" style="width:14px;height:14px;vertical-align:middle;"></i> إضافة سجل طبي
        </button>
    </div>

    <!-- Add Medical Record Inline Form (hidden by default) -->
    <div id="addMedicalForm" style="display:none;padding:20px;background:#F9FAFB;border-bottom:1px solid #E5E7EB;">
        <form method="POST" action="/players/<?= (int) $player->id ?>/medical">
            <?= csrf_field() ?>
            <div style="display:grid;grid-template-columns:1fr 1fr 1fr;gap:15px;">
                <div class="form-group" style="margin:0;">
                    <label class="form-label" style="font-size:12px;">نوع السجل <span style="color:#DC2626;">*</span></label>
                    <select name="record_type" class="form-input" required>
                        <option value="">-- اختر --</option>
                        <?php foreach ($recordTypes as $rtKey => $rtLabel): ?>
                        <option value="<?= e($rtKey) ?>"><?= e($rtLabel) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="form-group" style="margin:0;">
                    <label class="form-label" style="font-size:12px;">تاريخ الفحص <span style="color:#DC2626;">*</span></label>
                    <input type="date" name="exam_date" class="form-input" required>
                </div>
                <div class="form-group" style="margin:0;">
                    <label class="form-label" style="font-size:12px;">تاريخ الانتهاء</label>
                    <input type="date" name="expiry_date" class="form-input">
                </div>
            </div>
            <div style="display:grid;grid-template-columns:1fr 1fr 1fr;gap:15px;margin-top:15px;">
                <div class="form-group" style="margin:0;">
                    <label class="form-label" style="font-size:12px;">اسم الطبيب</label>
                    <input type="text" name="doctor_name" class="form-input" placeholder="اسم الطبيب">
                </div>
                <div class="form-group" style="margin:0;">
                    <label class="form-label" style="font-size:12px;">اسم العيادة/المستشفى</label>
                    <input type="text" name="clinic_name" class="form-input" placeholder="اسم المكان">
                </div>
                <div class="form-group" style="margin:0;">
                    <label class="form-label" style="font-size:12px;">النتيجة <span style="color:#DC2626;">*</span></label>
                    <select name="result" class="form-input" required>
                        <option value="">-- اختر --</option>
                        <?php foreach ($resultTypes as $resKey => $resLabel): ?>
                        <option value="<?= e($resKey) ?>"><?= e($resLabel) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
            </div>
            <div style="display:grid;grid-template-columns:1fr 1fr;gap:15px;margin-top:15px;">
                <div class="form-group" style="margin:0;">
                    <label class="form-label" style="font-size:12px;">قيود/شروط</label>
                    <textarea name="restrictions" class="form-input" rows="2" placeholder="أي قيود أو شروط طبية..."></textarea>
                </div>
                <div class="form-group" style="margin:0;">
                    <label class="form-label" style="font-size:12px;">ملاحظات</label>
                    <textarea name="notes" class="form-input" rows="2" placeholder="ملاحظات إضافية..."></textarea>
                </div>
            </div>
            <div style="margin-top:15px;display:flex;gap:8px;">
                <button type="submit" class="btn btn-primary" style="font-size:13px;padding:8px 20px;">
                    <i data-lucide="check" style="width:14px;height:14px;vertical-align:middle;margin-left:4px;"></i> حفظ السجل
                </button>
                <button type="button" class="btn btn-outline" style="font-size:13px;padding:8px 20px;" onclick="toggleMedicalForm()">إلغاء</button>
            </div>
        </form>
    </div>

    <?php if (!empty($medicalRecords)): ?>
    <div style="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>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($medicalRecords as $rec):
                    $recType = $rec['record_type'] ?? '';
                    $recResult = $rec['result'] ?? '';
                    $resultLabel = $resultTypes[$recResult] ?? $recResult;
                    $resultColorMap = ['fit' => '#059669', 'conditional' => '#D97706', 'unfit' => '#DC2626'];
                    $resultColor = $resultColorMap[$recResult] ?? '#6B7280';
                ?>
                <tr style="border-bottom:1px solid #F3F4F6;">
                    <td style="padding:12px 15px;font-weight:600;"><?= e($recordTypes[$recType] ?? $recType) ?></td>
                    <td style="padding:12px 15px;"><?= e($rec['exam_date'] ?? '—') ?></td>
                    <td style="padding:12px 15px;"><?= e($rec['expiry_date'] ?? '—') ?></td>
                    <td style="padding:12px 15px;">
                        <span style="display:inline-block;padding:4px 12px;border-radius:10px;font-size:12px;font-weight:600;background:<?= $resultColor ?>15;color:<?= $resultColor ?>;"><?= e($resultLabel) ?></span>
                    </td>
                    <td style="padding:12px 15px;"><?= e($rec['doctor_name'] ?? '—') ?></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <?php else: ?>
    <div style="padding:30px;text-align:center;color:#9CA3AF;font-size:14px;">
        <i data-lucide="info" style="width:20px;height:20px;margin-bottom:8px;"></i>
        <div>لا توجد سجلات طبية</div>
    </div>
    <?php endif; ?>
</div>

<script>
document.addEventListener('DOMContentLoaded', function() {
    if (typeof lucide !== 'undefined') lucide.createIcons();
});

function toggleEnrollForm() {
    var form = document.getElementById('enrollForm');
    form.style.display = form.style.display === 'none' ? 'block' : 'none';
}

function toggleMedicalForm() {
    var form = document.getElementById('addMedicalForm');
    form.style.display = form.style.display === 'none' ? 'block' : 'none';
}

function toggleDropForm(enrollmentId) {
    var form = document.getElementById('dropForm-' + enrollmentId);
    if (form) {
        form.style.display = form.style.display === 'none' ? 'block' : 'none';
    }
}
</script>
<?php $__template->endSection(); ?>
