@extends('layouts.app')

@section('title', 'Edit Laporan Kasus - KITA AKADEMIK')
@section('page-title', 'Tindak Lanjut & Edit Kasus oleh Admin')

@section('content')
<div class="row">
    <div class="col-lg-12">
        <div class="card card-custom p-4 mb-4 border-start border-4 border-primary">
            <div class="d-flex align-items-center justify-content-between mb-3">
                <div class="d-flex align-items-center gap-3">
                    <div class="bg-primary text-white rounded-3 p-3 fs-4 d-flex align-items-center justify-content-center" style="width: 50px; height: 50px;">
                        <i class="fa-solid fa-user-shield"></i>
                    </div>
                    <div>
                        <h5 class="fw-bold mb-0">Panel Keputusan & Edit Kasus #K-{{ str_pad($laporan->id, 4, '0', STR_PAD_LEFT) }}</h5>
                        <p class="text-muted mb-0" style="font-size: 0.85rem;">Sebagai Admin (Satgas/Rektor/LLDIKTI), Anda dapat mengoreksi data aduan dan menetapkan keputusan alur proses kasus.</p>
                    </div>
                </div>
                <a href="{{ route('laporan.show', $laporan->id) }}" class="btn btn-light rounded-3 px-3">
                    <i class="fa-solid fa-arrow-left me-1"></i> Kembali ke Detail
                </a>
            </div>
        </div>
    </div>
</div>

