<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>تعديل الموظف: <?= e($employee->full_name_ar) ?><?php $__template->endSection(); ?>
<?php $__template->section('content'); ?>
<form method="POST" action="/users/<?= (int) $employee->id ?>">
    <?= csrf_field() ?>
    <div class="card" style="padding:20px;margin-bottom:20px;">
        <div style="display:grid;grid-template-columns:1fr 1fr;gap:20px;">
            <div class="form-group">
                <label class="form-label">اسم المستخدم</label>
                <input type="text" value="<?= e($employee->username) ?>" class="form-input" disabled style="background:#f3f4f6;">
            </div>
            <div class="form-group">
                <label class="form-label">كلمة مرور جديدة (اتركه فارغاً للإبقاء)</label>
                <input type="password" name="new_password" class="form-input" minlength="8">
            </div>
            <div class="form-group">
                <label class="form-label">الاسم بالعربي <span style="color:#DC2626;">*</span></label>
                <input type="text" name="full_name_ar" value="<?= e($employee->full_name_ar) ?>" class="form-input" required>
            </div>
            <div class="form-group">
                <label class="form-label">الاسم بالإنجليزي</label>
                <input type="text" name="full_name_en" value="<?= e($employee->full_name_en ?? '') ?>" class="form-input">
            </div>
            <div class="form-group">
                <label class="form-label">البريد الإلكتروني</label>
                <input type="email" name="email" value="<?= e($employee->email ?? '') ?>" class="form-input">
            </div>
            <div class="form-group">
                <label class="form-label">رقم الهاتف</label>
                <input type="text" name="phone" value="<?= e($employee->phone ?? '') ?>" class="form-input">
            </div>
            <div class="form-group">
                <label class="form-label">الفرع</label>
                <select name="branch_id" class="form-select">
                    <option value="">جميع الفروع</option>
                    <?php foreach ($branches as $b): ?>
                        <option value="<?= (int) $b['id'] ?>" <?= (int) $employee->branch_id === (int) $b['id'] ? 'selected' : '' ?>><?= e($b['name_ar']) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="form-group">
                <label class="form-label">الحالة</label>
                <select name="is_active" class="form-select">
                    <option value="1" <?= $employee->is_active ? 'selected' : '' ?>>نشط</option>
                    <option value="0" <?= !$employee->is_active ? 'selected' : '' ?>>معطل</option>
                </select>
            </div>
        </div>
    </div>
    <div class="card" style="padding:20px;margin-bottom:20px;">
        <div style="display:flex;align-items:center;justify-content:space-between;margin-bottom:15px;">
            <h3 style="margin:0;">الأدوار</h3>
            <a href="/users/<?= (int) $employee->id ?>/permissions" class="btn btn-outline" style="font-size:13px;">إدارة الصلاحيات التفصيلية &larr;</a>
        </div>
        <div style="display:grid;grid-template-columns:repeat(auto-fill, minmax(320px, 1fr));gap:10px;">
            <?php foreach ($allRoles as $role): ?>
                <?php $checked = in_array($role['id'], $currentRoleIds); ?>
                <div style="display:flex;align-items:center;gap:10px;padding:6px 10px;border:1px solid <?= $checked ? '#059669' : '#E5E7EB' ?>;border-radius:6px;">
                    <input type="checkbox" name="roles[]" value="<?= (int) $role['id'] ?>"
                        <?= $checked ? 'checked' : '' ?> id="erole_<?= (int) $role['id'] ?>">
                    <label for="erole_<?= (int) $role['id'] ?>" style="cursor:pointer;flex:1;font-size:14px;"><?= e($role['name_ar']) ?></label>
                    <input type="date" name="role_expires[<?= (int) $role['id'] ?>]"
                        value="<?= e(isset($roleExpirations[(int) $role['id']]) ? substr($roleExpirations[(int) $role['id']], 0, 10) : '') ?>"
                        style="width:130px;font-size:12px;padding:3px 5px;border:1px solid #D1D5DB;border-radius:4px;"
                        title="انتهاء (اختياري)">
                </div>
            <?php endforeach; ?>
        </div>
    </div>
    <button type="submit" class="btn btn-primary">حفظ التعديلات</button>
    <a href="/users" class="btn btn-outline">إلغاء</a>
</form>
<?php $__template->endSection(); ?>