<?php $__template->layout('Layout.main'); ?>
<?php $isEdit = $department !== null; ?>
<?php $__template->section('title'); ?><?= $isEdit ? 'تعديل القسم' : 'إضافة قسم جديد' ?><?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<a href="/hr/departments" class="btn btn-secondary">رجوع</a>
<?php $__template->endSection(); ?>

<?php $__template->section('content'); ?>
<form method="POST" action="<?= $isEdit ? '/hr/departments/' . (int) $department->id : '/hr/departments' ?>">
    <?= csrf_field() ?>
    <div class="card" style="margin-bottom:20px;">
        <div style="padding:16px;border-bottom:1px solid #E5E7EB;">
            <h3 style="margin:0;font-size:16px;">بيانات القسم</h3>
        </div>
        <div style="padding:16px;display:grid;grid-template-columns:repeat(auto-fit,minmax(250px,1fr));gap:16px;">
            <div>
                <label style="display:block;margin-bottom:4px;font-size:13px;">اسم القسم بالعربي <span style="color:#DC2626;">*</span></label>
                <input type="text" name="name_ar" value="<?= e(old('name_ar') ?? ($isEdit ? $department->name_ar : '')) ?>" class="form-control" required>
            </div>
            <div>
                <label style="display:block;margin-bottom:4px;font-size:13px;">اسم القسم بالإنجليزي</label>
                <input type="text" name="name_en" value="<?= e(old('name_en') ?? ($isEdit ? $department->name_en : '')) ?>" class="form-control">
            </div>
            <div>
                <label style="display:block;margin-bottom:4px;font-size:13px;">الكود <span style="color:#DC2626;">*</span></label>
                <input type="text" name="department_code" value="<?= e(old('department_code') ?? ($isEdit ? $department->department_code : '')) ?>" class="form-control" required>
            </div>
            <div>
                <label style="display:block;margin-bottom:4px;font-size:13px;">القسم الأب</label>
                <select name="parent_id" class="form-control">
                    <option value="">-- بدون --</option>
                    <?php foreach ($departments as $d): ?>
                    <?php if ($isEdit && (int) $d['id'] === (int) $department->id) continue; ?>
                    <option value="<?= (int) $d['id'] ?>" <?= (old('parent_id') ?? ($isEdit ? $department->parent_id : '')) == $d['id'] ? 'selected' : '' ?>><?= e($d['name_ar']) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div>
                <label style="display:block;margin-bottom:4px;font-size:13px;">المدير</label>
                <select name="manager_employee_id" class="form-control">
                    <option value="">-- بدون --</option>
                    <?php foreach ($employees as $emp): ?>
                    <option value="<?= (int) $emp['id'] ?>" <?= (old('manager_employee_id') ?? ($isEdit ? $department->manager_employee_id : '')) == $emp['id'] ? 'selected' : '' ?>><?= e($emp['first_name_ar'] . ' ' . $emp['last_name_ar']) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div>
                <label style="display:block;margin-bottom:4px;font-size:13px;">الفرع</label>
                <select name="branch_id" class="form-control">
                    <option value="">-- بدون --</option>
                    <?php foreach ($branches as $b): ?>
                    <option value="<?= (int) $b['id'] ?>" <?= (old('branch_id') ?? ($isEdit ? $department->branch_id : '')) == $b['id'] ? 'selected' : '' ?>><?= e($b['name_ar']) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div>
                <label style="display:block;margin-bottom:4px;font-size:13px;">الحالة</label>
                <select name="is_active" class="form-control">
                    <option value="1" <?= (old('is_active') ?? ($isEdit ? $department->is_active : 1)) == 1 ? 'selected' : '' ?>>نشط</option>
                    <option value="0" <?= (old('is_active') ?? ($isEdit ? $department->is_active : 1)) == 0 ? 'selected' : '' ?>>غير نشط</option>
                </select>
            </div>
        </div>
    </div>
    <div style="display:flex;gap:10px;justify-content:flex-end;">
        <a href="/hr/departments" class="btn btn-secondary">إلغاء</a>
        <button type="submit" class="btn btn-primary"><?= $isEdit ? 'تحديث' : 'حفظ' ?></button>
    </div>
</form>
<script>if(typeof lucide!=='undefined')lucide.createIcons();</script>
<?php $__template->endSection(); ?>
