@extends('layouts.app')

@section('title', 'Edit SK Tim Penguji')

@section('content')
<div class="card">
    <div class="card-header"><h3 class="card-title">Edit SK Tim Penguji (P4)</h3></div>
    <div class="card-body">
        <form action="{{ route('p4.tim_penguji.update', $p4->id) }}" method="POST">
            @csrf
            @method('PUT')
            <div class="form-group row">
                <label class="col-sm-3 col-form-label">Mahasiswa</label>
                <div class="col-sm-9">
                    <select name="nim_mahasiswa" class="form-control" required>
                        @foreach($mahasiswa as $m)
                        <option value="{{ $m->nim }}" {{ $p4->nim_mahasiswa == $m->nim ? 'selected' : '' }}>{{ $m->nim }} - {{ $m->nama }}</option>
                        @endforeach
                    </select>
                </div>
            </div>
            <div class="form-group row">
                <label class="col-sm-3 col-form-label">Judul Skripsi</label>
                <div class="col-sm-9"><textarea name="judul_skripsi" class="form-control" required>{{ $p4->judul_skripsi }}</textarea></div>
            </div>

            <hr>
            <h4>Susunan Tim Penguji</h4>
            @for ($i = 1; $i <= 5; $i++)
            <div class="form-group row">
                <label class="col-sm-3 col-form-label">Penguji {{ $i }} {{ $i == 1 ? '(Ketua)' : ($i == 2 ? '(Sekretaris)' : '(Anggota)') }}</label>
                <div class="col-sm-9">
                    <select name="nip_penguji{{ $i }}" class="form-control" {{ $i <= 2 ? 'required' : '' }}>
                        <option value="">-- Pilih Dosen --</option>
                        @foreach($dosen as $d)
                        @php $fieldName = 'nip_penguji' . $i; @endphp
                        <option value="{{ $d->nip }}" {{ $p4->$fieldName == $d->nip ? 'selected' : '' }}>{{ $d->nip }} - {{ $d->nama }}</option>
                        @endforeach
                    </select>
                </div>
            </div>
            @endfor

            <button type="submit" class="btn btn-primary">Simpan Perubahan</button>
            <a href="{{ route('p4.tim_penguji.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

