@extends('layouts.app')

@section('title', 'Buat Undangan P5')

@section('content')
<div class="card">
    <div class="card-header"><h3 class="card-title">Form Undangan Ujian (P5)</h3></div>
    <div class="card-body">
        <form action="{{ route('p5.undangan_ujian.store') }}" method="POST">
            @csrf
            <div class="form-group row">
                <label class="col-sm-2 col-form-label">No. Surat</label>
                <div class="col-sm-10"><input type="text" name="nosrt" class="form-control" value="{{ $nosrt }}" required></div>
            </div>
            <div class="form-group row">
                <label class="col-sm-2 col-form-label">Mahasiswa</label>
                <div class="col-sm-10">
                    <select name="nim" class="form-control" required>
                        @foreach($mahasiswa as $m)
                        <option value="{{ $m->nim }}">{{ $m->nim }} - {{ $m->nama }}</option>
                        @endforeach
                    </select>
                </div>
            </div>
            <div class="form-group row">
                <label class="col-sm-2 col-form-label">Judul Skripsi</label>
                <div class="col-sm-10"><textarea name="judul_skripsi" class="form-control" required></textarea></div>
            </div>
            <div class="form-group row">
                <label class="col-sm-2 col-form-label">Tanggal Ujian</label>
                <div class="col-sm-10"><input type="date" name="tanggal_ujian" class="form-control" required></div>
            </div>
            <div class="form-group row">
                <label class="col-sm-2 col-form-label">Jam</label>
                <div class="col-sm-10"><input type="time" name="jam" class="form-control" required></div>
            </div>
            <div class="form-group row">
                <label class="col-sm-2 col-form-label">Tempat</label>
                <div class="col-sm-10"><input type="text" name="tempat" class="form-control" value="Fakultas Hukum" required></div>
            </div>
            <button type="submit" class="btn btn-primary">Simpan & Cetak</button>
            <a href="{{ route('p5.undangan_ujian.index') }}" class="btn btn-secondary">Batal</a>
        </form>
    </div>
</div>
@endsection

@push('js')
<script>
    $(document).ready(function() {
        var nimSelect = $('select[name="nim_mahasiswa"], select[name="nim"]');
        nimSelect.select2({ theme: 'bootstrap4', placeholder: 'Pilih atau Cari Mahasiswa' });
        
        var dosenSelect = $('select[name^="nip_dosen"], select[name^="nip_penguji"]');
        if (dosenSelect.length > 0) {
            dosenSelect.select2({ theme: 'bootstrap4', placeholder: 'Pilih atau Cari Dosen' });
        }

        var judulTextarea = $('textarea[name="judul_skripsi"], textarea[name="judul"]');
        
        nimSelect.on('change', function() {
            var nim = $(this).val();
            if (nim) {
                $.ajax({
                    url: '/mahasiswa/detail/' + nim,
                    type: 'GET',
                    dataType: 'json',
                    success: function(data) {
                        if (data && data.judul_skripsi) {
                            judulTextarea.val(data.judul_skripsi);
                        } else {
                            judulTextarea.val('');
                        }
                    }
                });
            } else {
                judulTextarea.val('');
            }
        });
    });
</script>
@endpush

