<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>الموظفين<?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<?php if (can('hr.employee.manage')): ?>
<a href="/hr/employees/create" class="btn btn-primary" style="display:inline-flex;align-items:center;gap:6px;">
    <i data-lucide="user-plus" 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/employees" style="display:flex;gap:10px;flex-wrap:wrap;align-items:end;padding:16px;">
        <div style="flex:1;min-width:200px;">
            <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: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): ?>
                <?php if (($d['department_type'] ?? '') === 'idara'): ?>
                <optgroup label="<?= e($d['name_ar']) ?>">
                    <?php foreach ($departments as $sub): ?>
                    <?php if (($sub['department_type'] ?? '') === 'qism' && (int)($sub['parent_id'] ?? 0) === (int)$d['id']): ?>
                    <option value="<?= (int) $sub['id'] ?>" <?= $filters['department_id'] == $sub['id'] ? 'selected' : '' ?>><?= e($sub['name_ar']) ?></option>
                    <?php endif; ?>
                    <?php endforeach; ?>
                </optgroup>
                <?php endif; ?>
                <?php endforeach; ?>
            </select>
        </div>
        <div style="min-width:140px;">
            <label style="display:block;margin-bottom:4px;font-size:13px;color:#6B7280;">الحالة</label>
            <select name="employment_status" class="form-control">
                <option value="">الكل</option>
                <?php foreach ($statuses as $k => $v): ?>
                <option value="<?= e($k) ?>" <?= $filters['employment_status'] === $k ? 'selected' : '' ?>><?= e($v) ?></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="employment_type" class="form-control">
                <option value="">الكل</option>
                <?php foreach ($types as $k => $v): ?>
                <option value="<?= e($k) ?>" <?= $filters['employment_type'] === $k ? 'selected' : '' ?>><?= e($v) ?></option>
                <?php endforeach; ?>
            </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><th>إجراءات</th></tr>
            </thead>
            <tbody>
                <?php if (empty($employees)): ?>
                <tr><td colspan="8" style="text-align:center;padding:40px;color:#9CA3AF;">لا يوجد موظفين</td></tr>
                <?php else: ?>
                <?php foreach ($employees as $emp): ?>
                <tr>
                    <td><code><?= e($emp['employee_number']) ?></code></td>
                    <td><a href="/hr/employees/<?= (int) $emp['id'] ?>"><?= e($emp['first_name_ar'] . ' ' . $emp['last_name_ar']) ?></a></td>
                    <td><?= e($emp['idara_name'] ?? '-') ?></td>
                    <td><?= e(($emp['department_type'] ?? '') === 'qism' ? ($emp['department_name'] ?? '-') : '-') ?></td>
                    <td><?= e($emp['job_title_name'] ?? '-') ?></td>
                    <td><?= e($emp['hire_date'] ?? '-') ?></td>
                    <td>
                        <?php
                        $statusColors = ['active' => '#DEF7EC;color:#03543F', 'on_leave' => '#FEF3C7;color:#92400E', 'suspended' => '#FDE8E8;color:#9B1C1C', 'terminated' => '#E5E7EB;color:#374151', 'resigned' => '#E5E7EB;color:#374151'];
                        $sc = $statusColors[$emp['employment_status']] ?? '#E5E7EB;color:#374151';
                        ?>
                        <span style="display:inline-block;padding:2px 8px;border-radius:9999px;font-size:12px;background:<?= $sc ?>;"><?= e($statuses[$emp['employment_status']] ?? $emp['employment_status']) ?></span>
                    </td>
                    <td style="white-space:nowrap;">
                        <a href="/hr/employees/<?= (int) $emp['id'] ?>" title="عرض" style="color:#2563EB;margin-inline-end:8px;"><i data-lucide="eye" style="width:16px;height:16px;"></i></a>
                        <?php if (can('hr.employee.manage')): ?>
                        <a href="/hr/employees/<?= (int) $emp['id'] ?>/edit" title="تعديل" style="color:#059669;"><i data-lucide="edit" style="width:16px;height:16px;"></i></a>
                        <?php endif; ?>
                    </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(); ?>
