<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>مستندات: <?= e($profile->first_name_ar . ' ' . $profile->last_name_ar) ?><?php $__template->endSection(); ?>
<?php $__template->section('page_actions'); ?><a href="/hr/documents" class="btn btn-secondary">رجوع</a><?php $__template->endSection(); ?>
<?php $__template->section('content'); ?>

<div class="card" style="margin-bottom:20px;">
    <div style="padding:16px;border-bottom:1px solid #E5E7EB;"><h3 style="margin:0;font-size:16px;">رفع مستند جديد</h3></div>
    <form method="POST" action="/hr/documents/upload" enctype="multipart/form-data" style="padding:16px;">
        <?= csrf_field() ?>
        <input type="hidden" name="employee_profile_id" value="<?= (int) $profile->id ?>">
        <div style="display:grid;grid-template-columns:repeat(auto-fit,minmax(200px,1fr));gap:12px;margin-bottom:12px;">
            <div><label style="display:block;margin-bottom:4px;font-size:13px;">نوع المستند <span style="color:#DC2626;">*</span></label>
                <select name="document_type" class="form-control" required>
                    <option value="">-- اختر --</option>
                    <?php foreach ($documentTypes as $k => $v): ?><option value="<?= e($k) ?>"><?= e($v) ?></option><?php endforeach; ?>
                </select>
            </div>
            <div><label style="display:block;margin-bottom:4px;font-size:13px;">عنوان المستند</label><input type="text" name="title_ar" class="form-control" placeholder="اختياري - يُستخدم اسم النوع كافتراضي"></div>
            <div><label style="display:block;margin-bottom:4px;font-size:13px;">تاريخ الانتهاء</label><input type="date" name="expiry_date" class="form-control"></div>
            <div><label style="display:block;margin-bottom:4px;font-size:13px;">الملف (PDF, JPG, PNG - حد 5MB)</label><input type="file" name="document_file" accept=".pdf,.jpg,.jpeg,.png" class="form-control"></div>
            <div><label style="display:block;margin-bottom:4px;font-size:13px;">ملاحظات</label><input type="text" name="notes" class="form-control"></div>
        </div>
        <button type="submit" class="btn btn-primary">رفع المستند</button>
    </form>
</div>

<div class="card">
    <div style="padding:16px;border-bottom:1px solid #E5E7EB;"><h3 style="margin:0;font-size:16px;">المستندات الحالية (<?= count($documents) ?>)</h3></div>
    <?php if (empty($documents)): ?>
    <div style="padding:40px;text-align:center;color:#9CA3AF;">لا توجد مستندات لهذا الموظف</div>
    <?php else: ?>
    <div class="table-responsive">
        <table class="data-table">
            <thead><tr><th>النوع</th><th>العنوان</th><th>الانتهاء</th><th>التحقق</th><th>إجراءات</th></tr></thead>
            <tbody>
                <?php foreach ($documents as $d): ?>
                <?php $isExpired = !empty($d['expiry_date']) && $d['expiry_date'] < date('Y-m-d'); ?>
                <tr>
                    <td><?= e($documentTypes[$d['document_type']] ?? $d['document_type']) ?></td>
                    <td><?= e($d['title_ar'] ?? '-') ?></td>
                    <td><span style="<?= $isExpired ? 'color:#DC2626;font-weight:600;' : '' ?>"><?= e($d['expiry_date'] ?? '-') ?><?php if ($isExpired): ?> (منتهي)<?php endif; ?></span></td>
                    <td>
                        <?php if ((int) ($d['is_verified'] ?? 0)): ?>
                        <span style="color:#059669;font-weight:600;">متحقق</span>
                        <?php else: ?>
                        <form method="POST" action="/hr/documents/<?= (int) $d['id'] ?>/verify" style="display:inline;"><?= csrf_field() ?><button type="submit" class="btn btn-secondary" style="padding:2px 8px;font-size:12px;">تحقق</button></form>
                        <?php endif; ?>
                    </td>
                    <td style="display:flex;gap:6px;">
                        <?php if (!empty($d['file_path'])): ?><a href="/<?= e($d['file_path']) ?>" target="_blank" style="color:#2563EB;"><i data-lucide="download" style="width:16px;height:16px;"></i></a><?php endif; ?>
                        <form method="POST" action="/hr/documents/<?= (int) $d['id'] ?>/archive" style="display:inline;" onsubmit="return confirm('هل أنت متأكد من حذف هذا المستند؟');"><?= csrf_field() ?><button type="submit" style="background:none;border:none;color:#DC2626;cursor:pointer;padding:0;"><i data-lucide="trash-2" style="width:16px;height:16px;"></i></button></form>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <?php endif; ?>
</div>
<script>if(typeof lucide!=='undefined')lucide.createIcons();</script>
<?php $__template->endSection(); ?>
