<?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/create" class="btn btn-primary"><i data-lucide="plus" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> تسجيل إصابة</a>
<?php $__template->endSection(); ?>

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

<?php $currentStatus = $filters['status'] ?? ''; ?>

<!-- Status 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="/injuries"
           style="padding:12px 20px;font-size:14px;font-weight:600;text-decoration:none;border-bottom:3px solid <?= $currentStatus === '' ? '#0D7377' : 'transparent' ?>;color:<?= $currentStatus === '' ? '#0D7377' : '#6B7280' ?>;white-space:nowrap;">
            الكل
        </a>
        <?php foreach ($statuses as $sKey => $sLabel): ?>
        <?php $statusColor = PlayerInjury::getStatusColor($sKey); ?>
        <a href="/injuries?status=<?= e($sKey) ?>"
           style="padding:12px 20px;font-size:14px;font-weight:600;text-decoration:none;border-bottom:3px solid <?= $currentStatus === $sKey ? $statusColor : 'transparent' ?>;color:<?= $currentStatus === $sKey ? $statusColor : '#6B7280' ?>;white-space:nowrap;">
            <?= e($sLabel) ?>
        </a>
        <?php endforeach; ?>
    </div>
</div>

<!-- Search & Filter Bar -->
<div class="card" style="margin-bottom:20px;padding:15px;">
    <form method="GET" action="/injuries" 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="severity" class="form-input">
                <option value="">الكل</option>
                <?php foreach ($severities as $key => $label): ?>
                <option value="<?= e($key) ?>" <?= ($filters['severity'] ?? '') === $key ? 'selected' : '' ?>><?= e($label) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div style="min-width:130px;">
            <label class="form-label" style="font-size:12px;">النوع</label>
            <select name="injury_type" class="form-input">
                <option value="">الكل</option>
                <?php foreach ($injuryTypes as $key => $label): ?>
                <option value="<?= e($key) ?>" <?= ($filters['injury_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 ($currentStatus !== ''): ?>
        <input type="hidden" name="status" value="<?= e($currentStatus) ?>">
        <?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="/injuries" class="btn btn-sm btn-outline" style="color:#6B7280;">مسح</a>
    </form>
</div>

<!-- Injuries Table -->
<?php if (!empty($injuries)): ?>
<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;">تاريخ الإصابة</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 ($injuries as $i => $inj):
                $severity = $inj['severity'] ?? '';
                $status = $inj['status'] ?? '';
                $sevColor = PlayerInjury::getSeverityColor($severity);
                $statColor = PlayerInjury::getStatusColor($status);
                $sevLabel = $severities[$severity] ?? $severity;
                $statLabel = $statuses[$status] ?? $status;
                $typeLabel = $injuryTypes[$inj['injury_type'] ?? ''] ?? ($inj['injury_type'] ?? '');
                $bodyPartLabel = PlayerInjury::getBodyParts()[$inj['body_part'] ?? ''] ?? ($inj['body_part'] ?? '');
            ?>
            <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($inj['player_name'] ?? '') ?></td>
                <td style="padding:12px 15px;"><?= e($typeLabel) ?></td>
                <td style="padding:12px 15px;"><?= e($bodyPartLabel) ?></td>
                <td style="padding:12px 15px;">
                    <span style="display:inline-block;padding:2px 10px;border-radius:10px;font-size:11px;font-weight:600;background:<?= $sevColor ?>15;color:<?= $sevColor ?>;"><?= e($sevLabel) ?></span>
                </td>
                <td style="padding:12px 15px;">
                    <span style="display:inline-block;padding:2px 10px;border-radius:10px;font-size:11px;font-weight:600;background:<?= $statColor ?>15;color:<?= $statColor ?>;"><?= e($statLabel) ?></span>
                </td>
                <td style="padding:12px 15px;color:#374151;font-size:13px;"><?= e($inj['occurred_date'] ?? '') ?></td>
                <td style="padding:12px 15px;white-space:nowrap;">
                    <a href="/injuries/<?= (int) $inj['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="heart-pulse" 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['status']) || !empty($filters['severity'])): ?>
            لا توجد نتائج مطابقة لبحثك.
        <?php else: ?>
            لم يتم تسجيل أي إصابات حتى الآن.
        <?php endif; ?>
    </p>
    <a href="/injuries/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(); ?>
