<?php
$__template->layout('Layout.main');
$session = \App\Core\App::getInstance()->session();
$employeePermissions = $session->get('permissions') ?? [];
$canApprove = in_array('sa.medical.approve', $employeePermissions, true) || ($session->get('is_super_admin') ?? false);
?>
<?php $__template->section('title'); ?>مستندات اللاعب: <?= e($player['full_name_ar'] ?? '') ?><?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<a href="/sa/players/<?= (int) $player['id'] ?>" 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
$docTypes = ['medical_cert' => 'شهادة طبية', 'national_id' => 'صورة بطاقة', 'photo' => 'صورة شخصية', 'birth_cert' => 'شهادة ميلاد'];
$approvalLabels = ['pending' => 'في الانتظار', 'approved' => 'معتمد', 'rejected' => 'مرفوض'];
$approvalColors = ['pending' => '#D97706', 'approved' => '#059669', 'rejected' => '#DC2626'];
?>

<!-- Documents List -->
<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>

    <?php if (!empty($documents)): ?>
    <div class="table-responsive">
        <table class="data-table">
            <thead>
                <tr>
                    <th>العنوان</th>
                    <th>النوع</th>
                    <th>الحالة</th>
                    <th>تاريخ الرفع</th>
                    <th>الملف</th>
                    <?php if ($canApprove): ?>
                    <th>إجراءات</th>
                    <?php endif; ?>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($documents as $doc):
                    $aStatus = $doc['approval_status'] ?? 'pending';
                    $aColor = $approvalColors[$aStatus] ?? '#6B7280';
                    $isMedicalPending = ($doc['document_type'] ?? '') === 'medical_cert' && $aStatus === 'pending';
                ?>
                <tr>
                    <td style="font-weight:600;"><?= e($doc['file_name'] ?? '') ?></td>
                    <td><?= e($docTypes[$doc['document_type'] ?? ''] ?? $doc['document_type'] ?? '') ?></td>
                    <td>
                        <span style="padding:3px 10px;border-radius:10px;font-size:11px;font-weight:600;background:<?= $aColor ?>15;color:<?= $aColor ?>;">
                            <?= e($approvalLabels[$aStatus] ?? $aStatus) ?>
                        </span>
                        <?php if ($aStatus === 'rejected' && !empty($doc['rejection_reason'])): ?>
                        <div style="font-size:11px;color:#DC2626;margin-top:4px;"><?= e($doc['rejection_reason']) ?></div>
                        <?php endif; ?>
                        <?php if ($aStatus === 'approved' && !empty($doc['expiry_date'])): ?>
                        <div style="font-size:11px;color:#059669;margin-top:4px;">صالح حتى: <?= e($doc['expiry_date']) ?></div>
                        <?php endif; ?>
                        <?php if ($aStatus === 'approved' && !empty($doc['conditional_notes'])): ?>
                        <div style="font-size:11px;color:#F59E0B;margin-top:4px;">مشروط: <?= e($doc['conditional_notes']) ?></div>
                        <?php endif; ?>
                    </td>
                    <td style="font-size:13px;"><?= e($doc['created_at'] ?? '') ?></td>
                    <td>
                        <?php if (!empty($doc['file_path'])): ?>
                        <a href="/<?= e($doc['file_path']) ?>" target="_blank" class="btn btn-sm btn-outline" title="تحميل">
                            <i data-lucide="download" style="width:14px;height:14px;"></i>
                        </a>
                        <?php endif; ?>
                    </td>
                    <?php if ($canApprove): ?>
                    <td>
                        <?php if ($isMedicalPending): ?>
                        <div style="display:flex;gap:6px;flex-wrap:wrap;">
                            <!-- Approve Form -->
                            <form method="POST" action="/sa/players/<?= (int) $player['id'] ?>/documents/<?= (int) $doc['id'] ?>/approve" style="display:inline-flex;gap:4px;align-items:center;">
                                <?= csrf_field() ?>
                                <input type="date" name="expiry_date" class="form-input" style="width:140px;height:30px;font-size:12px;" required title="تاريخ انتهاء الصلاحية">
                                <button type="submit" class="btn btn-sm" style="background:#059669;color:#fff;padding:4px 8px;font-size:11px;" title="اعتماد لائق">
                                    <i data-lucide="check" style="width:12px;height:12px;"></i>
                                </button>
                            </form>
                            <!-- Conditional Approve Button -->
                            <button type="button" class="btn btn-sm" style="background:#F59E0B;color:#fff;padding:4px 8px;font-size:11px;" title="اعتماد مشروط"
                                onclick="document.getElementById('cond_form_<?= (int) $doc['id'] ?>').style.display='block'">
                                <i data-lucide="alert-triangle" style="width:12px;height:12px;"></i>
                            </button>
                            <!-- Reject Button -->
                            <button type="button" class="btn btn-sm" style="background:#DC2626;color:#fff;padding:4px 8px;font-size:11px;" title="رفض"
                                onclick="document.getElementById('reject_form_<?= (int) $doc['id'] ?>').style.display='block'">
                                <i data-lucide="x" style="width:12px;height:12px;"></i>
                            </button>
                        </div>
                        <!-- Hidden Conditional Approve Form -->
                        <form method="POST" action="/sa/players/<?= (int) $player['id'] ?>/documents/<?= (int) $doc['id'] ?>/approve-conditional" id="cond_form_<?= (int) $doc['id'] ?>" style="display:none;margin-top:8px;padding:10px;background:#FFFBEB;border-radius:6px;">
                            <?= csrf_field() ?>
                            <div style="font-size:11px;color:#92400E;font-weight:600;margin-bottom:6px;">اعتماد مشروط</div>
                            <div style="display:flex;gap:4px;flex-wrap:wrap;align-items:center;margin-bottom:6px;">
                                <input type="date" name="expiry_date" class="form-input" style="width:140px;height:30px;font-size:12px;" required title="تاريخ الانتهاء">
                            </div>
                            <div style="margin-bottom:6px;">
                                <textarea name="conditional_notes" class="form-input" style="width:100%;height:50px;font-size:12px;resize:vertical;" placeholder="ملاحظات الاعتماد المشروط..." required></textarea>
                            </div>
                            <button type="submit" class="btn btn-sm" style="background:#F59E0B;color:#fff;padding:4px 10px;font-size:11px;">اعتماد مشروط</button>
                        </form>
                        <!-- Hidden Reject Form -->
                        <form method="POST" action="/sa/players/<?= (int) $player['id'] ?>/documents/<?= (int) $doc['id'] ?>/reject" id="reject_form_<?= (int) $doc['id'] ?>" style="display:none;margin-top:8px;">
                            <?= csrf_field() ?>
                            <div style="display:flex;gap:4px;align-items:center;">
                                <input type="text" name="rejection_reason" class="form-input" style="width:200px;height:30px;font-size:12px;" placeholder="سبب الرفض..." required>
                                <button type="submit" class="btn btn-sm" style="background:#DC2626;color:#fff;padding:4px 8px;font-size:11px;">رفض</button>
                            </div>
                        </form>
                        <?php endif; ?>
                    </td>
                    <?php endif; ?>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <?php else: ?>
    <div style="padding:40px 20px;text-align:center;color:#9CA3AF;">
        <i data-lucide="file-x" style="width:36px;height:36px;color:#D1D5DB;margin-bottom:10px;"></i>
        <p>لا توجد مستندات مرفوعة بعد.</p>
    </div>
    <?php endif; ?>
</div>

<!-- Upload Form -->
<div class="card">
    <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;">
        <form method="POST" action="/sa/players/<?= (int) $player['id'] ?>/documents/upload" enctype="multipart/form-data">
            <?= csrf_field() ?>
            <div style="display:grid;grid-template-columns:repeat(auto-fit, minmax(200px, 1fr));gap:15px;margin-bottom:15px;">
                <div>
                    <label class="form-label">نوع المستند <span style="color:#DC2626;">*</span></label>
                    <select name="document_type" class="form-select" required>
                        <option value="">اختر...</option>
                        <?php foreach ($docTypes as $key => $label): ?>
                        <option value="<?= e($key) ?>"><?= e($label) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div>
                    <label class="form-label">ملاحظات</label>
                    <input type="text" name="notes" class="form-input" placeholder="ملاحظات (اختياري)">
                </div>
                <div>
                    <label class="form-label">الملف <span style="color:#DC2626;">*</span></label>
                    <input type="file" name="document_file" class="form-input" required accept=".pdf,.jpg,.jpeg,.png,.doc,.docx">
                </div>
            </div>
            <button type="submit" class="btn btn-primary">
                <i data-lucide="upload" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> رفع المستند
            </button>
        </form>
    </div>
</div>

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