<?php $__template->layout('Layout.main'); ?>
<?php $isEdit = $department !== null; ?>
<?php $currentType = old('department_type') ?? ($isEdit ? ($department->department_type ?? 'qism') : 'qism'); ?>
<?php $__template->section('title'); ?><?= $isEdit ? ($currentType === 'idara' ? 'تعديل الإدارة' : 'تعديل القسم') : 'إضافة إدارة / قسم' ?><?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;">
            <div style="display:flex;gap:20px;">
                <label style="display:flex;align-items:center;gap:8px;cursor:pointer;padding:12px 20px;border:2px solid #E5E7EB;border-radius:8px;" id="label_idara">
                    <input type="radio" name="department_type" value="idara" <?= $currentType === 'idara' ? 'checked' : '' ?> onchange="toggleType()">
                    <span style="font-weight:600;font-size:15px;">إدارة</span>
                    <span style="color:#6B7280;font-size:13px;">(المستوى الأعلى)</span>
                </label>
                <label style="display:flex;align-items:center;gap:8px;cursor:pointer;padding:12px 20px;border:2px solid #E5E7EB;border-radius:8px;" id="label_qism">
                    <input type="radio" name="department_type" value="qism" <?= $currentType === 'qism' ? 'checked' : '' ?> onchange="toggleType()">
                    <span style="font-weight:600;font-size:15px;">قسم</span>
                    <span style="color:#6B7280;font-size:13px;">(تابع لإدارة)</span>
                </label>
            </div>
        </div>
    </div>

    <div class="card" style="margin-bottom:20px;">
        <div style="padding:16px;border-bottom:1px solid #E5E7EB;">
            <h3 style="margin:0;font-size:16px;" id="formTitle"><?= $currentType === 'idara' ? 'بيانات الإدارة' : 'بيانات القسم' ?></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;" id="nameLabel"><?= $currentType === 'idara' ? 'اسم الإدارة بالعربي' : 'اسم القسم بالعربي' ?> <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;" id="nameEnLabel"><?= $currentType === 'idara' ? 'اسم الإدارة بالإنجليزي' : 'اسم القسم بالإنجليزي' ?></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 id="parentField" style="<?= $currentType === 'idara' ? 'display:none;' : '' ?>">
                <label style="display:block;margin-bottom:4px;font-size:13px;">الإدارة التابع لها <span style="color:#DC2626;">*</span></label>
                <select name="parent_id" class="form-control" id="parentSelect">
                    <option value="">-- اختر الإدارة --</option>
                    <?php foreach ($idaras 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>
                <input type="number" name="staffing_capacity" value="<?= e(old('staffing_capacity') ?? ($isEdit ? $department->staffing_capacity : '')) ?>" class="form-control" min="0" placeholder="0">
            </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>
function toggleType() {
    var isIdara = document.querySelector('input[name="department_type"]:checked').value === 'idara';
    document.getElementById('parentField').style.display = isIdara ? 'none' : '';
    document.getElementById('formTitle').textContent = isIdara ? 'بيانات الإدارة' : 'بيانات القسم';
    document.getElementById('nameLabel').innerHTML = (isIdara ? 'اسم الإدارة بالعربي' : 'اسم القسم بالعربي') + ' <span style="color:#DC2626;">*</span>';
    document.getElementById('nameEnLabel').textContent = isIdara ? 'اسم الإدارة بالإنجليزي' : 'اسم القسم بالإنجليزي';
    document.getElementById('label_idara').style.borderColor = isIdara ? '#2563EB' : '#E5E7EB';
    document.getElementById('label_qism').style.borderColor = isIdara ? '#E5E7EB' : '#2563EB';
    if (isIdara) document.getElementById('parentSelect').value = '';
}
toggleType();
if(typeof lucide!=='undefined')lucide.createIcons();
</script>
<?php $__template->endSection(); ?>
