<?php
use App\Modules\PlayerAffairs\Models\PlayerMedicalRecord;
$__template->layout('Layout.main');
$certTypes = PlayerMedicalRecord::getCertificateTypes();
$recordTypes = PlayerMedicalRecord::getRecordTypes();
?>
<?php $__template->section('title'); ?>اعتماد السجلات الطبية<?php $__template->endSection(); ?>

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

<!-- Filter Tabs -->
<div style="display:flex;gap:8px;margin-bottom:20px;">
    <a href="?filter=pending" class="btn <?= $filter === 'pending' ? 'btn-primary' : 'btn-outline' ?>" style="font-size:13px;">
        <i data-lucide="clock" style="width:14px;height:14px;vertical-align:middle;margin-left:4px;"></i> في الانتظار
    </a>
    <a href="?filter=approved" class="btn <?= $filter === 'approved' ? 'btn-primary' : 'btn-outline' ?>" style="font-size:13px;">
        <i data-lucide="check-circle" style="width:14px;height:14px;vertical-align:middle;margin-left:4px;"></i> معتمد
    </a>
    <a href="?filter=rejected" class="btn <?= $filter === 'rejected' ? 'btn-primary' : 'btn-outline' ?>" style="font-size:13px;">
        <i data-lucide="x-circle" style="width:14px;height:14px;vertical-align:middle;margin-left:4px;"></i> مرفوض
    </a>
</div>

<?php if (empty($records)): ?>
<div class="card" style="padding:40px;text-align:center;">
    <i data-lucide="clipboard-check" style="width:48px;height:48px;color:#D1D5DB;margin-bottom:12px;"></i>
    <p style="color:#6B7280;font-size:15px;">لا توجد سجلات طبية <?= $filter === 'pending' ? 'في الانتظار' : ($filter === 'approved' ? 'معتمدة' : 'مرفوضة') ?></p>
</div>
<?php else: ?>

