<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>سجل الحضور<?php $__template->endSection(); ?>
<?php $__template->section('page_actions'); ?>
<?php if (can('hr.attendance.manage')): ?>
<a href="/hr/attendance/daily" class="btn btn-primary" style="display:inline-flex;align-items:center;gap:6px;"><i data-lucide="calendar-clock" style="width:16px;height:16px;"></i> تسجيل يومي</a>
<?php endif; ?>
<?php $__template->endSection(); ?>
<?php $__template->section('content'); ?>
<div class="card" style="margin-bottom:20px;">
    <form method="GET" action="/hr/attendance" 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="department_id" class="form-control"><option value="">الكل</option>
            <?php foreach ($departments as $d): ?><option value="<?= (int) $d['id'] ?>" <?= $filters['department_id'] == $d['id'] ? 'selected' : '' ?>><?= e($d['name_ar']) ?></option><?php endforeach; ?>
            </select>
        </div>
        <div style="min-width:120px;"><label style="display:block;margin-bottom:4px;font-size:13px;color:#6B7280;">الحالة</label>
            <select name="status" class="form-control"><option value="">الكل</option>
            <option value="present" <?= $filters['status'] === 'present' ? 'selected' : '' ?>>حاضر</option>
            <option value="absent" <?= $filters['status'] === 'absent' ? 'selected' : '' ?>>غائب</option>
            <option value="late" <?= $filters['status'] === 'late' ? 'selected' : '' ?>>متأخر</option>
            <option value="leave" <?= $filters['status'] === 'leave' ? 'selected' : '' ?>>إجازة</option>
            </select>
        </div>
        <button type="submit" class="btn btn-secondary">بحث</button>
    </form>
</div>
<form method="POST" action="/hr/attendance/approve">
    <?= csrf_field() ?>
    <div class="card">
        <div class="table-responsive">
            <table class="data-table">
                <thead><tr><th><input type="checkbox" id="selectAll"></th><th>التاريخ</th><th>الموظف</th><th>دخول</th><th>خروج</th><th>الساعات</th><th>الحالة</th><th>معتمد</th><th>إجراءات</th></tr></thead>
                <tbody>
                    <?php if (empty($records)): ?>
                    <tr><td colspan="9" style="text-align:center;padding:40px;color:#9CA3AF;">لا توجد سجلات</td></tr>
                    <?php else: ?>
                    <?php foreach ($records as $r): ?>
                    <?php $statusLabels = ['present' => 'حاضر', 'absent' => 'غائب', 'late' => 'متأخر', 'leave' => 'إجازة', 'holiday' => 'عطلة', 'rest_day' => 'يوم راحة', 'half_day' => 'نصف يوم']; ?>
                    <?php $statusColors = ['present' => '#DEF7EC;color:#03543F', 'absent' => '#FDE8E8;color:#9B1C1C', 'late' => '#FEF3C7;color:#92400E', 'leave' => '#DBEAFE;color:#1E40AF', 'holiday' => '#E5E7EB;color:#374151', 'rest_day' => '#E5E7EB;color:#374151', 'half_day' => '#FEF3C7;color:#92400E']; ?>
                    <tr>
                        <td><?php if (!$r['is_approved']): ?><input type="checkbox" name="ids[]" value="<?= (int) $r['id'] ?>"><?php endif; ?></td>
                        <td><?= e($r['attendance_date']) ?></td>
                        <td><a href="/hr/attendance/monthly/<?= (int) $r['employee_profile_id'] ?>"><?= e($r['first_name_ar'] . ' ' . $r['last_name_ar']) ?></a></td>
                        <td><?= e($r['check_in_time'] ?? '-') ?></td>
                        <td><?= e($r['check_out_time'] ?? '-') ?></td>
                        <td><?= number_format((float) ($r['actual_hours'] ?? 0), 1) ?></td>
                        <td><span style="display:inline-block;padding:2px 8px;border-radius:9999px;font-size:12px;background:<?= $statusColors[$r['status']] ?? '#E5E7EB;color:#374151' ?>;"><?= e($statusLabels[$r['status']] ?? $r['status']) ?></span></td>
                        <td><?= $r['is_approved'] ? '<span style="color:#059669;">&#10003;</span>' : '<span style="color:#9CA3AF;">-</span>' ?></td>
                        <td><?php if (can('hr.attendance.manage')): ?><a href="/hr/attendance/<?= (int) $r['id'] ?>/edit" style="color:#2563EB;"><i data-lucide="edit" style="width:16px;height:16px;"></i></a><?php endif; ?></td>
                    </tr>
                    <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
        <div style="padding:12px;border-top:1px solid #E5E7EB;display:flex;justify-content:space-between;align-items:center;">
            <?php if (can('hr.attendance.approve')): ?>
            <button type="submit" class="btn btn-primary">اعتماد المحدد</button>
            <?php else: ?>
            <div></div>
            <?php endif; ?>
            <?php if (!empty($pagination)): ?>
            <?php $__template->include('Shared.Components.pagination', ['pagination' => $pagination]); ?>
            <?php endif; ?>
        </div>
    </div>
</form>
<script>
if(typeof lucide!=='undefined')lucide.createIcons();
document.getElementById('selectAll')?.addEventListener('change',function(){document.querySelectorAll('input[name="ids[]"]').forEach(c=>c.checked=this.checked);});
</script>
<?php $__template->endSection(); ?>
