<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>سجل البصمات<?php $__template->endSection(); ?>
<?php $__template->section('page_actions'); ?>
<?php if (can('hr.biometric.manage')): ?>
<form method="POST" action="/hr/biometric/process-all" style="display:inline;">
    <?= csrf_field() ?>
    <button type="submit" class="btn btn-primary" style="display:inline-flex;align-items:center;gap:6px;" onclick="return confirm('هل تريد معالجة جميع البصمات المعلقة؟')"><i data-lucide="play" style="width:16px;height:16px;"></i> معالجة الكل</button>
</form>
<?php endif; ?>
<?php $__template->endSection(); ?>
<?php $__template->section('content'); ?>
<div class="card" style="margin-bottom:20px;">
    <form method="GET" action="/hr/biometric/punches" style="display:flex;gap:10px;flex-wrap:wrap;align-items:end;padding:16px;">
        <div style="min-width:160px;">
            <label style="display:block;margin-bottom:4px;font-size:13px;color:#6B7280;">من تاريخ</label>
            <input type="date" name="date_from" value="<?= e($filters['date_from']) ?>" class="form-control">
        </div>
        <div style="min-width:160px;">
            <label style="display:block;margin-bottom:4px;font-size:13px;color:#6B7280;">إلى تاريخ</label>
            <input type="date" name="date_to" value="<?= e($filters['date_to']) ?>" class="form-control">
        </div>
        <div style="min-width:160px;">
            <label style="display:block;margin-bottom:4px;font-size:13px;color:#6B7280;">الجهاز</label>
            <select name="device_id" class="form-control">
                <option value="">الكل</option>
                <?php foreach ($devices as $d): ?>
                <option value="<?= (int) $d['id'] ?>" <?= $filters['device_id'] == $d['id'] ? 'selected' : '' ?>><?= e($d['device_name']) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div style="min-width:140px;">
            <label style="display:block;margin-bottom:4px;font-size:13px;color:#6B7280;">حالة المعالجة</label>
            <select name="processed" class="form-control">
                <option value="">الكل</option>
                <option value="0" <?= $filters['processed'] === '0' ? 'selected' : '' ?>>غير معالج</option>
                <option value="1" <?= $filters['processed'] === '1' ? '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>
                </tr>
            </thead>
            <tbody>
                <?php if (empty($punches)): ?>
                <tr><td colspan="6" style="text-align:center;padding:40px;color:#9CA3AF;">لا توجد سجلات</td></tr>
                <?php else: ?>
                <?php
                    $punchTypeLabels = ['in' => 'دخول', 'out' => 'خروج', 'unknown' => 'غير محدد'];
                ?>
                <?php foreach ($punches as $p): ?>
                <tr>
                    <td><?= e($p['punch_time']) ?></td>
                    <td><code><?= e($p['fingerprint_code']) ?></code></td>
                    <td>
                        <?php if (!empty($p['first_name_ar'])): ?>
                        <?= e($p['first_name_ar'] . ' ' . $p['last_name_ar']) ?>
                        <small style="color:#6B7280;">(<?= e($p['employee_number']) ?>)</small>
                        <?php else: ?>
                        <span style="color:#DC2626;">غير معروف</span>
                        <?php endif; ?>
                    </td>
                    <td>
                        <?php
                            $typeColor = match($p['punch_type']) {
                                'in' => '#DEF7EC;color:#03543F',
                                'out' => '#DBEAFE;color:#1E40AF',
                                default => '#E5E7EB;color:#374151',
                            };
                        ?>
                        <span style="display:inline-block;padding:2px 8px;border-radius:9999px;font-size:12px;background:<?= $typeColor ?>;"><?= e($punchTypeLabels[$p['punch_type']] ?? $p['punch_type']) ?></span>
                    </td>
                    <td><?= e($p['device_name']) ?></td>
                    <td>
                        <?php if ($p['processed']): ?>
                        <?php if ($p['error_message']): ?>
                        <span style="display:inline-block;padding:2px 8px;border-radius:9999px;font-size:12px;background:#FEF3C7;color:#92400E;" title="<?= e($p['error_message']) ?>">خطأ</span>
                        <?php else: ?>
                        <span style="display:inline-block;padding:2px 8px;border-radius:9999px;font-size:12px;background:#DEF7EC;color:#03543F;">تمت المعالجة</span>
                        <?php endif; ?>
                        <?php else: ?>
                        <span style="display:inline-block;padding:2px 8px;border-radius:9999px;font-size:12px;background:#FDE8E8;color:#9B1C1C;">معلق</span>
                        <?php endif; ?>
                    </td>
                </tr>
                <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
    <?php if (!empty($pagination) && $pagination['last_page'] > 1): ?>
    <div style="padding:12px;border-top:1px solid #E5E7EB;display:flex;justify-content:center;">
        <?php $__template->include('Shared.Components.pagination', ['pagination' => $pagination]); ?>
    </div>
    <?php endif; ?>
</div>
<script>if(typeof lucide!=='undefined')lucide.createIcons();</script>
<?php $__template->endSection(); ?>