<form action="{{ route('laporan.update', $laporan->id) }}" method="POST" id="adminEditForm">
    @csrf
    @method('PUT')

    <div class="row">
        <!-- Kolom Kiri: Koreksi Informasi Laporan -->
        <div class="col-lg-7">
            <div class="card card-custom p-4 mb-4">
                <h5 class="fw-bold mb-3 border-bottom pb-2 text-dark"><i class="fa-solid fa-pen-to-square me-1"></i> Koreksi Informasi Laporan</h5>
                
                <div class="row g-3">
                    <div class="col-md-6">
                        <label for="pelapor_nama" class="form-label fw-semibold text-dark">Nama Lengkap</label>
                        <input type="text" class="form-control" id="pelapor_nama" name="pelapor_nama" value="{{ old('pelapor_nama', $laporan->pelapor_nama) }}" required>
                    </div>
                    
                    <div class="col-md-6">
                        <label for="pelapor_tipe" class="form-label fw-semibold text-dark">Pelapor</label>
                        <select class="form-select" id="pelapor_tipe" name="pelapor_tipe" required>
                            <option value="Mahasiswa" {{ old('pelapor_tipe', $laporan->pelapor_tipe) === 'Mahasiswa' ? 'selected' : '' }}>Mahasiswa</option>
                            <option value="Dosen" {{ old('pelapor_tipe', $laporan->pelapor_tipe) === 'Dosen' ? 'selected' : '' }}>Dosen</option>
                            <option value="Tenaga Kependidikan" {{ old('pelapor_tipe', $laporan->pelapor_tipe) === 'Tenaga Kependidikan' ? 'selected' : '' }}>Tenaga Kependidikan</option>
                            <option value="Masyarakat Umum" {{ old('pelapor_tipe', $laporan->pelapor_tipe) === 'Masyarakat Umum' ? 'selected' : '' }}>Masyarakat Umum</option>
                        </select>
                    </div>

                    <div class="col-md-6">
                        <label for="jenis_identitas" class="form-label fw-semibold text-dark">Jenis Identitas</label>
                        <select class="form-select" id="jenis_identitas" name="jenis_identitas" required>
                            <option value="KTM" {{ old('jenis_identitas', $laporan->jenis_identitas) === 'KTM' ? 'selected' : '' }}>KTM</option>
                            <option value="NIDN/NIP" {{ old('jenis_identitas', $laporan->jenis_identitas) === 'NIDN/NIP' ? 'selected' : '' }}>NIDN/NIP</option>
                            <option value="KTP" {{ old('jenis_identitas', $laporan->jenis_identitas) === 'KTP' ? 'selected' : '' }}>KTP</option>
                            <option value="SIM" {{ old('jenis_identitas', $laporan->jenis_identitas) === 'SIM' ? 'selected' : '' }}>SIM</option>
                            <option value="Paspor" {{ old('jenis_identitas', $laporan->jenis_identitas) === 'Paspor' ? 'selected' : '' }}>Paspor</option>
                        </select>
                    </div>

                    <div class="col-md-6">
                        <label for="pelapor_nim_nidn" class="form-label fw-semibold text-dark">NIK/NIP/NIM</label>
                        <input type="text" class="form-control" id="pelapor_nim_nidn" name="pelapor_nim_nidn" value="{{ old('pelapor_nim_nidn', $laporan->pelapor_nim_nidn) }}" required>
                    </div>

                    <div class="col-md-6">
                        <label for="no_telepon" class="form-label fw-semibold text-dark">Nomor Telepon</label>
                        <input type="text" class="form-control" id="no_telepon" name="no_telepon" value="{{ old('no_telepon', $laporan->no_telepon) }}" required>
                    </div>

                    <div class="col-md-6">
                        <label for="pelapor_email" class="form-label fw-semibold text-dark">Email Pelapor</label>
                        <input type="email" class="form-control" id="pelapor_email" name="pelapor_email" value="{{ old('pelapor_email', $laporan->pelapor_email) }}" required>
                    </div>

                    <div class="col-md-6">
                        <label class="form-label fw-semibold text-dark d-block">Jenis Kelamin</label>
                        <div class="form-check form-check-inline mt-1">
                            <input class="form-check-input" type="radio" name="jenis_kelamin" id="jk_l" value="Laki-Laki" {{ old('jenis_kelamin', $laporan->jenis_kelamin) === 'Laki-Laki' ? 'checked' : '' }} required>
                            <label class="form-check-label text-dark" for="jk_l">Laki-Laki</label>
                        </div>
                        <div class="form-check form-check-inline">
                            <input class="form-check-input" type="radio" name="jenis_kelamin" id="jk_p" value="Perempuan" {{ old('jenis_kelamin', $laporan->jenis_kelamin) === 'Perempuan' ? 'checked' : '' }} required>
                            <label class="form-check-label text-dark" for="jk_p">Perempuan</label>
                        </div>
                    </div>

                    <div class="col-md-6">
                        <label for="tipe_laporan" class="form-label fw-semibold text-dark">Kategori</label>
                        <select class="form-select" id="tipe_laporan" name="tipe_laporan" required>
                            <option value="preventif_konseling" {{ old('tipe_laporan', $laporan->tipe_laporan) === 'preventif_konseling' ? 'selected' : '' }}>Preventif & Bimbingan Konseling</option>
                            <option value="kekerasan_bullying" {{ old('tipe_laporan', $laporan->tipe_laporan) === 'kekerasan_bullying' ? 'selected' : '' }}>Aduan Kekerasan atau Bullying</option>
                            <option value="integritas_akademik" {{ old('tipe_laporan', $laporan->tipe_laporan) === 'integritas_akademik' ? 'selected' : '' }}>Aduan Pelanggaran Integritas Akademik</option>
                        </select>
                    </div>

                    <div class="col-md-6">
                        <label for="tanggal_kejadian" class="form-label fw-semibold text-dark">Tanggal Kejadian</label>
                        <input type="date" class="form-control" id="tanggal_kejadian" name="tanggal_kejadian" value="{{ old('tanggal_kejadian', $laporan->tanggal_kejadian) }}" required>
                    </div>

                    <div class="col-md-6">
                        <label for="lokasi_kejadian" class="form-label fw-semibold text-dark">Lokasi Kejadian</label>
                        <input type="text" class="form-control" id="lokasi_kejadian" name="lokasi_kejadian" value="{{ old('lokasi_kejadian', $laporan->lokasi_kejadian) }}" required>
                    </div>
                </div>

                <div class="mb-3 mt-3">
                    <label for="judul" class="form-label fw-semibold text-dark">Judul Laporan / Aduan</label>
                    <input type="text" class="form-control" id="judul" name="judul" value="{{ old('judul', $laporan->judul) }}" required>
                </div>

                <div class="mb-0">
                    <label for="deskripsi" class="form-label fw-semibold text-dark">Deskripsi Kronologis</label>
                    <textarea class="form-control" id="deskripsi" name="deskripsi" rows="5" required>{{ old('deskripsi', $laporan->deskripsi) }}</textarea>
                </div>
            </div>
        </div>

        <!-- Kolom Kanan: Tindak Lanjut & Alur Keputusan -->
        <div class="col-lg-5">
            <div class="card card-custom p-4 mb-4">
                <h5 class="fw-bold mb-3 border-bottom pb-2 text-primary"><i class="fa-solid fa-route me-1"></i> Tindak Lanjut & Alur Kasus</h5>

                <!-- Status Alur Saat Ini -->
                <div class="mb-4">
                    <label for="status" class="form-label fw-bold text-dark mb-1">PILIH TAHAPAN / STATUS KASUS:</label>
                    <select class="form-select fw-bold text-primary" id="status" name="status" onchange="showDecisionPanel()" required>
                        <option value="diajukan" {{ old('status', $laporan->status) === 'diajukan' ? 'selected' : '' }}>Diajukan (Awal)</option>
                        <option value="menunggu_satgas" {{ old('status', $laporan->status) === 'menunggu_satgas' ? 'selected' : '' }}>Menunggu Satgas Kampus</option>
                        <option value="menunggu_psikolog" {{ old('status', $laporan->status) === 'menunggu_psikolog' ? 'selected' : '' }}>Menunggu Psikolog (Bimbingan/Konseling)</option>
                        <option value="menunggu_penilai" {{ old('status', $laporan->status) === 'menunggu_penilai' ? 'selected' : '' }}>Menunggu Tim Penilai Ilmiah</option>
                        <option value="menunggu_rektor" {{ old('status', $laporan->status) === 'menunggu_rektor' ? 'selected' : '' }}>Menunggu Pimpinan PTS / Rektor</option>
                        <option value="selesai" {{ old('status', $laporan->status) === 'selesai' ? 'selected' : '' }}>Selesai & Ditutup</option>
                        <option value="ditolak" {{ old('status', $laporan->status) === 'ditolak' ? 'selected' : '' }}>Ditolak / Dihentikan</option>
                    </select>
                </div>

                <!-- 0. DECISION PANEL: MENUNGGU SATGAS -->
                <div class="decision-panel d-none p-3 bg-light rounded-3 mb-3 border border-1" id="panel_menunggu_satgas">
                    <h6 class="fw-bold text-dark border-bottom pb-2 mb-3"><i class="fa-solid fa-shield-halved me-1 text-primary"></i> Keputusan Satgas Kampus</h6>
                    
                    <div class="mb-3">
                        <label class="form-label fw-bold d-block text-dark">Keputusan Laporan:</label>
                        <div class="form-check form-check-inline">
                            <input class="form-check-input" type="radio" name="satgas_keputusan" id="satgas_terima" value="Terima" onchange="toggleDecisionSubFields('satgas')" checked>
                            <label class="form-check-label fw-semibold text-success" for="satgas_terima">Terima & Lindungi Korban</label>
                        </div>
                        <div class="form-check form-check-inline">
                            <input class="form-check-input" type="radio" name="satgas_keputusan" id="satgas_tolak" value="Tolak" onchange="toggleDecisionSubFields('satgas')">
                            <label class="form-check-label fw-semibold text-danger" for="satgas_tolak">Tolak Laporan</label>
                        </div>
                    </div>

                    <!-- Sub Field: Terima (Bantuan) -->
                    <div id="satgas_sub_terima">
                        <p class="text-muted small mb-2"><i class="fa-solid fa-circle-info me-1"></i> Tentukan tindakan bantuan hukum atau perlindungan rumah aman untuk korban:</p>
                        <div class="mb-2">
                            <label for="satgas_bantuan" class="form-label small fw-bold mb-1">Bantuan Hukum / Rumah Aman</label>
                            <textarea class="form-control form-control-sm" id="satgas_bantuan" name="satgas_bantuan" rows="3" placeholder="Tuliskan bantuan...">{{ old('satgas_bantuan', $laporan->tindakanSatgas->bantuan_hukum_rumah_aman ?? '') }}</textarea>
                        </div>
                        <div class="mb-2">
                            <label for="satgas_catatan" class="form-label small fw-bold mb-1">Catatan Satgas (Opsional)</label>
                            <textarea class="form-control form-control-sm" id="satgas_catatan" name="satgas_catatan" rows="2" placeholder="Catatan tambahan...">{{ old('satgas_catatan', $laporan->tindakanSatgas->catatan ?? '') }}</textarea>
                        </div>
                    </div>

                    <!-- Sub Field: Tolak (Alasan Penolakan) -->
                    <div class="d-none" id="satgas_sub_tolak">
                        <label for="satgas_alasan_penolakan" class="form-label small fw-bold mb-1">Alasan Penolakan (Akan Dikirim via Email)</label>
                        <textarea class="form-control" id="satgas_alasan_penolakan" name="alasan_penolakan" rows="3" placeholder="Tulis alasan penolakan..."></textarea>
                    </div>
                </div>

                <!-- 1. DECISION PANEL: MENUNGU PSIKOLOG -->
                <div class="decision-panel d-none p-3 bg-light rounded-3 mb-3 border border-1" id="panel_menunggu_psikolog">
                    <h6 class="fw-bold text-dark border-bottom pb-2 mb-3"><i class="fa-solid fa-headset me-1 text-primary"></i> Keputusan Layanan Psikologis</h6>
                    
                    <div class="mb-3">
                        <label class="form-label fw-bold d-block text-dark">Keputusan Laporan:</label>
                        <div class="form-check form-check-inline">
                            <input class="form-check-input" type="radio" name="psikolog_keputusan" id="pk_terima" value="Terima" onchange="toggleDecisionSubFields('psikolog')" checked>
                            <label class="form-check-label fw-semibold text-success" for="pk_terima">Terima & Jadwalkan Tatap Muka</label>
                        </div>
                        <div class="form-check form-check-inline">
                            <input class="form-check-input" type="radio" name="psikolog_keputusan" id="pk_tolak" value="Tolak" onchange="toggleDecisionSubFields('psikolog')">
                            <label class="form-check-label fw-semibold text-danger" for="pk_tolak">Tolak Laporan</label>
                        </div>
                    </div>

                    <!-- Sub Field: Terima (Jadwal Tatap Muka) -->
                    <div id="psikolog_sub_terima">
                        <p class="text-muted small mb-2"><i class="fa-solid fa-circle-info me-1"></i> Tentukan jadwal pertemuan tatap muka antara korban/pelapor langsung dengan psikolog kampus:</p>
                        <div class="mb-2">
                            <label for="psikolog_hari" class="form-label small fw-bold mb-1">Hari Pertemuan</label>
                            <input type="text" class="form-control form-control-sm" id="psikolog_hari" name="psikolog_hari" value="{{ old('psikolog_hari', $laporan->catatanKlinisPsikolog->jadwal_hari ?? '') }}" placeholder="Contoh: Senin atau Selasa">
                        </div>
                        <div class="mb-2">
                            <label for="psikolog_tanggal" class="form-label small fw-bold mb-1">Tanggal Pertemuan</label>
                            <input type="date" class="form-control form-control-sm" id="psikolog_tanggal" name="psikolog_tanggal" value="{{ old('psikolog_tanggal', $laporan->catatanKlinisPsikolog->jadwal_tanggal ?? '') }}">
                        </div>
                        <div class="mb-2">
                            <label for="psikolog_jam" class="form-label small fw-bold mb-1">Waktu / Jam</label>
                            <input type="text" class="form-control form-control-sm" id="psikolog_jam" name="psikolog_jam" value="{{ old('psikolog_jam', $laporan->catatanKlinisPsikolog->jadwal_jam ?? '') }}" placeholder="Contoh: 10:00 - 12:00">
                        </div>
                    </div>

                    <!-- Sub Field: Tolak (Alasan Penolakan) -->
                    <div class="d-none" id="psikolog_sub_tolak">
                        <label for="psikolog_alasan_penolakan" class="form-label small fw-bold mb-1">Alasan Penolakan (Akan Dikirim via Email)</label>
                        <textarea class="form-control" id="psikolog_alasan_penolakan" name="alasan_penolakan" rows="3" placeholder="Tulis alasan mengapa aduan ditolak..."></textarea>
                    </div>
                </div>

                <!-- 2. DECISION PANEL: MENUNGGU PENILAI -->
                <div class="decision-panel d-none p-3 bg-light rounded-3 mb-3 border border-1" id="panel_menunggu_penilai">
                    <h6 class="fw-bold text-dark border-bottom pb-2 mb-3"><i class="fa-solid fa-gavel me-1 text-warning"></i> Keputusan Tim Penilai Ilmiah</h6>
                    
                    <div class="mb-3">
                        <label class="form-label fw-bold d-block text-dark">Keputusan Laporan:</label>
                        <div class="form-check form-check-inline">
                            <input class="form-check-input" type="radio" name="penilai_keputusan" id="penk_terima" value="Terima" onchange="toggleDecisionSubFields('penilai')" checked>
                            <label class="form-check-label fw-semibold text-success" for="penk_terima">Terima & Jadwalkan Sidang Etik</label>
                        </div>
                        <div class="form-check form-check-inline">
                            <input class="form-check-input" type="radio" name="penilai_keputusan" id="penk_tolak" value="Tolak" onchange="toggleDecisionSubFields('penilai')">
                            <label class="form-check-label fw-semibold text-danger" for="penk_tolak">Tolak Laporan</label>
                        </div>
                    </div>

                    <!-- Sub Field: Terima (Jadwal Sidang + Detail) -->
                    <div id="penilai_sub_terima">
                        <div class="mb-2">
                            <label for="penilai_sidang_hari" class="form-label small fw-bold mb-1">Hari Sidang Etik</label>
                            <input type="text" class="form-control form-control-sm" id="penilai_sidang_hari" name="penilai_sidang_hari" value="{{ old('penilai_sidang_hari', $laporan->tindakanPenilai->sidang_etik_hari ?? '') }}" placeholder="Contoh: Rabu">
                        </div>
                        <div class="mb-2">
                            <label for="penilai_sidang_tanggal" class="form-label small fw-bold mb-1">Tanggal Sidang Etik</label>
                            <input type="date" class="form-control form-control-sm" id="penilai_sidang_tanggal" name="penilai_sidang_tanggal" value="{{ old('penilai_sidang_tanggal', $laporan->tindakanPenilai->sidang_etik_tanggal ?? '') }}">
                        </div>
                        <div class="mb-2">
                            <label for="penilai_sidang_jam" class="form-label small fw-bold mb-1">Waktu / Jam</label>
                            <input type="text" class="form-control form-control-sm" id="penilai_sidang_jam" name="penilai_sidang_jam" value="{{ old('penilai_sidang_jam', $laporan->tindakanPenilai->sidang_etik_jam ?? '') }}" placeholder="Contoh: 09:00 WIB - Selesai">
                        </div>
                        <div class="mb-2">
                            <label for="penilai_plagiat_score" class="form-label small fw-bold mb-1">Skor Plagiarisme (%)</label>
                            <input type="number" class="form-control form-control-sm" id="penilai_plagiat_score" name="penilai_plagiat_score" value="{{ old('penilai_plagiat_score', $laporan->tindakanPenilai->cek_plagiat_score ?? '0') }}" min="0" max="100">
                        </div>
                        <div class="mb-2">
                            <label for="penilai_rekomendasi" class="form-label small fw-bold mb-1">Rekomendasi Sanksi</label>
                            <textarea class="form-control form-control-sm" id="penilai_rekomendasi" name="penilai_rekomendasi" rows="3" placeholder="Tuliskan rekomendasi sanksi atas pelanggaran akademik...">{{ old('penilai_rekomendasi', $laporan->tindakanPenilai->rekomendasi_sanksi ?? '') }}</textarea>
                        </div>
                    </div>

                    <!-- Sub Field: Tolak (Alasan Penolakan) -->
                    <div class="d-none" id="penilai_sub_tolak">
                        <label for="penilai_alasan_penolakan" class="form-label small fw-bold mb-1">Alasan Penolakan (Akan Dikirim via Email)</label>
                        <textarea class="form-control" id="penilai_alasan_penolakan" name="alasan_penolakan" rows="3" placeholder="Tulis alasan mengapa laporan pelanggaran akademik ditolak..."></textarea>
                    </div>
                </div>

                <!-- 3. DECISION PANEL: MENUNGGU REKTOR -->
                <div class="decision-panel d-none p-3 bg-light rounded-3 mb-3 border border-1" id="panel_menunggu_rektor">
                    <h6 class="fw-bold text-dark border-bottom pb-2 mb-3"><i class="fa-solid fa-signature me-1 text-danger"></i> Keputusan Eksekusi Rektor / PTS</h6>
                    
                    <div class="mb-3">
                        <label class="form-label fw-bold d-block text-dark">Keputusan Akhir:</label>
                        <div class="form-check form-check-inline">
                            <input class="form-check-input" type="radio" name="rektor_keputusan" id="rk_terima" value="Terima" onchange="toggleDecisionSubFields('rektor')" checked>
                            <label class="form-check-label fw-semibold text-success" for="rk_terima">Terima & Eksekusi Sanksi</label>
                        </div>
                        <div class="form-check form-check-inline">
                            <input class="form-check-input" type="radio" name="rektor_keputusan" id="rk_tolak" value="Tolak" onchange="toggleDecisionSubFields('rektor')">
                            <label class="form-check-label fw-semibold text-danger" for="rk_tolak">Tolak Laporan</label>
                        </div>
                    </div>

                    <!-- Sub Field: Terima (Sanksi Akhir) -->
                    <div id="rektor_sub_terima">
                        <div class="mb-2">
                            <label for="rektor_sanksi" class="form-label small fw-bold mb-1">Sanksi yang Dieksekusi</label>
                            <textarea class="form-control form-control-sm" id="rektor_sanksi" name="rektor_sanksi" rows="3" placeholder="Tuliskan keputusan sanksi resmi yang dikeluarkan oleh Rektor...">{{ old('rektor_sanksi', $laporan->eksekusiRektor->sanksi_eksekusi ?? '') }}</textarea>
                        </div>
                        <div class="mb-2">
                            <label for="rektor_catatan" class="form-label small fw-bold mb-1">Catatan Tambahan Keputusan</label>
                            <textarea class="form-control form-control-sm" id="rektor_catatan" name="rektor_catatan" rows="2" placeholder="Catatan rektor...">{{ old('rektor_catatan', $laporan->eksekusiRektor->catatan ?? '') }}</textarea>
                        </div>
                    </div>

                    <!-- Sub Field: Tolak (Alasan Penolakan) -->
                    <div class="d-none" id="rektor_sub_tolak">
                        <label for="rektor_alasan_penolakan" class="form-label small fw-bold mb-1">Alasan Penolakan (Akan Dikirim via Email)</label>
                        <textarea class="form-control" id="rektor_alasan_penolakan" name="alasan_penolakan" rows="3" placeholder="Tulis alasan penolakan..."></textarea>
                    </div>
                </div>

                <!-- 4. DECISION PANEL: SELESAI -->
                <div class="decision-panel d-none p-3 bg-light rounded-3 mb-3 border border-1" id="panel_selesai">
                    <h6 class="fw-bold text-success border-bottom pb-2 mb-3"><i class="fa-solid fa-circle-check me-1"></i> Detail Hasil Kasus Selesai</h6>
                    <p class="text-muted small"><i class="fa-solid fa-info-circle me-1"></i> Kasus telah ditutup. Anda dapat mengoreksi data sanksi rektor di bawah ini jika diperlukan:</p>
                    
                    <div class="mb-2">
                        <label for="selesai_sanksi" class="form-label small fw-bold mb-1">Sanksi Akhir yang Diberikan</label>
                        <textarea class="form-control form-control-sm" id="selPres_sanksi" name="rektor_sanksi" rows="3" placeholder="Sanksi rektor...">{{ old('rektor_sanksi', $laporan->eksekusiRektor->sanksi_eksekusi ?? '') }}</textarea>
                    </div>
                    <div class="mb-2">
                        <label for="selesai_catatan" class="form-label small fw-bold mb-1">Catatan Akhir Rektor</label>
                        <textarea class="form-control form-control-sm" id="selPres_catatan" name="rektor_catatan" rows="2" placeholder="Catatan...">{{ old('rektor_catatan', $laporan->eksekusiRektor->catatan ?? '') }}</textarea>
                    </div>
                </div>

                <!-- 5. DECISION PANEL: DITOLAK -->
                <div class="decision-panel d-none p-3 bg-danger-subtle text-danger rounded-3 mb-3 border border-1 border-danger" id="panel_ditolak">
                    <h6 class="fw-bold border-bottom border-danger pb-2 mb-3"><i class="fa-solid fa-ban me-1"></i> Kasus Dinyatakan Ditolak</h6>
                    <div class="mb-2">
                        <label for="ditolak_alasan" class="form-label small fw-bold mb-1">Alasan Penolakan Kasus</label>
                        <textarea class="form-control" id="ditolak_alasan" name="alasan_penolakan" rows="3" placeholder="Alasan penolakan...">{{ old('alasan_penolakan', $laporan->alasan_penolakan ?? '') }}</textarea>
                    </div>
                </div>

                <!-- Save and Cancel Actions -->
                <div class="mt-4 pt-3 border-top d-flex gap-2">
                    <a href="{{ route('laporan.show', $laporan->id) }}" class="btn btn-light rounded-3 w-50 py-2">Batal</a>
                    <button type="submit" class="btn btn-success rounded-3 w-50 fw-bold py-2">
                        <i class="fa-solid fa-floppy-disk me-1"></i> Simpan Data
                    </button>
                </div>
            </div>
        </div>
    </div>