<div style="display:grid;gap:15px;">
    <?php foreach ($records as $rec):
        $recCertType = $rec['certificate_type'] ?? 'recreational';
        $certLabel = $certTypes[$recCertType] ?? $recCertType;
        $certColorMap = ['recreational' => '#2563EB', 'academy' => '#7C3AED', 'international' => '#DC2626'];
        $certColor = $certColorMap[$recCertType] ?? '#6B7280';
        $recTypeLabel = $recordTypes[$rec['record_type'] ?? ''] ?? ($rec['record_type'] ?? '—');
    ?>
    <div class="card" style="padding:20px;border-right:4px solid <?= $certColor ?>;">
        <div style="display:flex;justify-content:space-between;align-items:start;gap:20px;">
            <!-- Player Info -->
            <div style="display:flex;gap:15px;align-items:center;flex:1;">
                <div style="width:50px;height:50px;border-radius:50%;background:#F3F4F6;display:flex;align-items:center;justify-content:center;overflow:hidden;flex-shrink:0;">
                    <?php if (!empty($rec['photo_path'])): ?>
                    <img src="/<?= e($rec['photo_path']) ?>" style="width:100%;height:100%;object-fit:cover;">
                    <?php else: ?>
                    <i data-lucide="user" style="width:24px;height:24px;color:#9CA3AF;"></i>
                    <?php endif; ?>
                </div>
                <div>
                    <div style="font-size:15px;font-weight:700;color:#1A1A2E;margin-bottom:4px;">
                        <a href="/players/<?= (int) $rec['player_id'] ?>" style="color:inherit;text-decoration:none;"><?= e($rec['player_name'] ?? '—') ?></a>
                    </div>
                    <div style="display:flex;gap:12px;flex-wrap:wrap;font-size:12px;color:#6B7280;">
                        <?php if (!empty($rec['registration_serial'])): ?>
                        <span><i data-lucide="hash" style="width:11px;height:11px;vertical-align:middle;"></i> <?= e($rec['registration_serial']) ?></span>
                        <?php endif; ?>
                        <span><?= e($rec['player_type'] === 'member' ? 'عضو' : 'غير عضو') ?></span>
                    </div>
                </div>
            </div>

            <!-- Certificate Details -->
            <div style="flex:1;">
                <div style="display:flex;gap:10px;align-items:center;margin-bottom:8px;">
                    <span style="display:inline-block;padding:4px 12px;border-radius:10px;font-size:12px;font-weight:600;background:<?= $certColor ?>15;color:<?= $certColor ?>;"><?= e($certLabel) ?></span>
                    <span style="font-size:13px;color:#374151;font-weight:600;"><?= e($recTypeLabel) ?></span>
                </div>
                <div style="display:grid;grid-template-columns:1fr 1fr;gap:6px;font-size:12px;color:#6B7280;">
                    <span>تاريخ الفحص: <strong style="color:#374151;"><?= e($rec['exam_date'] ?? '—') ?></strong></span>
                    <span>الانتهاء: <strong style="color:#374151;"><?= e($rec['expiry_date'] ?? '—') ?></strong></span>
                    <?php if (!empty($rec['doctor_name'])): ?>
                    <span>الطبيب: <strong style="color:#374151;"><?= e($rec['doctor_name']) ?></strong></span>
                    <?php endif; ?>
                    <?php if (!empty($rec['issuing_authority'])): ?>
                    <span>جهة الإصدار: <strong style="color:#374151;"><?= e($rec['issuing_authority']) ?></strong></span>
                    <?php endif; ?>
                    <?php if (!empty($rec['cert_number'])): ?>
                    <span>رقم الشهادة: <strong style="color:#374151;"><?= e($rec['cert_number']) ?></strong></span>
                    <?php endif; ?>
                    <?php if (!empty($rec['clinic_name'])): ?>
                    <span>العيادة: <strong style="color:#374151;"><?= e($rec['clinic_name']) ?></strong></span>
                    <?php endif; ?>
                </div>
                <?php if (!empty($rec['notes'])): ?>
                <div style="margin-top:6px;font-size:12px;color:#6B7280;font-style:italic;"><?= e($rec['notes']) ?></div>
                <?php endif; ?>
                <?php if ($filter === 'rejected' && !empty($rec['rejection_reason'])): ?>
                <div style="margin-top:8px;padding:8px 12px;background:#FEF2F2;border:1px solid #FCA5A5;border-radius:6px;font-size:12px;color:#DC2626;">
                    <strong>سبب الرفض:</strong> <?= e($rec['rejection_reason']) ?>
                </div>
                <?php endif; ?>
            </div>

            <!-- Actions -->
            <?php if ($filter === 'pending'): ?>
            <div style="display:flex;flex-direction:column;gap:8px;flex-shrink:0;">
                <form method="POST" action="/medical-approvals/<?= (int) $rec['id'] ?>/approve" style="margin:0;">
                    <?= csrf_field() ?>
                    <button type="submit" class="btn btn-primary" style="font-size:12px;padding:8px 16px;width:100%;" onclick="return confirm('تأكيد اعتماد هذا السجل الطبي؟')">
                        <i data-lucide="check" style="width:13px;height:13px;vertical-align:middle;margin-left:4px;"></i> اعتماد
                    </button>
                </form>
                <button type="button" class="btn" style="font-size:12px;padding:8px 16px;color:#DC2626;border:1px solid #FCA5A5;background:#FEF2F2;border-radius:6px;cursor:pointer;width:100%;" onclick="toggleRejectForm(<?= (int) $rec['id'] ?>)">
                    <i data-lucide="x" style="width:13px;height:13px;vertical-align:middle;margin-left:4px;"></i> رفض
                </button>
            </div>
            <?php endif; ?>
        </div>

        <?php if ($filter === 'pending'): ?>
        <div id="rejectForm-<?= (int) $rec['id'] ?>" style="display:none;margin-top:15px;padding:15px;background:#FEF2F2;border:1px solid #FCA5A5;border-radius:8px;">
            <form method="POST" action="/medical-approvals/<?= (int) $rec['id'] ?>/reject">
                <?= csrf_field() ?>
                <div class="form-group" style="margin:0 0 10px 0;">
                    <label class="form-label" style="font-size:12px;color:#DC2626;">سبب الرفض <span style="color:#DC2626;">*</span></label>
                    <textarea name="rejection_reason" class="form-input" rows="2" placeholder="أدخل سبب رفض السجل الطبي..." required></textarea>
                </div>
                <div style="display:flex;gap:8px;">
                    <button type="submit" class="btn btn-danger" style="font-size:12px;padding:6px 14px;" onclick="return confirm('تأكيد رفض هذا السجل الطبي؟')">تأكيد الرفض</button>
                    <button type="button" class="btn btn-outline" style="font-size:12px;padding:6px 14px;" onclick="toggleRejectForm(<?= (int) $rec['id'] ?>)">إلغاء</button>
                </div>
            </form>
        </div>
        <?php endif; ?>
    </div>
    <?php endforeach; ?>
</div>

<?php endif; ?>

<script>
function toggleRejectForm(id) {
    var el = document.getElementById('rejectForm-' + id);
    if (el) el.style.display = el.style.display === 'none' ? 'block' : 'none';
}
document.addEventListener('DOMContentLoaded', function() {
    if (typeof lucide !== 'undefined') lucide.createIcons();
});
</script>
<?php $__template->endSection(); ?>
