<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>إدارة القوام - <?= e($department->name_ar) ?><?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<a href="/hr/departments/<?= (int) $department->id ?>" 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="plus-circle" style="width:18px;height:18px;color:#6B7280;"></i> إضافة وظيفة معتمدة
        </h3>
    </div>
    <div style="padding:16px;">
        <form method="POST" action="/hr/departments/<?= (int) $department->id ?>/positions" style="display:flex;gap:12px;align-items:flex-end;flex-wrap:wrap;">
            <?= csrf_field() ?>
            <div style="flex:1;min-width:200px;">
                <label style="display:block;margin-bottom:4px;font-size:13px;">المسمى الوظيفي <span style="color:#DC2626;">*</span></label>
                <select name="job_title_id" class="form-control" required>
                    <option value="">-- اختر --</option>
                    <?php foreach ($jobTitles as $jt): ?>
                    <option value="<?= (int) $jt['id'] ?>"><?= e($jt['name_ar']) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div style="width:150px;">
                <label style="display:block;margin-bottom:4px;font-size:13px;">العدد المعتمد <span style="color:#DC2626;">*</span></label>
                <input type="number" name="authorized_count" class="form-control" min="1" value="1" required>
            </div>
            <button type="submit" class="btn btn-primary">حفظ</button>
        </form>
    </div>
</div>

<?php if (!empty($positions)): ?>
<div class="card" style="margin-bottom:20px;">
    <div style="padding:16px;border-bottom:1px solid #E5E7EB;">
        <h3 style="margin:0;font-size:16px;">الوظائف المعتمدة (<?= count($positions) ?>)</h3>
    </div>
    <div class="table-responsive">
        <table class="data-table">
            <thead>
                <tr>
                    <th>المسمى الوظيفي</th>
                    <th>القوام المعتمد</th>
                    <th>القوة الفعلية</th>
                    <th>النواقص</th>
                    <th>إجراءات</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($positions as $pos): ?>
                <?php
                $authorized = (int) $pos['authorized_count'];
                $actual = (int) $pos['actual_count'];
                $deficit = $authorized - $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>
                    <td>
                        <form method="POST" action="/hr/departments/<?= (int) $department->id ?>/positions/<?= (int) $pos['id'] ?>/delete" style="display:inline;" onsubmit="return confirm('هل أنت متأكد من حذف هذه الوظيفة؟');">
                            <?= csrf_field() ?>
                            <button type="submit" class="btn btn-sm btn-danger">حذف</button>
                        </form>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
<?php else: ?>
<div class="card" style="margin-bottom:20px;">
    <div style="padding:16px;text-align:center;color:#6B7280;">
        لا توجد وظائف معتمدة لهذا القسم بعد
    </div>
</div>
<?php endif; ?>
<script>if(typeof lucide!=='undefined')lucide.createIcons();</script>
<?php $__template->endSection(); ?>
