<?php
$__template->layout('Layout.main');
?>
<?php $__template->section('title'); ?>بيانات اللاعب: <?= e($player['full_name_ar'] ?? '') ?><?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<a href="/sa/players/<?= (int) $player['id'] ?>/edit" class="btn btn-primary"><i data-lucide="edit-3" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> تعديل</a>
<a href="/sa/players/<?= (int) $player['id'] ?>/documents" class="btn btn-outline"><i data-lucide="file-text" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> المستندات</a>
<a href="/sa/players" 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
$typeLabels = ['member' => 'عضو', 'non_member' => 'غير عضو'];
$medLabels = ['fit' => 'لائق', 'unfit' => 'غير لائق', 'pending' => 'في الانتظار', 'expired' => 'منتهي', 'conditional' => 'مشروط'];
$medColors = ['fit' => '#059669', 'unfit' => '#DC2626', 'pending' => '#D97706', 'expired' => '#6B7280', 'conditional' => '#F59E0B'];
$cardLabels = ['inactive' => 'غير مفعلة', 'active' => 'فعالة', 'suspended' => 'موقوفة', 'revoked' => 'ملغاة'];
$genderLabels = ['male' => 'ذكر', 'female' => 'أنثى'];
$relationLabels = ['father' => 'أب', 'mother' => 'أم', 'brother' => 'أخ', 'sister' => 'أخت', 'uncle' => 'عم / خال', 'other' => 'أخرى'];
?>

<!-- Player Info Card -->
<div class="card" style="margin-bottom:20px;">
    <div style="padding:20px;display:flex;align-items:center;gap:20px;border-bottom:1px solid #E5E7EB;">
        <div style="width:64px;height:64px;border-radius:50%;background:linear-gradient(135deg, #0D737715, #0D737730);display:flex;align-items:center;justify-content:center;">
            <i data-lucide="user" style="width:32px;height:32px;color:#0D7377;"></i>
        </div>
        <div>
            <h2 style="margin:0 0 4px;font-size:20px;font-weight:700;"><?= e($player['full_name_ar'] ?? '') ?></h2>
            <?php if (!empty($player['full_name_en'])): ?>
            <div style="font-size:13px;color:#6B7280;margin-bottom:4px;" dir="ltr"><?= e($player['full_name_en']) ?></div>
            <?php endif; ?>
            <div style="display:flex;gap:8px;flex-wrap:wrap;">
                <code style="font-size:11px;background:#F3F4F6;padding:2px 8px;border-radius:4px;"><?= e($player['registration_serial'] ?? '') ?></code>
                <?php $mStatus = $player['medical_status'] ?? 'pending'; $mColor = $medColors[$mStatus] ?? '#6B7280'; ?>
                <span style="padding:2px 10px;border-radius:10px;font-size:11px;font-weight:600;background:<?= $mColor ?>15;color:<?= $mColor ?>;"><?= e($medLabels[$mStatus] ?? $mStatus) ?></span>
            </div>
        </div>
    </div>

    <div style="padding:20px;display:grid;grid-template-columns:repeat(auto-fit, minmax(220px, 1fr));gap:20px;">
        <div>
            <div style="font-size:12px;color:#9CA3AF;margin-bottom:4px;">نوع اللاعب</div>
            <div style="font-size:14px;font-weight:600;"><?= e($typeLabels[$player['player_type'] ?? ''] ?? '-') ?></div>
        </div>
        <div>
            <div style="font-size:12px;color:#9CA3AF;margin-bottom:4px;">الرقم القومي</div>
            <div style="font-size:14px;font-weight:600;" dir="ltr"><?= e($player['national_id'] ?? '-') ?></div>
        </div>
        <div>
            <div style="font-size:12px;color:#9CA3AF;margin-bottom:4px;">تاريخ الميلاد</div>
            <div style="font-size:14px;font-weight:600;"><?= e($player['date_of_birth'] ?? '-') ?></div>
        </div>
        <div>
            <div style="font-size:12px;color:#9CA3AF;margin-bottom:4px;">النوع</div>
            <div style="font-size:14px;font-weight:600;"><?= e($genderLabels[$player['gender'] ?? ''] ?? '-') ?></div>
        </div>
        <div>
            <div style="font-size:12px;color:#9CA3AF;margin-bottom:4px;">الهاتف</div>
            <div style="font-size:14px;font-weight:600;" dir="ltr"><?= e($player['phone'] ?? '-') ?></div>
        </div>
        <div>
            <div style="font-size:12px;color:#9CA3AF;margin-bottom:4px;">البريد الإلكتروني</div>
            <div style="font-size:14px;font-weight:600;" dir="ltr"><?= e($player['email'] ?? '-') ?></div>
        </div>
        <div>
            <div style="font-size:12px;color:#9CA3AF;margin-bottom:4px;">حالة البطاقة</div>
            <div style="font-size:14px;font-weight:600;"><?= e($cardLabels[$player['card_status'] ?? ''] ?? '-') ?></div>
        </div>
        <div>
            <div style="font-size:12px;color:#9CA3AF;margin-bottom:4px;">انتهاء الكشف الطبي</div>
            <div style="font-size:14px;font-weight:600;"><?= e($player['medical_expiry_date'] ?? '-') ?></div>
        </div>
        <?php if (!empty($player['member_id'])): ?>
        <div>
            <div style="font-size:12px;color:#9CA3AF;margin-bottom:4px;">رقم العضوية</div>
            <div style="font-size:14px;font-weight:600;"><?= (int) $player['member_id'] ?></div>
        </div>
        <?php endif; ?>
    </div>
