<?php $__template->layout('Layout.main'); ?>
<?php $isIdara = ($department->department_type ?? 'qism') === 'idara'; ?>
<?php $__template->section('title'); ?><?= $isIdara ? 'الإدارة' : 'القسم' ?>: <?= 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> بيانات <?= $isIdara ? 'الإدارة' : 'القسم' ?>
        </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>
            <?php if ($isIdara): ?>
            <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; ?>
        </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>
        <?php if (!$isIdara): ?>
        <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>
        <?php endif; ?>
        <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($positions)): ?>
<div class="card" style="margin-bottom:20px;">
    <div style="padding:16px;border-bottom:1px solid #E5E7EB;display:flex;align-items:center;justify-content:space-between;">
        <h3 style="margin:0;font-size:16px;display:flex;align-items:center;gap:8px;">
            <i data-lucide="users" style="width:18px;height:18px;color:#6B7280;"></i> القوام والقوة الفعلية
        </h3>
        <?php if (can('hr.department.manage')): ?>
        <a href="/hr/departments/<?= (int) $department->id ?>/positions" class="btn btn-sm btn-primary">إدارة الوظائف</a>
        <?php endif; ?>
    </div>
    <div class="table-responsive">
        <table class="data-table">
            <thead>
                <tr>
                    <th>المسمى الوظيفي</th>
                    <th>القوام المعتمد</th>
                    <th>القوة الفعلية</th>
                    <th>النواقص</th>
                </tr>
            </thead>
            <tbody>
                <?php
                $totalAuthorized = 0;
                $totalActual = 0;
                ?>
                <?php foreach ($positions as $pos): ?>
                <?php
                $authorized = (int) $pos['authorized_count'];
                $actual = (int) $pos['actual_count'];
                $deficit = $authorized - $actual;
                $totalAuthorized += $authorized;
                $totalActual += $actual;
                ?>
                <tr>
                    <td><?= e($pos['job_title_name']) ?></td>
                    <td><?= $authorized ?></td>
                    <td><?= $actual ?></td>
                    <td>
                        <?php if ($deficit > 0): ?>
                        <span style="color:#DC2626;font-weight:600;"><?= $deficit ?></span>
                        <?php elseif ($deficit < 0): ?>
                        <span style="color:#059669;font-weight:600;"><?= abs($deficit) ?>+</span>
                        <?php else: ?>
                        <span style="color:#6B7280;">0</span>
                        <?php endif; ?>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
            <tfoot>
                <tr style="font-weight:700;background:#F9FAFB;">
                    <td>الإجمالي</td>
                    <td><?= $totalAuthorized ?></td>
                    <td><?= $totalActual ?></td>
                    <td>
                        <?php $totalDeficit = $totalAuthorized - $totalActual; ?>
                        <?php if ($totalDeficit > 0): ?>
                        <span style="color:#DC2626;"><?= $totalDeficit ?></span>
                        <?php elseif ($totalDeficit < 0): ?>
                        <span style="color:#059669;"><?= abs($totalDeficit) ?>+</span>
                        <?php else: ?>
                        <span style="color:#6B7280;">0</span>
                        <?php endif; ?>
                    </td>
                </tr>
            </tfoot>
        </table>
    </div>
</div>
<?php elseif (can('hr.department.manage')): ?>
<div class="card" style="margin-bottom:20px;">
    <div style="padding:16px;display:flex;align-items:center;justify-content:space-between;">
        <span style="color:#6B7280;">لم يتم تحديد القوام المعتمد لهذا القسم بعد</span>
        <a href="/hr/departments/<?= (int) $department->id ?>/positions" class="btn btn-sm btn-primary">إدارة الوظائف</a>
    </div>
</div>
<?php endif; ?>

<?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;"><?= $isIdara ? 'الأقسام التابعة' : 'الأقسام الفرعية' ?> (<?= 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('هل أنت متأكد من حذف <?= $isIdara ? 'هذه الإدارة' : 'هذا القسم' ?>؟');">
        <?= csrf_field() ?>
        <button type="submit" class="btn btn-danger">حذف <?= $isIdara ? 'الإدارة' : 'القسم' ?></button>
    </form>
</div>
<?php endif; ?>
<script>if(typeof lucide!=='undefined')lucide.createIcons();</script>
<?php $__template->endSection(); ?>
