<?php $__env->startSection('content'); ?>
    <h4 class="mb-3"><?php echo e($user ? 'Edit User' : 'Tambah User'); ?></h4>

    <form method="POST" action="<?php echo e($user ? route('admin.users.update', $user) : route('admin.users.store')); ?>">
        <?php echo csrf_field(); ?>
        <?php if($user): ?>
            <?php echo method_field('PUT'); ?>
        <?php endif; ?>

        <div class="row mb-3">
            <div class="col-md-6">
                <label class="form-label">Nama</label>
                <input type="text" name="name" class="form-control" required value="<?php echo e(old('name', $user->name ?? '')); ?>">
            </div>
            <div class="col-md-6">
                <label class="form-label">Email</label>
                <input type="email" name="email" class="form-control" required value="<?php echo e(old('email', $user->email ?? '')); ?>">
            </div>
        </div>

        <div class="row mb-3">
            <div class="col-md-6">
                <label class="form-label">Password <?php echo e($user ? '(kosongkan jika tidak diubah)' : ''); ?></label>
                <input type="password" name="password" class="form-control" <?php echo e($user ? '' : 'required'); ?>>
            </div>
        </div>

        <?php if($user): ?>
            <div class="row mb-3">
                <div class="col-md-6">
                    <label class="form-label">MFA (TOTP)</label>
                    <?php if($user->mfa_enabled_at): ?>
                        <p class="mb-2 text-success">Aktif sejak <?php echo e($user->mfa_enabled_at->format('Y-m-d H:i')); ?></p>
                        <a href="<?php echo e(route('admin.users.edit', $user)); ?>?regen_mfa=1" class="btn btn-sm btn-outline-primary" data-confirm-modal="Regenerasi secret akan menghapus kode sebelumnya. Lanjutkan?">Regenerate Secret & Recovery</a>
                        <a href="<?php echo e(route('admin.users.edit', $user)); ?>?disable_mfa=1" class="btn btn-sm btn-outline-danger ms-2" data-confirm-modal="Nonaktifkan MFA?">Nonaktifkan MFA</a>
                    <?php else: ?>
                        <p class="mb-2 text-muted">Belum aktif.</p>
                        <a href="<?php echo e(route('admin.users.edit', $user)); ?>?enable_mfa=1" class="btn btn-sm btn-outline-primary">Aktifkan MFA</a>
                    <?php endif; ?>
                </div>
            </div>

            <?php if(session('mfa_setup')): ?>
                <div class="card card-glass border-info border-opacity-25 mt-3 shadow-lg overflow-hidden">
                    <div class="card-header bg-info bg-opacity-10 py-3 border-bottom border-white border-opacity-10">
                        <h6 class="mb-0 text-info d-flex align-items-center">
                            <i class="bi bi-shield-check me-2 fs-5"></i>
                            Two-Factor Authentication Setup
                        </h6>
                    </div>
                    <div class="card-body p-4">
                        <div class="row g-4 align-items-center">
                            <div class="col-md-4 text-center">
                                <div class="bg-white p-3 d-inline-block rounded-4 shadow-sm mb-3">
                                    <img src="https://api.qrserver.com/v1/create-qr-code/?size=180x180&data=<?php echo e(urlencode(session('mfa_setup')['otpauth'])); ?>" alt="QR" class="img-fluid" style="width: 150px;">
                                </div>
                                <div class="p-2 bg-black bg-opacity-25 rounded font-monospace small text-info border border-info border-opacity-10">
                                    <?php echo e(session('mfa_setup')['secret']); ?>

                                </div>
                            </div>
                            <div class="col-md-8 border-start border-white border-opacity-10 ps-md-4">
                                <div class="mb-4">
                                    <label class="small text-uppercase fw-bold text-muted mb-2 d-block letter-spacing-1">Instructions</label>
                                    <p class="small text-muted mb-0">1. Scan the QR code with your authenticator app.</p>
                                    <p class="small text-muted">2. If you cannot scan, enter the secret key manually.</p>
                                </div>
                                
                                <div>
                                    <label class="small text-uppercase fw-bold text-muted mb-2 d-block letter-spacing-1 text-warning">
                                        <i class="bi bi-exclamation-triangle-fill me-1"></i>
                                        Recovery Codes
                                    </label>
                                    <p class="extra-small text-muted mb-3">Save these codes in a safe place. They are shown only once!</p>
                                    <div class="row g-2">
                                        <?php $__currentLoopData = session('mfa_setup')['recovery']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $code): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                            <div class="col-6 col-md-4">
                                                <div class="p-2 bg-black bg-opacity-25 rounded font-monospace small text-center border border-white border-opacity-10">
                                                    <?php echo e($code); ?>

                                                </div>
                                            </div>
                                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            <?php endif; ?>
        <?php endif; ?>

        <div class="mb-3">
            <label class="form-label">Roles</label>
            <div class="row">
                <?php $__currentLoopData = $roles; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $app => $roleGroup): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                    <div class="col-md-4 mb-2">
                        <div class="fw-semibold text-uppercase small"><?php echo e($app); ?></div>
                        <?php $__currentLoopData = $roleGroup; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $role): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                            <div class="form-check">
                                <input class="form-check-input" type="checkbox" name="roles[]"
                                       value="<?php echo e($role->id); ?>" id="role_<?php echo e($role->id); ?>"
                                       <?php echo e(in_array($role->id, old('roles', $selectedRoles)) ? 'checked' : ''); ?>>
                                <label class="form-check-label" for="role_<?php echo e($role->id); ?>">
                                    <?php echo e($role->key); ?> (<?php echo e($role->name); ?>)
                                </label>
                            </div>
                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                    </div>
                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
            </div>
        </div>

        <div class="d-flex gap-2">
            <button class="btn btn-primary"><?php echo e($user ? 'Simpan' : 'Buat'); ?></button>
            <a class="btn btn-outline-secondary" href="<?php echo e(route('admin.users.index')); ?>">Batal</a>
        </div>
    </form>
<?php $__env->stopSection(); ?>

<?php echo $__env->make('layouts.admin', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH /home/sppsynergo/public_html/sso.synergo.id/resources/views/admin/users/form.blade.php ENDPATH**/ ?>