</div>

<!-- Guardian Info -->
<?php if (!empty($player['guardian_name'])): ?>
<div class="card" style="margin-bottom:20px;">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;">
        <h3 style="margin:0;font-size:16px;font-weight:600;">بيانات ولي الأمر</h3>
    </div>
    <div style="padding:20px;display:grid;grid-template-columns:repeat(auto-fit, minmax(220px, 1fr));gap:20px;">
        <div>
            <div style="font-size:12px;color:#9CA3AF;margin-bottom:4px;">الاسم</div>
            <div style="font-size:14px;font-weight:600;"><?= e($player['guardian_name'] ?? '-') ?></div>
        </div>
        <div>
            <div style="font-size:12px;color:#9CA3AF;margin-bottom:4px;">الهاتف</div>
            <div style="font-size:14px;font-weight:600;" dir="ltr"><?= e($player['guardian_phone'] ?? '-') ?></div>
        </div>
        <div>
            <div style="font-size:12px;color:#9CA3AF;margin-bottom:4px;">الرقم القومي</div>
            <div style="font-size:14px;font-weight:600;" dir="ltr"><?= e($player['guardian_national_id'] ?? '-') ?></div>
        </div>
        <div>
            <div style="font-size:12px;color:#9CA3AF;margin-bottom:4px;">صلة القرابة</div>
            <div style="font-size:14px;font-weight:600;"><?= e($relationLabels[$player['guardian_relationship'] ?? ''] ?? '-') ?></div>
        </div>
    </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;">
        <h3 style="margin:0;font-size:16px;font-weight:600;">ملاحظات</h3>
    </div>
    <div style="padding:20px;font-size:14px;line-height:1.8;color:#374151;">
        <?= nl2br(e($player['notes'])) ?>
    </div>
</div>
<?php endif; ?>

