<div class="row justify-content-center">
    <div class="col-lg-8">
        <div class="card">
            <div class="card-header">
                <h5 class="mb-0"><i class="fas fa-user me-2"></i><?= $user ? 'Edit User' : 'Create User' ?></h5>
            </div>
            <div class="card-body">
                <form method="POST" action="<?= $user ? '/admin/users/' . $user['id'] : '/admin/users' ?>" data-ajax="true">
                    <?= csrf_field() ?>
                    <div class="row g-3">
                        <div class="col-md-6">
                            <label class="form-label">Username <span class="text-danger">*</span></label>
                            <input type="text" name="username" class="form-control" value="<?= e($user['username'] ?? '') ?>" required <?= $user ? 'readonly' : '' ?>>
                            <div class="invalid-feedback" data-field="username"></div>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label">Email <span class="text-danger">*</span></label>
                            <input type="email" name="email" class="form-control" value="<?= e($user['email'] ?? '') ?>" required>
                            <div class="invalid-feedback" data-field="email"></div>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label">Full Name (EN) <span class="text-danger">*</span></label>
                            <input type="text" name="full_name_en" class="form-control" value="<?= e($user['full_name_en'] ?? '') ?>" required>
                            <div class="invalid-feedback" data-field="full_name_en"></div>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label">Full Name (AR)</label>
                            <input type="text" name="full_name_ar" class="form-control" value="<?= e($user['full_name_ar'] ?? '') ?>">
                        </div>
                        <div class="col-md-6">
                            <label class="form-label">Password <?= $user ? '' : '<span class="text-danger">*</span>' ?></label>
                            <input type="password" name="password" class="form-control" <?= $user ? '' : 'required' ?> minlength="8">
                            <small class="text-muted"><?= $user ? 'Leave blank to keep current password' : 'Minimum 8 characters' ?></small>
                            <div class="invalid-feedback" data-field="password"></div>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label">Phone</label>
                            <input type="text" name="phone" class="form-control" value="<?= e($user['phone'] ?? '') ?>">
                        </div>
                        <div class="col-12">
                            <label class="form-label">Roles</label>
                            <div class="row">
                                <?php foreach ($roles as $role): ?>
                                <div class="col-md-4">
                                    <div class="form-check">
                                        <input class="form-check-input" type="checkbox" name="roles[]" value="<?= $role['id'] ?>" id="role-<?= $role['id'] ?>"
                                            <?= in_array($role['id'], $userRoles) ? 'checked' : '' ?>>
                                        <label class="form-check-label" for="role-<?= $role['id'] ?>"><?= e($role['role_name_en']) ?></label>
                                    </div>
                                </div>
                                <?php endforeach; ?>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-check form-switch">
                                <input class="form-check-input" type="checkbox" name="is_active" value="1" id="is_active" <?= ($user['is_active'] ?? 1) ? 'checked' : '' ?>>
                                <label class="form-check-label" for="is_active">Active</label>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-check form-switch">
                                <input class="form-check-input" type="checkbox" name="must_change_password" value="1" id="must_change_password" <?= ($user['must_change_password'] ?? 0) ? 'checked' : '' ?>>
                                <label class="form-check-label" for="must_change_password">Force Password Change</label>
                            </div>
                        </div>
                    </div>
                    <div class="mt-4 d-flex gap-2">
                        <button type="submit" class="btn btn-primary"><i class="fas fa-save me-1"></i>Save</button>
                        <a href="/admin/users" class="btn btn-secondary">Cancel</a>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>