@extends('layouts.app')

@section('content') 
    <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="{{ route('user.index') }}" class="btn btn-outline-secondary rounded-3 px-4">
            <i class="bi bi-arrow-left me-1"></i> Kembali
        </a>
    </div>
 
    <div class="card border-0 shadow-sm rounded-4">
 
        <div style="height:4px; background:#d6a100;"></div>

        <div class="card-body p-4">
 
            <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 akan dibuat otomatis sesuai role yang dipilih.&nbsp;&nbsp;
                </div>

                <small class="text-muted">
                    Administrator/Atasan: <code>123</code> |
                    Anak Asuh: <code>cam2026</code>
                </small>
            </div>

            <form action="{{ route('user.store') }}" method="POST" id="formUser">

                @csrf

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

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

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

                        <select class="form-select rounded-4 @error('karyawan_id') is-invalid @enderror" id="karyawan_id"
                            name="karyawan_id">

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

                            @foreach ($karyawans as $karyawan)
                                <option value="{{ $karyawan->id }}" data-nama="{{ $karyawan->nama }}"
                                    data-nrp="{{ $karyawan->nrp }}"
                                    {{ old('karyawan_id') == $karyawan->id ? 'selected' : '' }}>

                                    {{ $karyawan->nrp }} - {{ $karyawan->nama }}

                                </option>
                            @endforeach

                        </select>

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

                        @error('karyawan_id')
                            <div class="invalid-feedback d-block">
                                {{ $message }}
                            </div>
                        @enderror

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

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

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

                        <select class="form-select rounded-4 @error('role') is-invalid @enderror" id="role"
                            name="role">

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

                            <option value="administrator" {{ old('role') == 'administrator' ? 'selected' : '' }}>
                                Administrator
                            </option>

                            <option value="atasan" {{ old('role') == 'atasan' ? 'selected' : '' }}>
                                Atasan
                            </option>

                            <option value="anak_asuh" {{ old('role') == 'anak_asuh' ? 'selected' : '' }}>
                                Anak asuh
                            </option>

                        </select>

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

                        @error('role')
                            <div class="invalid-feedback d-block">
                                {{ $message }}
                            </div>
                        @enderror

                    </div>

                </div>
 
                <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') || '';

        });
 
        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');
 
            messageKaryawan.textContent = '';
            messageRole.textContent = '';
 
            if (!karyawan.value) {

                messageKaryawan.textContent =
                    'Karyawan wajib dipilih';

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

                valid = false;

            } else {

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

            }
 
            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>
@endsection
