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

<?php $__template->section('page_actions'); ?>
<a href="/injuries" 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
$status = $injury->status ?? 'active';
$severity = $injury->severity ?? 'minor';
$statusLabel = $statuses[$status] ?? $status;
$severityLabel = $severities[$severity] ?? $severity;
$statusColor = PlayerInjury::getStatusColor($status);
$severityColor = PlayerInjury::getSeverityColor($severity);
$injuryTypeLabel = $injuryTypes[$injury->injury_type ?? ''] ?? ($injury->injury_type ?? '');
$bodyPartLabel = $bodyParts[$injury->body_part ?? ''] ?? ($injury->body_part ?? '');
?>

<!-- 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="heart-pulse" style="width:22px;height:22px;vertical-align:middle;margin-left:8px;color:<?= $severityColor ?>;"></i>
                إصابة <?= e($injuryTypeLabel) ?> - <?= e($bodyPartLabel) ?>
            </h2>
            <div style="display:flex;gap:10px;align-items:center;flex-wrap:wrap;">
                <span style="display:inline-block;padding:3px 12px;border-radius:10px;font-size:12px;font-weight:600;background:<?= $statusColor ?>15;color:<?= $statusColor ?>;"><?= e($statusLabel) ?></span>
                <span style="display:inline-block;padding:3px 12px;border-radius:10px;font-size:12px;font-weight:600;background:<?= $severityColor ?>15;color:<?= $severityColor ?>;"><?= e($severityLabel) ?></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 (in_array($status, ['active', 'recovering'])): ?>
        <div style="display:flex;gap:8px;">
            <button onclick="document.getElementById('clearanceModal').style.display='flex'" class="btn btn-primary" style="background:#059669;">
                <i data-lucide="shield-check" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> إصدار تصريح
            </button>
        </div>
        <?php endif; ?>
    </div>
</div>

