<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>اللجنة الطبية<?php $__template->endSection(); ?>

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

<div style="margin-bottom:20px;">
    <div style="display:flex;gap:10px;align-items:center;">
        <a href="/medical-board?status=pending" class="btn <?= ($currentStatus === 'pending') ? 'btn-primary' : 'btn-outline' ?>" style="padding:8px 16px;">
            في الانتظار
        </a>
        <a href="/medical-board?status=approved" class="btn <?= ($currentStatus === 'approved') ? 'btn-primary' : 'btn-outline' ?>" style="padding:8px 16px;">
            معتمد
        </a>
        <a href="/medical-board?status=rejected" class="btn <?= ($currentStatus === 'rejected') ? 'btn-primary' : 'btn-outline' ?>" style="padding:8px 16px;">
            مرفوض
        </a>
    </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="heart-pulse" style="width:18px;height:18px;color:#DC2626;"></i>
        <h3 style="margin:0;color:#DC2626;font-size:15px;">الشهادات الطبية - <?= $currentStatus === 'pending' ? 'في الانتظار' : ($currentStatus === 'approved' ? 'معتمدة' : 'مرفوضة') ?></h3>
    </div>

    <?php if (empty($records)): ?>
    <div style="padding:40px;text-align:center;color:#6B7280;">
        لا توجد سجلات
    </div>
    <?php else: ?>
    <div style="overflow-x:auto;">
        <table class="data-table" style="width:100%;">
            <thead>
                <tr>
                    <th>اللاعب</th>
                    <th>نوع السجل</th>
                    <th>نوع الشهادة</th>
                    <th>تاريخ الفحص</th>
                    <th>تاريخ الانتهاء</th>
                    <th>الطبيب / العيادة</th>
                    <th>المستند</th>
                    <?php if ($currentStatus === 'pending'): ?>
                    <th>إجراء</th>
                    <?php endif; ?>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($records as $record): ?>
                <tr>
                    <td>
                        <strong><?= e($record['player_name'] ?? '') ?></strong>
                        <?php if (($record['source'] ?? 'membership') === 'sports_activity'): ?>
                        <span style="padding:2px 6px;border-radius:8px;font-size:10px;font-weight:600;background:#EFF6FF;color:#2563EB;margin-right:4px;">نشاط رياضي</span>
                        <?php endif; ?>
                        <?php if (!empty($record['player_phone'])): ?>
                        <br><small style="color:#6B7280;"><?= e($record['player_phone']) ?></small>
                        <?php endif; ?>
                    </td>
                    <td><?= e($record['record_type'] ?? '-') ?></td>
                    <td><?= e($record['certificate_type'] ?? '-') ?></td>
                    <td style="direction:ltr;text-align:center;"><?= e($record['exam_date'] ?? '-') ?></td>
                    <td style="direction:ltr;text-align:center;"><?= e($record['expiry_date'] ?? '-') ?></td>
                    <td><?= e($record['doctor_name'] ?? $record['clinic_name'] ?? '-') ?></td>
                    <td>
                        <?php
                        $docPath = $record['document_path'] ?? $record['sa_document_path'] ?? '';
                        ?>
                        <?php if (!empty($docPath)): ?>
                        <a href="/<?= e($docPath) ?>" target="_blank" style="color:#2563EB;">
                            <i data-lucide="file-text" style="width:14px;height:14px;vertical-align:middle;"></i> عرض
                        </a>
                        <?php else: ?>
                        <span style="color:#9CA3AF;">-</span>
                        <?php endif; ?>
                    </td>
                    <?php if ($currentStatus === 'pending'): ?>
                    <td>
                        <div style="display:flex;flex-direction:column;gap:8px;">
                            <!-- Approve Form -->
                            <form method="POST" action="/medical-board/<?= (int) $record['id'] ?>/approve" style="display:flex;gap:6px;align-items:flex-end;">
                                <?= csrf_field() ?>
                                <div>
                                    <label style="font-size:11px;color:#6B7280;">تاريخ الانتهاء</label>
                                    <input type="date" name="expiry_date" required class="form-input" style="padding:4px 8px;font-size:12px;direction:ltr;width:140px;">
                                </div>
                                <button type="submit" class="btn btn-primary" style="padding:4px 10px;font-size:12px;background:#059669;" title="اعتماد">
                                    <i data-lucide="check" style="width:12px;height:12px;"></i> اعتماد
                                </button>
                            </form>
                            <!-- Reject Form -->
                            <form method="POST" action="/medical-board/<?= (int) $record['id'] ?>/reject" style="display:flex;gap:6px;align-items:flex-end;">
                                <?= csrf_field() ?>
                                <div>
                                    <label style="font-size:11px;color:#6B7280;">سبب الرفض</label>
                                    <input type="text" name="rejection_reason" placeholder="السبب..." class="form-input" style="padding:4px 8px;font-size:12px;width:140px;">
                                </div>
                                <button type="submit" class="btn btn-outline" style="padding:4px 10px;font-size:12px;color:#DC2626;border-color:#DC2626;" title="رفض">
                                    <i data-lucide="x" style="width:12px;height:12px;"></i> رفض
                                </button>
                            </form>
                        </div>
                    </td>
                    <?php endif; ?>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <?php endif; ?>
</div>

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

<?php $__template->endSection(); ?>
