<?php $__env->startSection('content'); ?>
    <!-- HEADER -->
    <div class="d-flex justify-content-between align-items-center mb-3">
        <div>
            <h2 class="mb-0">Tambah User</h2>
            <small class="text-muted">
                Buat akun login pengguna sistem
            </small>
        </div>

        <a href="<?php echo e(route('user.index')); ?>" class="btn btn-outline-secondary rounded-3 px-4">
            <i class="bi bi-arrow-left me-1"></i> Kembali
        </a>
    </div>

    <!-- CARD -->
    <div class="card border-0 shadow-sm rounded-4">

        <!-- GOLD LINE -->
        <div style="height:4px; background:#d6a100;"></div>

        <div class="card-body p-4">

            <!-- INFO -->
            <div class="alert alert-light border rounded-3 d-flex align-items-center mb-4">
                <i class="bi bi-info-circle-fill text-warning me-2"></i>

                <div>
                    Password default user baru adalah
                    <code>123</code>
                </div>
            </div>

            <form action="<?php echo e(route('user.store')); ?>" method="POST" id="formUser">

                <?php echo csrf_field(); ?>

                <input type="hidden" name="name" id="name">
                <input type="hidden" name="nrp" id="nrp">

                <div class="row">

                    <!-- KIRI -->
                    <div class="col-lg-6 mb-4">

                        <label class="form-label fw-semibold mb-2">
                            Hubungkan Karyawan
                        </label>

                        <select class="form-select rounded-4 <?php $__errorArgs = ['karyawan_id'];
$__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); ?>" id="karyawan_id"
                            name="karyawan_id">

                            <option value="">
                                Pilih Karyawan
                            </option>

                            <?php $__currentLoopData = $karyawans; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $karyawan): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <option value="<?php echo e($karyawan->id); ?>" data-nama="<?php echo e($karyawan->nama); ?>"
                                    data-nrp="<?php echo e($karyawan->nrp); ?>"
                                    <?php echo e(old('karyawan_id') == $karyawan->id ? 'selected' : ''); ?>>

                                    <?php echo e($karyawan->nrp); ?> - <?php echo e($karyawan->nama); ?>


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

                        </select>

                        <span id="messageKaryawan" class="text-danger small mt-1 d-block"></span>

                        <?php $__errorArgs = ['karyawan_id'];
$__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 d-block">
                                <?php echo e($message); ?>

                            </div>
                        <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>

                        <div class="form-text mt-2">
                            Hanya menampilkan karyawan yang belum memiliki akun user.
                        </div>

                    </div>

                    <!-- KANAN -->
                    <div class="col-lg-6 mb-4">

                        <label class="form-label fw-semibold mb-2">
                            Role
                        </label>

                        <select class="form-select rounded-4 <?php $__errorArgs = ['role'];
$__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); ?>" id="role"
                            name="role">

                            <option value="">
                                Pilih Role
                            </option>

                            <option value="administrator" <?php echo e(old('role') == 'administrator' ? 'selected' : ''); ?>>
                                Administrator
                            </option>

                            <option value="atasan" <?php echo e(old('role') == 'atasan' ? 'selected' : ''); ?>>
                                Atasan
                            </option>

                            <option value="anak_asuh" <?php echo e(old('role') == 'anak_asuh' ? 'selected' : ''); ?>>
                                Anak asuh
                            </option>

                        </select>

                        <span id="messageRole" class="text-danger small mt-1 d-block"></span>

                        <?php $__errorArgs = ['role'];
$__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 d-block">
                                <?php echo e($message); ?>

                            </div>
                        <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>

                    </div>

                </div>

                <!-- BUTTON -->
                <div class="d-flex justify-content-end border-top pt-4">

                    <button type="submit" class="btn btn-warning rounded-4 px-4 py-2 fw-semibold shadow-sm text-dark">

                        <i class="bi bi-check-circle me-1"></i>
                        Simpan User

                    </button>

                </div>

            </form>

        </div>
    </div>

    <script>
        window.addEventListener('DOMContentLoaded', function() {

            new TomSelect("#karyawan_id", {
                create: false,
                sortField: {
                    field: "text",
                    direction: "asc"
                }
            });

            const select = document.getElementById('karyawan_id');

            if (select.value) {
                select.dispatchEvent(new Event('change'));
            }

        });

        document.getElementById('karyawan_id').addEventListener('change', function() {

            const selected = this.options[this.selectedIndex];

            document.getElementById('name').value =
                selected.getAttribute('data-nama') || '';

            document.getElementById('nrp').value =
                selected.getAttribute('data-nrp') || '';

        });

        // VALIDASI
        document.getElementById('formUser').addEventListener('submit', function(e) {

            let valid = true;

            const karyawan = document.getElementById('karyawan_id');
            const role = document.getElementById('role');

            const messageKaryawan = document.getElementById('messageKaryawan');
            const messageRole = document.getElementById('messageRole');

            // reset
            messageKaryawan.textContent = '';
            messageRole.textContent = '';

            // validasi karyawan
            if (!karyawan.value) {

                messageKaryawan.textContent =
                    'Karyawan wajib dipilih';

                karyawan.classList.add('is-invalid');

                valid = false;

            } else {

                karyawan.classList.remove('is-invalid');

            }

            // validasi role
            if (!role.value) {

                messageRole.textContent =
                    'Role wajib dipilih';

                role.classList.add('is-invalid');

                valid = false;

            } else {

                role.classList.remove('is-invalid');

            }

            if (!valid) {
                e.preventDefault();
            }

        });
    </script>
<?php $__env->stopSection(); ?>

<?php echo $__env->make('layouts.app', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH /home/u544939042/domains/mitracempaka.id/gl-asuh/resources/views/menu/user/create.blade.php ENDPATH**/ ?>