<div style="display:grid;grid-template-columns:2fr 1fr;gap:20px;">
    <!-- Main Details -->
    <div>
        <!-- Injury Details Card -->
        <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:140px;">تاريخ الإصابة:</td>
                        <td style="padding:8px 0;font-weight:600;"><?= e($injury->occurred_date ?? '') ?></td>
                    </tr>
                    <?php if ($injury->occurred_during): ?>
                    <tr>
                        <td style="padding:8px 0;color:#6B7280;">حدثت أثناء:</td>
                        <td style="padding:8px 0;"><?= e(PlayerInjury::getOccurredDuringOptions()[$injury->occurred_during] ?? $injury->occurred_during) ?></td>
                    </tr>
                    <?php endif; ?>
                    <?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 ($injury->estimated_recovery_days): ?>
                    <tr>
                        <td style="padding:8px 0;color:#6B7280;">فترة التعافي المتوقعة:</td>
                        <td style="padding:8px 0;"><?= (int) $injury->estimated_recovery_days ?> يوم</td>
                    </tr>
                    <?php endif; ?>
                    <?php if ($injury->actual_recovery_date): ?>
                    <tr>
                        <td style="padding:8px 0;color:#6B7280;">تاريخ الشفاء الفعلي:</td>
                        <td style="padding:8px 0;color:#059669;font-weight:600;"><?= e($injury->actual_recovery_date) ?></td>
                    </tr>
                    <?php endif; ?>
                    <?php if ($injury->follow_up_date): ?>
                    <tr>
                        <td style="padding:8px 0;color:#6B7280;">تاريخ المتابعة:</td>
                        <td style="padding:8px 0;"><?= e($injury->follow_up_date) ?></td>
                    </tr>
                    <?php endif; ?>
                </table>
            </div>
        </div>

        <!-- Description & Diagnosis -->
        <?php if ($injury->description_ar || $injury->diagnosis_ar || $injury->treatment_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;">
                <?php if ($injury->description_ar): ?>
                <div style="margin-bottom:15px;">
                    <strong style="color:#374151;font-size:13px;">الوصف:</strong>
                    <p style="margin:5px 0;color:#4B5563;line-height:1.6;"><?= e($injury->description_ar) ?></p>
                </div>
                <?php endif; ?>
                <?php if ($injury->diagnosis_ar): ?>
                <div style="margin-bottom:15px;">
                    <strong style="color:#374151;font-size:13px;">التشخيص:</strong>
                    <p style="margin:5px 0;color:#4B5563;line-height:1.6;"><?= e($injury->diagnosis_ar) ?></p>
                </div>
                <?php endif; ?>
                <?php if ($injury->treatment_ar): ?>
                <div style="margin-bottom:15px;">
                    <strong style="color:#374151;font-size:13px;">العلاج:</strong>
                    <p style="margin:5px 0;color:#4B5563;line-height:1.6;"><?= e($injury->treatment_ar) ?></p>
                </div>
                <?php endif; ?>
                <?php if ($injury->diagnosed_by): ?>
                <div style="font-size:13px;color:#6B7280;">
                    <i data-lucide="stethoscope" style="width:14px;height:14px;vertical-align:middle;"></i>
                    تم التشخيص بواسطة: <?= e($injury->diagnosed_by) ?>
                </div>
                <?php endif; ?>
            </div>
        </div>
        <?php endif; ?>

        <!-- Status Update Form -->
        <?php if (in_array($status, ['active', 'recovering'])): ?>
        <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;">
                <form method="POST" action="/injuries/<?= (int) $injury->id ?>/status" style="display:flex;gap:10px;align-items:end;">
                    <?= csrf_field() ?>
                    <div style="flex:1;">
                        <label class="form-label" style="font-size:12px;">الحالة الجديدة</label>
                        <select name="status" class="form-input" required>
                            <?php foreach ($statuses as $sKey => $sLabel): ?>
                            <?php if ($sKey !== $status): ?>
                            <option value="<?= e($sKey) ?>"><?= e($sLabel) ?></option>
                            <?php endif; ?>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <button type="submit" class="btn btn-primary">تحديث</button>
                </form>
            </div>
        </div>
        <?php endif; ?>
    </div>

    <!-- Sidebar -->
    <div>
        <!-- Player Stats -->
        <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:grid;grid-template-columns:1fr 1fr;gap:15px;text-align:center;">
                    <div>
                        <div style="font-size:24px;font-weight:700;color:#1A1A2E;"><?= (int) ($stats['total'] ?? 0) ?></div>
                        <div style="font-size:11px;color:#6B7280;">إجمالي الإصابات</div>
                    </div>
                    <div>
                        <div style="font-size:24px;font-weight:700;color:#DC2626;"><?= (int) ($stats['active'] ?? 0) ?></div>
                        <div style="font-size:11px;color:#6B7280;">إصابات نشطة</div>
                    </div>
                    <div>
                        <div style="font-size:24px;font-weight:700;color:#059669;"><?= (int) ($stats['recovered'] ?? 0) ?></div>
                        <div style="font-size:11px;color:#6B7280;">تم الشفاء</div>
                    </div>
                    <div>
                        <div style="font-size:24px;font-weight:700;color:#0D7377;"><?= $stats['avg_recovery_days'] ?? '—' ?></div>
                        <div style="font-size:11px;color:#6B7280;">متوسط أيام التعافي</div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Notes -->
        <?php if ($injury->notes): ?>
        <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:13px;color:#4B5563;line-height:1.6;">
                <?= e($injury->notes) ?>
            </div>
        </div>
        <?php endif; ?>

        <!-- Injury History -->
        <?php if (!empty($playerInjuries) && count($playerInjuries) > 1): ?>
        <div class="card">
            <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;">
                <h3 style="margin:0;font-size:15px;color:#374151;">سجل الإصابات</h3>
            </div>
            <div style="padding:10px 20px;max-height:300px;overflow-y:auto;">
                <?php foreach ($playerInjuries as $hist):
                    if ((int) $hist['id'] === (int) $injury->id) continue;
                    $hStatus = $hist['status'] ?? '';
                    $hColor = PlayerInjury::getStatusColor($hStatus);
                ?>
                <div style="padding:10px 0;border-bottom:1px solid #F3F4F6;display:flex;justify-content:space-between;align-items:center;">
                    <div>
                        <div style="font-size:13px;font-weight:600;color:#1A1A2E;"><?= e($injuryTypes[$hist['injury_type'] ?? ''] ?? '') ?></div>
                        <div style="font-size:11px;color:#6B7280;"><?= e($hist['occurred_date'] ?? '') ?></div>
                    </div>
                    <span style="padding:2px 8px;border-radius:8px;font-size:10px;font-weight:600;background:<?= $hColor ?>15;color:<?= $hColor ?>;">
                        <?= e($statuses[$hStatus] ?? $hStatus) ?>
                    </span>
                </div>
                <?php endforeach; ?>
            </div>
        </div>
        <?php endif; ?>
    </div>
</div>

<!-- Clearance Modal -->
<?php if (in_array($status, ['active', 'recovering'])): ?>
<div id="clearanceModal" style="display:none;position:fixed;inset:0;background:rgba(0,0,0,0.5);z-index:1000;align-items:center;justify-content:center;">
    <div class="card" style="width:90%;max-width:450px;margin:20px;">
        <div style="padding:20px;border-bottom:1px solid #E5E7EB;">
            <h3 style="margin:0;font-size:16px;color:#1A1A2E;">إصدار تصريح طبي</h3>
        </div>
        <form method="POST" action="/injuries/<?= (int) $injury->id ?>/clear" style="padding:20px;">
            <?= csrf_field() ?>
            <div style="margin-bottom:15px;">
                <label class="form-label">تاريخ الشفاء</label>
                <input type="date" name="recovery_date" value="<?= date('Y-m-d') ?>" class="form-input" required>
            </div>
            <div style="display:flex;gap:10px;justify-content:flex-end;">
                <button type="button" onclick="document.getElementById('clearanceModal').style.display='none'" class="btn btn-outline">إلغاء</button>
                <button type="submit" class="btn btn-primary" style="background:#059669;">تأكيد التصريح</button>
            </div>
        </form>
    </div>
</div>
<?php endif; ?>

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