<?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:140px;">
            <label style="display:block;margin-bottom:4px;font-size:13px;color:#6B7280;">النوع</label>
            <select name="department_type" class="form-control">
                <option value="">الكل</option>
                <option value="idara" <?= ($filters['department_type'] ?? '') === 'idara' ? 'selected' : '' ?>>إدارة</option>
                <option value="qism" <?= ($filters['department_type'] ?? '') === 'qism' ? 'selected' : '' ?>>قسم</option>
            </select>
        </div>
        <div style="min-width:160px;">
            <label style="display:block;margin-bottom:4px;font-size:13px;color:#6B7280;">الإدارة</label>
            <select name="parent_id" class="form-control">
                <option value="">الكل</option>
                <?php foreach ($idaras as $ida): ?>
                <option value="<?= (int) $ida['id'] ?>" <?= ($filters['parent_id'] ?? '') == $ida['id'] ? 'selected' : '' ?>><?= e($ida['name_ar']) ?></option>
                <?php endforeach; ?>
            </select>
        </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>
                    <th>إجراءات</th>
                </tr>
            </thead>
            <tbody>
                <?php if (empty($departments)): ?>
                <tr><td colspan="7" 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>
                        <?php if (($dept['department_type'] ?? 'qism') === 'idara'): ?>
                        <span style="display:inline-block;padding:2px 8px;border-radius:9999px;font-size:12px;background:#DBEAFE;color:#1E40AF;">إدارة</span>
                        <?php else: ?>
                        <span style="display:inline-block;padding:2px 8px;border-radius:9999px;font-size:12px;background:#F3E8FF;color:#6B21A8;">قسم</span>
                        <?php endif; ?>
                    </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(); ?>
