<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>الأقسام<?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<?php if (can('hr.department.manage')): ?>
<a href="/hr/departments/create" class="btn btn-primary" style="display:inline-flex;align-items:center;gap:6px;">
    <i data-lucide="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/departments" 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="branch_id" class="form-control">
                <option value="">الكل</option>
                <?php foreach ($branches as $b): ?>
                <option value="<?= (int) $b['id'] ?>" <?= $filters['branch_id'] == $b['id'] ? 'selected' : '' ?>><?= e($b['name_ar']) ?></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>
                </tr>
            </thead>
            <tbody>
                <?php if (empty($departments)): ?>
                <tr><td colspan="6" style="text-align:center;padding:40px;color:#9CA3AF;">لا توجد أقسام</td></tr>
                <?php else: ?>
                <?php foreach ($departments as $dept): ?>
                <tr>
                    <td><code><?= e($dept['department_code']) ?></code></td>
                    <td><a href="/hr/departments/<?= (int) $dept['id'] ?>"><?= e($dept['name_ar']) ?></a></td>
                    <td><?= e($dept['parent_name'] ?? '-') ?></td>
                    <td><?= e($dept['branch_name'] ?? '-') ?></td>
                    <td>
                        <?php if ($dept['is_active']): ?>
                        <span style="display:inline-block;padding:2px 8px;border-radius:9999px;font-size:12px;background:#DEF7EC;color:#03543F;">نشط</span>
                        <?php else: ?>
                        <span style="display:inline-block;padding:2px 8px;border-radius:9999px;font-size:12px;background:#FDE8E8;color:#9B1C1C;">غير نشط</span>
                        <?php endif; ?>
                    </td>
                    <td>
                        <?php if (can('hr.department.manage')): ?>
                        <a href="/hr/departments/<?= (int) $dept['id'] ?>/edit" title="تعديل" style="color:#2563EB;margin-inline-end:8px;">
                            <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(); ?>
