<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>مستندات الموظفين<?php $__template->endSection(); ?>
<?php $__template->section('content'); ?>
<div class="card" style="margin-bottom:20px;">
    <form method="GET" action="/hr/documents" style="display:flex;gap:10px;flex-wrap:wrap;align-items:end;padding:16px;">
        <div style="flex:1;min-width:160px;"><label style="display:block;margin-bottom:4px;font-size:13px;color:#6B7280;">بحث</label><input type="text" name="q" value="<?= e($filters['q']) ?>" placeholder="اسم الموظف..." class="form-control"></div>
        <div style="min-width:150px;"><label style="display:block;margin-bottom:4px;font-size:13px;color:#6B7280;">نوع المستند</label>
            <select name="document_type" class="form-control"><option value="">الكل</option>
            <?php foreach ($documentTypes as $k => $v): ?><option value="<?= e($k) ?>" <?= $filters['document_type'] === $k ? 'selected' : '' ?>><?= e($v) ?></option><?php endforeach; ?>
            </select>
        </div>
        <div style="min-width:130px;"><label style="display:block;margin-bottom:4px;font-size:13px;color:#6B7280;">الصلاحية</label>
            <select name="expiring_soon" class="form-control">
                <option value="">الكل</option>
                <option value="1" <?= $filters['expiring_soon'] === '1' ? 'selected' : '' ?>>منتهية/قاربت على الانتهاء</option>
            </select>
        </div>
        <div style="min-width:130px;"><label style="display:block;margin-bottom:4px;font-size:13px;color:#6B7280;">حالة الصلاحية</label>
            <select name="validity_status" class="form-control">
                <option value="">الكل</option>
                <option value="valid" <?= ($filters['validity_status'] ?? '') === 'valid' ? 'selected' : '' ?>>سارية</option>
                <option value="expiring_soon" <?= ($filters['validity_status'] ?? '') === 'expiring_soon' ? 'selected' : '' ?>>قاربت على الانتهاء</option>
                <option value="expired" <?= ($filters['validity_status'] ?? '') === 'expired' ? 'selected' : '' ?>>منتهية</option>
            </select>
        </div>
        <button type="submit" class="btn btn-secondary">بحث</button>
    </form>
</div>
<div class="card">
    <div class="table-responsive">
        <table class="data-table">
            <thead><tr><th>الموظف</th><th>نوع المستند</th><th>العنوان</th><th>تاريخ الانتهاء</th><th>حالة الصلاحية</th><th>التحقق</th><th>إجراءات</th></tr></thead>
            <tbody>
                <?php if (empty($documents)): ?>
                <tr><td colspan="7" style="text-align:center;padding:40px;color:#9CA3AF;">لا توجد مستندات</td></tr>
                <?php else: ?>
                <?php foreach ($documents as $d): ?>
                <?php $isExpired = !empty($d['expiry_date']) && $d['expiry_date'] < date('Y-m-d'); ?>
                <?php $isExpiringSoon = !empty($d['expiry_date']) && !$isExpired && $d['expiry_date'] <= date('Y-m-d', strtotime('+30 days')); ?>
                <tr>
                    <td><a href="/hr/documents/employee/<?= (int) $d['employee_profile_id'] ?>"><?= e(($d['first_name_ar'] ?? '') . ' ' . ($d['last_name_ar'] ?? '')) ?></a></td>
                    <td><?= e($documentTypes[$d['document_type']] ?? $d['document_type']) ?></td>
                    <td><?= e($d['title_ar'] ?? '-') ?></td>
                    <td><?php if (!empty($d['expiry_date'])): ?><span style="<?= $isExpired ? 'color:#DC2626;font-weight:600;' : ($isExpiringSoon ? 'color:#D97706;font-weight:600;' : '') ?>"><?= e($d['expiry_date']) ?><?php if ($isExpired): ?> (منتهي)<?php elseif ($isExpiringSoon): ?> (قارب)<?php endif; ?></span><?php else: ?>-<?php endif; ?></td>
                    <td><?php
                        $vs = $d['validity_status'] ?? '';
                        if ($vs === 'valid'): ?><span style="display:inline-block;padding:2px 8px;border-radius:9999px;font-size:12px;background:#DEF7EC;color:#03543F;">سارية</span><?php
                        elseif ($vs === 'expiring_soon'): ?><span style="display:inline-block;padding:2px 8px;border-radius:9999px;font-size:12px;background:#FEF3C7;color:#92400E;">قاربت على الانتهاء</span><?php
                        elseif ($vs === 'expired'): ?><span style="display:inline-block;padding:2px 8px;border-radius:9999px;font-size:12px;background:#FDE8E8;color:#9B1C1C;">منتهية</span><?php
                        else: ?>-<?php endif; ?>
                    </td>
                    <td><?php if ((int) ($d['is_verified'] ?? 0)): ?><span style="color:#059669;font-weight:600;">متحقق</span><?php else: ?><span style="color:#D97706;">غير متحقق</span><?php endif; ?></td>
                    <td><a href="/hr/documents/employee/<?= (int) $d['employee_profile_id'] ?>" style="color:#2563EB;"><i data-lucide="eye" style="width:16px;height:16px;"></i></a></td>
                </tr>
                <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
    <?php if (!empty($pagination)): ?><?php $__template->include('Shared.Components.pagination', ['pagination' => $pagination]); ?><?php endif; ?>
</div>
<script>if(typeof lucide!=='undefined')lucide.createIcons();</script>
<?php $__template->endSection(); ?>
