<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>القسم: <?= e($department->name_ar) ?><?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<?php if (can('hr.department.manage')): ?>
<a href="/hr/departments/<?= (int) $department->id ?>/edit" class="btn btn-primary" style="display:inline-flex;align-items:center;gap:6px;">
    <i data-lucide="edit" style="width:16px;height:16px;"></i> تعديل
</a>
<?php endif; ?>
<a href="/hr/departments" class="btn btn-secondary">رجوع</a>
<?php $__template->endSection(); ?>

<?php $__template->section('content'); ?>
<div class="card" style="margin-bottom:20px;">
    <div style="padding:16px;border-bottom:1px solid #E5E7EB;">
        <h3 style="margin:0;font-size:16px;display:flex;align-items:center;gap:8px;">
            <i data-lucide="building-2" style="width:18px;height:18px;color:#6B7280;"></i> بيانات القسم
        </h3>
    </div>
    <div style="padding:16px;display:grid;grid-template-columns:repeat(auto-fit,minmax(220px,1fr));gap:16px;">
        <div><span style="color:#6B7280;font-size:13px;">الكود</span><div style="font-weight:600;"><?= e($department->department_code) ?></div></div>
        <div><span style="color:#6B7280;font-size:13px;">الاسم بالعربي</span><div style="font-weight:600;"><?= e($department->name_ar) ?></div></div>
        <div><span style="color:#6B7280;font-size:13px;">الاسم بالإنجليزي</span><div><?= e($department->name_en ?? '-') ?></div></div>
        <div><span style="color:#6B7280;font-size:13px;">القسم الأب</span><div><?= $parent ? '<a href="/hr/departments/' . (int) $parent->id . '">' . e($parent->name_ar) . '</a>' : '-' ?></div></div>
        <div><span style="color:#6B7280;font-size:13px;">المدير</span><div><?= $manager ? e($manager['first_name_ar'] . ' ' . $manager['last_name_ar']) : '-' ?></div></div>
        <div><span style="color:#6B7280;font-size:13px;">الفرع</span><div><?= $branch ? e($branch['name_ar']) : '-' ?></div></div>
        <div><span style="color:#6B7280;font-size:13px;">عدد الموظفين</span><div style="font-weight:600;"><?= $employeeCount ?></div></div>
        <div><span style="color:#6B7280;font-size:13px;">الحالة</span><div>
            <?php if ($department->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; ?>
        </div></div>
    </div>
</div>

<?php if (!empty($children)): ?>
<div class="card" style="margin-bottom:20px;">
    <div style="padding:16px;border-bottom:1px solid #E5E7EB;">
        <h3 style="margin:0;font-size:16px;">الأقسام الفرعية (<?= count($children) ?>)</h3>
    </div>
    <div class="table-responsive">
        <table class="data-table">
            <thead><tr><th>الكود</th><th>الاسم</th></tr></thead>
            <tbody>
                <?php foreach ($children as $child): ?>
                <tr>
                    <td><code><?= e($child['department_code']) ?></code></td>
                    <td><a href="/hr/departments/<?= (int) $child['id'] ?>"><?= e($child['name_ar']) ?></a></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
<?php endif; ?>

<?php if (can('hr.department.manage')): ?>
<div style="display:flex;gap:10px;justify-content:flex-end;">
    <form method="POST" action="/hr/departments/<?= (int) $department->id ?>/archive" onsubmit="return confirm('هل أنت متأكد من حذف هذا القسم؟');">
        <?= csrf_field() ?>
        <button type="submit" class="btn btn-danger">حذف القسم</button>
    </form>
</div>
<?php endif; ?>
<script>if(typeof lucide!=='undefined')lucide.createIcons();</script>
<?php $__template->endSection(); ?>