</form>
@endsection

@push('scripts')
<script>
    function showDecisionPanel() {
        const statusVal = document.getElementById('status').value;
        
        // Hide all decision panels first
        document.querySelectorAll('.decision-panel').forEach(panel => {
            panel.classList.add('d-none');
            // Remove required from inner inputs
            panel.querySelectorAll('input, textarea, select').forEach(input => {
                input.removeAttribute('required');
            });
        });

        // Show selected panel
        if (statusVal === 'menunggu_satgas') {
            const panel = document.getElementById('panel_menunggu_satgas');
            panel.classList.remove('d-none');
            toggleDecisionSubFields('satgas');
        } else if (statusVal === 'menunggu_psikolog') {
            const panel = document.getElementById('panel_menunggu_psikolog');
            panel.classList.remove('d-none');
            toggleDecisionSubFields('psikolog');
        } else if (statusVal === 'menunggu_penilai') {
            const panel = document.getElementById('panel_menunggu_penilai');
            panel.classList.remove('d-none');
            toggleDecisionSubFields('penilai');
        } else if (statusVal === 'menunggu_rektor') {
            const panel = document.getElementById('panel_menunggu_rektor');
            panel.classList.remove('d-none');
            toggleDecisionSubFields('rektor');
        } else if (statusVal === 'selesai') {
            const panel = document.getElementById('panel_selesai');
            panel.classList.remove('d-none');
            document.getElementById('selPres_sanksi').setAttribute('required', 'required');
        } else if (statusVal === 'ditolak') {
            const panel = document.getElementById('panel_ditolak');
            panel.classList.remove('d-none');
            document.getElementById('ditolak_alasan').setAttribute('required', 'required');
        }
    }

    function toggleDecisionSubFields(role) {
        const decisionVal = document.querySelector(`input[name="${role}_keputusan"]:checked`).value;
        const subTerima = document.getElementById(`${role}_sub_terima`);
        const subTolak = document.getElementById(`${role}_sub_tolak`);

        if (decisionVal === 'Terima') {
            subTerima.classList.remove('d-none');
            subTolak.classList.add('d-none');
            
            // Set required fields for Terima
            subTerima.querySelectorAll('input, textarea').forEach(input => {
                if (input.id !== `${role}_catatan`) { // Catatan rektor is optional
                    input.setAttribute('required', 'required');
                }
            });
            // Clear required fields for Tolak
            subTolak.querySelectorAll('textarea').forEach(input => {
                input.removeAttribute('required');
            });
        } else {
            subTerima.classList.add('d-none');
            subTolak.classList.remove('d-none');
            
            // Set required fields for Tolak
            subTolak.querySelectorAll('textarea').forEach(input => {
                input.setAttribute('required', 'required');
            });
            // Clear required for Terima
            subTerima.querySelectorAll('input, textarea').forEach(input => {
                input.removeAttribute('required');
            });
        }
    }

    // Run on load to render the correct panel initially
    document.addEventListener('DOMContentLoaded', function() {
        showDecisionPanel();
    });
</script>
@endpush
