@extends('layouts.app')

@section('title', 'Tambah User')

@section('content')
<div class="row">
    <div class="col-md-8 mx-auto">
        <div class="card shadow-sm">
            <div class="card-header">
                <h3 class="card-title">Form Tambah User</h3>
                <div class="card-tools">
                    <a href="{{ route('users.index') }}" class="btn btn-sm btn-secondary">Kembali</a>
                </div>
            </div>
            
            <form action="{{ route('users.store') }}" method="POST">
                @csrf
                <div class="card-body">
                    
                    @if ($errors->any())
                        <div class="alert alert-danger">
                            <ul class="mb-0">
                                @foreach ($errors->all() as $error)
                                    <li>{{ $error }}</li>
                                @endforeach
                            </ul>
                        </div>
                    @endif

                    <div class="form-group">
                        <label>Nama Lengkap <span class="text-danger">*</span></label>
                        <input type="text" name="name" class="form-control @error('name') is-invalid @enderror" value="{{ old('name') }}" required>
                    </div>

                    <div class="row">
                        <div class="col-md-6 form-group">
                            <label>Username <span class="text-danger">*</span></label>
                            <input type="text" name="username" class="form-control @error('username') is-invalid @enderror" value="{{ old('username') }}" required>
                        </div>
                        <div class="col-md-6 form-group">
                            <label>Email <span class="text-danger">*</span></label>
                            <input type="email" name="email" class="form-control @error('email') is-invalid @enderror" value="{{ old('email') }}" required>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-md-6 form-group">
                            <label>Password <span class="text-danger">*</span></label>
                            <input type="password" name="password" class="form-control @error('password') is-invalid @enderror" required minlength="8">
                        </div>
                        <div class="col-md-6 form-group">
                            <label>Konfirmasi Password <span class="text-danger">*</span></label>
                            <input type="password" name="password_confirmation" class="form-control" required minlength="8">
                        </div>
                    </div>

                    <div class="form-group">
                        <label>Role <span class="text-danger">*</span></label>
                        <select name="role_id" id="role_id" class="form-control @error('role_id') is-invalid @enderror" required>
                            <option value="">-- Pilih Role --</option>
                            @foreach($roles as $role)
                                <option value="{{ $role->id }}" data-nama="{{ $role->nama }}" {{ old('role_id') == $role->id ? 'selected' : '' }}>
                                    {{ $role->label }}
                                </option>
                            @endforeach
                        </select>
                    </div>

                    <div class="form-group" id="kelas_group" style="display: none;">
                        <label>Penempatan Kelas (Wali Kelas) <span class="text-danger">*</span></label>
                        <select name="kelas_id" id="kelas_id" class="form-control @error('kelas_id') is-invalid @enderror select2">
                            <option value="">-- Pilih Kelas --</option>
                            @foreach($kelases as $kelas)
                                <option value="{{ $kelas->id }}" {{ old('kelas_id') == $kelas->id ? 'selected' : '' }}>
                                    {{ $kelas->nama_kelas }} ({{ $kelas->jenjang }})
                                </option>
                            @endforeach
                        </select>
                    </div>

                    <div class="form-group" id="jenjang_group" style="display: none;">
                        <label>Penempatan Jenjang (Kepala Sekolah) <span class="text-danger">*</span></label>
                        <select name="jenjang" id="jenjang" class="form-control @error('jenjang') is-invalid @enderror">
                            <option value="">-- Pilih Jenjang --</option>
                            @foreach($jenjangs as $jenjang)
                                <option value="{{ $jenjang }}" {{ old('jenjang') == $jenjang ? 'selected' : '' }}>
                                    {{ $jenjang }}
                                </option>
                            @endforeach
                        </select>
                    </div>

                    <div class="form-group mb-0 mt-4">
                        <div class="custom-control custom-switch">
                            <input type="checkbox" class="custom-control-input" id="is_active" name="is_active" checked>
                            <label class="custom-control-label" for="is_active">User Aktif (Dapat Login)</label>
                        </div>
                    </div>
                </div>
                
                <div class="card-footer text-right">
                    <button type="submit" class="btn btn-primary"><i class="fas fa-save mr-1"></i> Simpan User</button>
                </div>
            </form>
        </div>
    </div>
</div>
@endsection

@push('scripts')
<script>
    $(document).ready(function() {
        $('.select2').select2({ theme: 'bootstrap4' });

        function toggleDynamicFields() {
            var selectedOption = $('#role_id').find('option:selected');
            var roleNama = selectedOption.data('nama');
            
            if (roleNama === 'wali_kelas') {
                $('#kelas_group').slideDown();
                $('#kelas_id').prop('required', true);
                
                $('#jenjang_group').slideUp();
                $('#jenjang').prop('required', false);
            } else if (roleNama === 'kepala_sekolah') {
                $('#jenjang_group').slideDown();
                $('#jenjang').prop('required', true);
                
                $('#kelas_group').slideUp();
                $('#kelas_id').prop('required', false);
            } else {
                $('#kelas_group').slideUp();
                $('#kelas_id').prop('required', false);
                
                $('#jenjang_group').slideUp();
                $('#jenjang').prop('required', false);
            }
        }

        // Run on load
        toggleDynamicFields();

        // Run on change
        $('#role_id').change(function() {
            toggleDynamicFields();
        });
    });
</script>
@endpush