<!-- Medical History Timeline + Inline Edit -->
<div class="card" style="margin-bottom:20px;">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;display:flex;justify-content:space-between;align-items:center;">
        <h3 style="margin:0;font-size:16px;font-weight:600;">
            <i data-lucide="heart-pulse" style="width:18px;height:18px;vertical-align:middle;margin-left:6px;color:#DC2626;"></i>
            السجل الطبي
        </h3>
        <?php if (!empty($canApproveMedical)): ?>
        <button type="button" class="btn btn-sm btn-outline" onclick="document.getElementById('medicalEditForm').style.display = document.getElementById('medicalEditForm').style.display === 'none' ? 'block' : 'none';">
            <i data-lucide="edit-3" style="width:14px;height:14px;vertical-align:middle;margin-left:4px;"></i> تعديل الحالة
        </button>
        <?php endif; ?>
    </div>

    <!-- Inline Medical Edit Form -->
    <?php if (!empty($canApproveMedical)): ?>
    <div id="medicalEditForm" style="display:none;padding:20px;background:#F9FAFB;border-bottom:1px solid #E5E7EB;">
        <form method="POST" action="/sa/players/<?= (int) $player['id'] ?>/medical/update">
            <?= csrf_field() ?>
            <div style="display:grid;grid-template-columns:repeat(auto-fit, minmax(180px, 1fr));gap:12px;margin-bottom:12px;">
                <div>
                    <label class="form-label" style="font-size:11px;">الحالة الطبية</label>
                    <select name="medical_status" class="form-input" style="font-size:13px;">
                        <?php foreach ($medLabels as $key => $label): ?>
                        <option value="<?= $key ?>" <?= ($player['medical_status'] ?? '') === $key ? 'selected' : '' ?>><?= e($label) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div>
                    <label class="form-label" style="font-size:11px;">تاريخ انتهاء الصلاحية</label>
                    <input type="date" name="medical_expiry_date" class="form-input" value="<?= e($player['medical_expiry_date'] ?? '') ?>" style="font-size:13px;">
                </div>
                <div>
                    <label class="form-label" style="font-size:11px;">تاريخ الكشف</label>
                    <input type="date" name="exam_date" class="form-input" style="font-size:13px;">
                </div>
                <div>
                    <label class="form-label" style="font-size:11px;">اسم الطبيب</label>
                    <input type="text" name="doctor_name" class="form-input" placeholder="د. ..." style="font-size:13px;">
                </div>
                <div>
                    <label class="form-label" style="font-size:11px;">العيادة / المستشفى</label>
                    <input type="text" name="clinic_name" class="form-input" placeholder="اسم العيادة" style="font-size:13px;">
                </div>
            </div>
            <button type="submit" class="btn btn-sm btn-primary">
                <i data-lucide="save" style="width:14px;height:14px;vertical-align:middle;margin-left:4px;"></i> حفظ التعديلات
            </button>
        </form>
    </div>
    <?php endif; ?>

    <!-- Timeline -->
    <?php if (!empty($medicalHistory)): ?>
    <div style="padding:20px;">
        <?php
        $approvalLabels = ['pending' => 'في الانتظار', 'approved' => 'معتمد', 'rejected' => 'مرفوض', 'expired' => 'منتهي'];
        $approvalColors = ['pending' => '#D97706', 'approved' => '#059669', 'rejected' => '#DC2626', 'expired' => '#6B7280'];
        ?>
        <?php foreach ($medicalHistory as $idx => $doc): ?>
        <?php $aStatus = $doc['approval_status'] ?? 'pending'; $aColor = $approvalColors[$aStatus] ?? '#6B7280'; ?>
        <div style="display:flex;gap:16px;padding-bottom:16px;<?= $idx < count($medicalHistory) - 1 ? 'border-bottom:1px dashed #E5E7EB;margin-bottom:16px;' : '' ?>">
            <div style="width:10px;height:10px;border-radius:50%;background:<?= $aColor ?>;margin-top:6px;flex-shrink:0;"></div>
            <div style="flex:1;">
                <div style="display:flex;align-items:center;gap:8px;margin-bottom:6px;">
                    <span style="padding:2px 8px;border-radius:8px;font-size:11px;font-weight:600;background:<?= $aColor ?>15;color:<?= $aColor ?>;">
                        <?= e($approvalLabels[$aStatus] ?? $aStatus) ?>
                    </span>
                    <span style="font-size:12px;color:#9CA3AF;"><?= e($doc['created_at'] ?? '') ?></span>
                </div>
                <div style="display:grid;grid-template-columns:repeat(auto-fit, minmax(150px, 1fr));gap:8px;font-size:13px;color:#374151;">
                    <?php if (!empty($doc['exam_date'])): ?>
                    <div><span style="color:#9CA3AF;">تاريخ الكشف:</span> <?= e($doc['exam_date']) ?></div>
                    <?php endif; ?>
                    <?php if (!empty($doc['expiry_date'])): ?>
                    <div><span style="color:#9CA3AF;">ينتهي:</span> <?= e($doc['expiry_date']) ?></div>
                    <?php endif; ?>
                    <?php if (!empty($doc['doctor_name'])): ?>
                    <div><span style="color:#9CA3AF;">الطبيب:</span> <?= e($doc['doctor_name']) ?></div>
                    <?php endif; ?>
                    <?php if (!empty($doc['clinic_name'])): ?>
                    <div><span style="color:#9CA3AF;">العيادة:</span> <?= e($doc['clinic_name']) ?></div>
                    <?php endif; ?>
                </div>
                <?php if (!empty($doc['conditional_notes'])): ?>
                <div style="margin-top:6px;padding:8px 12px;background:#FEF3C7;border-radius:6px;font-size:12px;color:#92400E;">
                    <strong>ملاحظات مشروطة:</strong> <?= e($doc['conditional_notes']) ?>
                </div>
                <?php endif; ?>
                <?php if (!empty($doc['rejection_reason'])): ?>
                <div style="margin-top:6px;padding:8px 12px;background:#FEE2E2;border-radius:6px;font-size:12px;color:#991B1B;">
                    <strong>سبب الرفض:</strong> <?= e($doc['rejection_reason']) ?>
                </div>
                <?php endif; ?>
            </div>
        </div>
        <?php endforeach; ?>
    </div>
    <?php else: ?>
    <div style="padding:40px 20px;text-align:center;color:#9CA3AF;">
        لا يوجد سجل طبي بعد.
    </div>
    <?php endif; ?>
