<?php $__env->startSection('heading', 'User'); ?>



<?php $__env->startSection('content'); ?>


<style>
    .warning {
        background-color: yellow;
    }

</style>


<!-- Content Header (Page header) -->
<section class="content-header">
    <div class="container-fluid">
        <div class="row mb-2">
            <div class="col-sm-6">
                <h1> Users </h1>
            </div>
            <div class="col-sm-6">
                <ol class="breadcrumb float-sm-right">
                    <li class="breadcrumb-item">
                        <a href="<?php echo e(route('home')); ?>">Dashboard</a>
                    </li>
                    <li class="breadcrumb-item active"> Users </li>
                </ol>
            </div>
        </div>
    </div>
</section>

<!-- Main content -->
<section class="content">
    <div class="container-fluid">
        <div class="row">
            <div class="col-12">
                <div class="card">
                    <div class="card-header">
                        <h3 class="card-title"> <b>Users</b> </h3>
                        <div class="card-tools">
                            <?php if (app(\Illuminate\Contracts\Auth\Access\Gate::class)->check('users.create')): ?>
                                <button type="button" class="btn btn-success text-white btn-sm m-1" title="Tambah" data-toggle="modal" data-target="#modal-tambah">
                                    <i class="fas fa-plus-circle"></i> Tambah
                                </button>
                                <div class="modal fade" id="modal-tambah">
                                    <div class="modal-dialog modal-xl">
                                        <div class="modal-content">
                                            <div class="modal-header">
                                                <h4 class="modal-title"> Tambah User </h4>
                                                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                                    <span aria-hidden="true">&times;</span>
                                                </button>
                                            </div>
                                            <form action="<?php echo e(route('user.save')); ?>" method="POST" enctype="multipart/form-data" id="form-tambah-user">
                                                <?php echo csrf_field(); ?>
                                                <div class="modal-body">
                                                    <div class="form-group">
                                                        <label>Nama User <span class="text-danger">*</span> </label>
                                                        <input type="text" name="name" id="name-tambah" value="<?php echo e(old('name')); ?>" placeholder="Masukkan Nama User" class="form-control <?php $__errorArgs = ['name'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> is-invalid <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>" required>
                                                        <?php $__errorArgs = ['name'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
                                                        <div class="invalid-feedback" style="display: block">
                                                            <?php echo e($message); ?>

                                                        </div>
                                                        <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                                                    </div>
                                                    <div class="form-group">
                                                        <label>Email <span class="text-danger">*</span> </label>
                                                        <input type="email" name="email" id="email-tambah"  value="<?php echo e(old('email')); ?>" placeholder="Masukkan Email" class="form-control <?php $__errorArgs = ['email'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> is-invalid <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>" required>
                                                        <?php $__errorArgs = ['email'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
                                                        <div class="invalid-feedback" style="display: block">
                                                            <?php echo e($message); ?>

                                                        </div>
                                                        <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                                                    </div>
                                                    <div class="row">
                                                        <div class="col-md-6">
                                                            <div class="form-group">
                                                                <label>Password <span class="text-danger">*</span></label>
                                                                <input type="password" name="password" id="password-tambah" value="<?php echo e(old('password')); ?>" placeholder="Masukkan Password" class="form-control <?php $__errorArgs = ['password'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> is-invalid <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>" required>
                                                                <?php $__errorArgs = ['password'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
                                                                <div class="invalid-feedback" style="display: block">
                                                                    <?php echo e($message); ?>

                                                                </div>
                                                                <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                                                            </div>
                                                        </div>
                                                        <div class="col-md-6">
                                                            <div class="form-group">
                                                                <label>Konfirmasi Password <span class="text-danger">*</span></label>
                                                                <input type="password" name="password_confirmation" id="password-confirmation-tambah" value="<?php echo e(old('password_confirmation')); ?>" placeholder="Masukkan Konfirmasi Password" class="form-control" required>
                                                            </div>
                                                        </div>
                                                    </div>
                                                    <div class="form-group">
                                                        <label class="font-weight-bold">Role</label>
                                                        <?php $__currentLoopData = $roles; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $role): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                                        <div class="form-check form-check-inline">
                                                            <input class="form-check-input" type="checkbox" name="role[]" value="<?php echo e($role->name); ?>" id="check-<?php echo e($role->id); ?>">
                                                            <label class="form-check-label" for="check-<?php echo e($role->id); ?>">
                                                                <?php echo e($role->name); ?>

                                                            </label>
                                                        </div>
                                                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                                    </div>
                                                    <div class="modal-footer">
                                                        <button type="submit" class="btn btn-primary btn-submit">
                                                            <i class="fa fa-paper-plane"></i> Simpan
                                                        </button>
                                                        <button class="btn btn-warning text-white btn-reset" type="reset">
                                                            <i class="fa fa-redo"></i> Reset
                                                        </button>
                                                        <button type="button" class="btn btn-default" data-dismiss="modal">
                                                            <i class="fas fa-undo"></i> Kembali
                                                        </button>
                                                    </div>
                                                </div>
                                            </form>
                                        </div>
                                    </div>
                                </div>
                            <?php endif; ?>
                        </div>
                    </div>
                    <div class="card-body">
                        <table id="example1" class="table table-bordered table-striped">
                            <thead>
                                <tr>
                                    <th scope="col" style="text-align: center;width: 6%">NO.</th>
                                    <th scope="col">Nama User</th>
                                    <th scope="col">Role</th>
                                    <th scope="col" style="width: 15%;text-align: center">Aksi</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php $nomer = 1; ?>
                                <?php $__currentLoopData = $users; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $no => $user): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <tr>
                                    <th><?php echo e($nomer++); ?></th>
                                    <td><?php echo e($user->name); ?></td>
                                    <td>
                                        <?php if(!empty($user->getRoleNames())): ?>
                                            <?php $__currentLoopData = $user->getRoleNames(); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $role): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                                <label class="badge badge-success"><?php echo e($role); ?></label>
                                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                        <?php endif; ?>
                                    </td>
                                    <td class="text-left">
                                        <?php if (app(\Illuminate\Contracts\Auth\Access\Gate::class)->check('users.edit')): ?>
                                            <button type="button" class="btn btn-warning text-white btn-sm m-1" title="Edit" data-toggle="modal" data-target="#modal-edit-<?php echo e($user->id); ?>">
                                                <i class="fas fa-pencil-alt"></i>
                                            </button>
                                            <div class="modal fade" id="modal-edit-<?php echo e($user->id); ?>">
                                                <div class="modal-dialog modal-xl">
                                                    <div class="modal-content">
                                                        <div class="modal-header">
                                                            <h4 class="modal-title"> Edit Data </h4>
                                                            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                                                <span aria-hidden="true">&times;</span>
                                                            </button>
                                                        </div>
                                                        <form action="<?php echo e(route('user.update', $user->id)); ?>" method="POST" enctype="multipart/form-data" class="form-edit-user">
                                                            <?php echo csrf_field(); ?>
                                                            <?php echo method_field('PUT'); ?>
                                                            <div class="modal-body">
                                                                <div class="form-group">
                                                                    <label>Nama User <span class="text-danger">*</span> </label>
                                                                    <input type="text" name="name" value="<?php echo e(old('name', $user->name)); ?>"
                                                                        placeholder="Masukkan Nama User"
                                                                        class="form-control <?php $__errorArgs = ['name'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> is-invalid <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>" required>
                                                                    <?php $__errorArgs = ['name'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
                                                                    <div class="invalid-feedback" style="display: block">
                                                                        <?php echo e($message); ?>

                                                                    </div>
                                                                    <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                                                                </div>
                                                                <div class="form-group">
                                                                    <label>Email <span class="text-danger">*</span>  </label>
                                                                    <input type="email" name="email" value="<?php echo e(old('email', $user->email)); ?>"
                                                                        placeholder="Masukkan Email" class="form-control <?php $__errorArgs = ['email'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> is-invalid <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>" required>
                                                                    <?php $__errorArgs = ['email'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
                                                                    <div class="invalid-feedback" style="display: block">
                                                                        <?php echo e($message); ?>

                                                                    </div>
                                                                    <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                                                                </div>
                                                                <div class="row">
                                                                    <div class="col-md-6">
                                                                        <div class="form-group">
                                                                            <label>Password</label>
                                                                            <input type="password" name="password" value="<?php echo e(old('password')); ?>"
                                                                                placeholder="Masukkan Password"
                                                                                class="form-control <?php $__errorArgs = ['password'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> is-invalid <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>">
                                                                            <?php $__errorArgs = ['password'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
                                                                            <div class="invalid-feedback" style="display: block">
                                                                                <?php echo e($message); ?>

                                                                            </div>
                                                                            <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                                                                        </div>
                                                                    </div>
                                                                    <div class="col-md-6">
                                                                        <div class="form-group">
                                                                            <label>Konfirmasi Password</label>
                                                                            <input type="password" name="password_confirmation"
                                                                                value="<?php echo e(old('password_confirmation')); ?>"
                                                                                placeholder="Masukkan Konfirmasi Password" class="form-control">
                                                                        </div>
                                                                    </div>
                                                                </div>
                                                                <div class="form-group">
                                                                    <label class="font-weight-bold">Role</label>
                                                                    <?php $__currentLoopData = $roles; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $role): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                                                        <div class="form-check form-check-inline">
                                                                            <input class="form-check-input" type="checkbox" name="role[]" value="<?php echo e($role->name); ?>"
                                                                                id="check-<?php echo e($role->id); ?>" <?php echo e($user->roles->contains($role->id) ? 'checked' : ''); ?>>
                                                                            <label class="form-check-label" for="check-<?php echo e($role->id); ?>">
                                                                                <?php echo e($role->name); ?>

                                                                            </label>
                                                                        </div>
                                                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                                                </div>
                                                            </div>
                                                            <div class="modal-footer">
                                                                <button type="submit" class="btn btn-primary btn-submit">
                                                                    <i class="fa fa-paper-plane"></i> Simpan
                                                                </button>
                                                                <button class="btn btn-warning text-white btn-reset" type="reset">
                                                                    <i class="fa fa-redo"></i> Reset
                                                                </button>
                                                                <button type="button" class="btn btn-default" data-dismiss="modal">
                                                                    <i class="fas fa-undo"></i> Kembali
                                                                </button>
                                                            </div>
                                                        </form>
                                                    </div>
                                                </div>
                                            </div>
                                        <?php endif; ?>
                                        <?php if (app(\Illuminate\Contracts\Auth\Access\Gate::class)->check('users.delete')): ?>
                                            <form action="<?php echo e(route('user.delete', $user->id)); ?>" method="POST" style="display:inline;" class="delete-form-<?php echo e($user->id); ?>">
                                                <?php echo csrf_field(); ?>
                                                <?php echo method_field('DELETE'); ?>
                                                <button type="button" class="btn btn-danger btn-sm delete m-1" data-id="<?php echo e($user->id); ?>" data-name="<?php echo e($user->name); ?>" title="Hapus">
                                                    <i class="fas fa-trash"></i>
                                                </button>
                                            </form>
                                        <?php endif; ?>
                                    </td>
                                </tr>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>
<!-- /.content -->

<?php $__env->stopSection(); ?>

<?php $__env->startPush('scripts'); ?>
<script>
    $(document).ready(function() {
        // Auto buka modal jika ada error dari server (Form Tambah)
        <?php if($errors->any() && old('_method') === null): ?>
            $('#modal-tambah').modal('show');
        <?php endif; ?>

        // Validasi Form Tambah User
        $('#form-tambah-user').on('submit', function(e) {
            var nameInput = $('#name-tambah').val().trim();
            var emailInput = $('#email-tambah').val().trim();
            var passwordInput = $('#password-tambah').val().trim();
            var passwordConfirmInput = $('#password-confirmation-tambah').val().trim();
            var isValid = true;

            // Reset semua error terlebih dahulu
            $('.invalid-feedback:not(.d-block)').remove();

            // Validasi Nama
            if (nameInput === '') {
                e.preventDefault();
                e.stopPropagation();
                isValid = false;

                $('#name-tambah').addClass('is-invalid');
                if ($('#name-tambah').siblings('.invalid-feedback').length === 0) {
                    $('#name-tambah').after('<div class="invalid-feedback" style="display: block">Nama User wajib diisi!</div>');
                }
            }

            // Validasi Email
            if (emailInput === '') {
                e.preventDefault();
                e.stopPropagation();
                isValid = false;

                $('#email-tambah').addClass('is-invalid');
                if ($('#email-tambah').siblings('.invalid-feedback').length === 0) {
                    $('#email-tambah').after('<div class="invalid-feedback" style="display: block">Email wajib diisi!</div>');
                }
            }

            // Validasi Password
            if (passwordInput === '') {
                e.preventDefault();
                e.stopPropagation();
                isValid = false;

                $('#password-tambah').addClass('is-invalid');
                if ($('#password-tambah').siblings('.invalid-feedback').length === 0) {
                    $('#password-tambah').after('<div class="invalid-feedback" style="display: block">Password wajib diisi!</div>');
                }
            }

            // Validasi Konfirmasi Password
            if (passwordConfirmInput === '') {
                e.preventDefault();
                e.stopPropagation();
                isValid = false;

                $('#password-confirmation-tambah').addClass('is-invalid');
                if ($('#password-confirmation-tambah').siblings('.invalid-feedback').length === 0) {
                    $('#password-confirmation-tambah').after('<div class="invalid-feedback" style="display: block">Konfirmasi Password wajib diisi!</div>');
                }
            } else if (passwordInput !== '' && passwordInput !== passwordConfirmInput) {
                e.preventDefault();
                e.stopPropagation();
                isValid = false;

                $('#password-tambah').addClass('is-invalid');
                $('#password-confirmation-tambah').addClass('is-invalid');

                if ($('#password-tambah').siblings('.invalid-feedback').length === 0) {
                    $('#password-tambah').after('<div class="invalid-feedback" style="display: block">Password tidak cocok!</div>');
                }
                if ($('#password-confirmation-tambah').siblings('.invalid-feedback').length === 0) {
                    $('#password-confirmation-tambah').after('<div class="invalid-feedback" style="display: block">Konfirmasi Password tidak cocok!</div>');
                }
            }

            if (!isValid) {
                // Focus ke input pertama yang error
                if (nameInput === '') {
                    $('#name-tambah').focus();
                } else if (emailInput === '') {
                    $('#email-tambah').focus();
                } else if (passwordInput === '') {
                    $('#password-tambah').focus();
                } else if (passwordConfirmInput === '' || passwordInput !== passwordConfirmInput) {
                    $('#password-tambah').focus();
                }
                return false;
            }
        });

        // Hapus error saat user mulai mengetik
        $('#name-tambah, #email-tambah, #password-tambah, #password-confirmation-tambah').on('input', function() {
            if ($(this).val().trim() !== '') {
                $(this).removeClass('is-invalid');
                $(this).siblings('.invalid-feedback:not([class*="d-block"])').remove();
            }
        });

        // Auto buka modal edit jika ada error dari server (Form Edit)
        <?php if($errors->any() && old('_method') === 'PUT'): ?>
            <?php $__currentLoopData = $users; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $user): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                <?php if(old('_token') && request()->route('id') == $user->id): ?>
                    $('#modal-edit-<?php echo e($user->id); ?>').modal('show');
                <?php endif; ?>
            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
        <?php endif; ?>

        // Validasi Form Edit user
        $('.form-edit-user').on('submit', function(e) {
            var nameInput = $(this).find('input[name="name"]');
            var emailInput = $(this).find('input[name="email"]');
            var passwordInput = $(this).find('input[name="password"]');
            var passwordConfirmInput = $(this).find('input[name="password_confirmation"]');
            var isValid = true;

            // Reset error yang dibuat JavaScript (bukan dari server)
            $(this).find('.invalid-feedback:not([class*="d-block"])').remove();

            // Validasi Nama
            if (nameInput.val().trim() === '') {
                e.preventDefault();
                e.stopPropagation();
                isValid = false;

                nameInput.addClass('is-invalid');
                if (nameInput.siblings('.invalid-feedback').length === 0) {
                    nameInput.after('<div class="invalid-feedback" style="display: block">Nama User wajib diisi!</div>');
                }
            }

            // Validasi Email
            if (emailInput.val().trim() === '') {
                e.preventDefault();
                e.stopPropagation();
                isValid = false;

                emailInput.addClass('is-invalid');
                if (emailInput.siblings('.invalid-feedback').length === 0) {
                    emailInput.after('<div class="invalid-feedback" style="display: block">Email wajib diisi!</div>');
                }
            }

            // Validasi Password (hanya jika diisi)
            if (passwordInput.val().trim() !== '' && passwordConfirmInput.val().trim() === '') {
                e.preventDefault();
                e.stopPropagation();
                isValid = false;

                passwordConfirmInput.addClass('is-invalid');
                if (passwordConfirmInput.siblings('.invalid-feedback').length === 0) {
                    passwordConfirmInput.after('<div class="invalid-feedback" style="display: block">Konfirmasi Password wajib diisi!</div>');
                }
            } else if (passwordInput.val().trim() !== '' && passwordInput.val() !== passwordConfirmInput.val()) {
                e.preventDefault();
                e.stopPropagation();
                isValid = false;

                passwordInput.addClass('is-invalid');
                passwordConfirmInput.addClass('is-invalid');

                if (passwordInput.siblings('.invalid-feedback').length === 0) {
                    passwordInput.after('<div class="invalid-feedback" style="display: block">Password tidak cocok!</div>');
                }
                if (passwordConfirmInput.siblings('.invalid-feedback').length === 0) {
                    passwordConfirmInput.after('<div class="invalid-feedback" style="display: block">Konfirmasi Password tidak cocok!</div>');
                }
            }

            if (!isValid) {
                // Focus ke input pertama yang error
                if (nameInput.val().trim() === '') {
                    nameInput.focus();
                } else if (emailInput.val().trim() === '') {
                    emailInput.focus();
                } else if (passwordInput.val().trim() !== '' && (passwordConfirmInput.val().trim() === '' || passwordInput.val() !== passwordConfirmInput.val())) {
                    passwordInput.focus();
                }
                return false;
            }
        });

        // Hapus error saat user mulai mengetik di form edit
        $('.form-edit-user input[name="name"], .form-edit-user input[name="email"], .form-edit-user input[name="password"], .form-edit-user input[name="password_confirmation"]').on('input', function() {
            if ($(this).val().trim() !== '') {
                $(this).removeClass('is-invalid');
                $(this).siblings('.invalid-feedback:not([class*="d-block"])').remove();
            }
        });
    });
</script>
<?php $__env->stopPush(); ?>

<?php echo $__env->make('layouts.app', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH C:\xampp82\htdocs\umi-cell\resources\views/spatie/user.blade.php ENDPATH**/ ?>