</div>

<!-- Documents Section -->
<div class="card">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;display:flex;justify-content:space-between;align-items:center;">
        <h3 style="margin:0;font-size:16px;font-weight:600;">المستندات</h3>
        <a href="/sa/players/<?= (int) $player['id'] ?>/documents" class="btn btn-sm btn-primary">
            <i data-lucide="upload" style="width:14px;height:14px;vertical-align:middle;margin-left:4px;"></i> إدارة المستندات
        </a>
    </div>
    <?php if (!empty($documents)): ?>
    <div class="table-responsive">
        <table class="data-table">
            <thead>
                <tr>
                    <th>العنوان</th>
                    <th>النوع</th>
                    <th>الحالة</th>
                    <th>تاريخ الرفع</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($documents as $doc): ?>
                <tr>
                    <td><?= e($doc['file_name'] ?? '') ?></td>
                    <td>
                        <?php
                        $docTypes = ['medical_cert' => 'شهادة طبية', 'national_id' => 'صورة بطاقة', 'photo' => 'صورة شخصية', 'birth_cert' => 'شهادة ميلاد'];
                        ?>
                        <?= e($docTypes[$doc['document_type'] ?? ''] ?? $doc['document_type'] ?? '') ?>
                    </td>
                    <td>
                        <?php
                        $approvalLabels2 = ['pending' => 'في الانتظار', 'approved' => 'معتمد', 'rejected' => 'مرفوض', 'expired' => 'منتهي'];
                        $approvalColors2 = ['pending' => '#D97706', 'approved' => '#059669', 'rejected' => '#DC2626', 'expired' => '#6B7280'];
                        $aStatus2 = $doc['approval_status'] ?? 'pending';
                        $aColor2 = $approvalColors2[$aStatus2] ?? '#6B7280';
                        ?>
                        <span style="padding:2px 8px;border-radius:8px;font-size:11px;font-weight:600;background:<?= $aColor2 ?>15;color:<?= $aColor2 ?>;">
                            <?= e($approvalLabels2[$aStatus2] ?? $aStatus2) ?>
                        </span>
                    </td>
                    <td style="font-size:13px;"><?= e($doc['created_at'] ?? '') ?></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <?php else: ?>
    <div style="padding:40px 20px;text-align:center;color:#9CA3AF;">
        لا توجد مستندات مرفوعة بعد.
    </div>
    <?php endif; ?>
</div>

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