<x-app-layout>
    <x-slot name="header">Isi Hasil Konsultasi</x-slot>

    <div class="max-w-4xl mx-auto px-4 sm:px-6 lg:px-8 py-6 space-y-5">

        {{-- Breadcrumb --}}
        <nav class="flex items-center gap-2 text-sm text-slate-500 dark:text-slate-400">
            <a href="{{ route('dokter.konsultasi.index') }}" class="hover:text-teal-500 transition">Konsultasi Saya</a>
            <i class="fas fa-chevron-right text-xs"></i>
            <span class="text-slate-700 dark:text-slate-200 font-medium">{{ $konsultasi->nomor_konsultasi ?? 'Isi Hasil' }}</span>
        </nav>

        @if(session('success'))
            <div class="flex items-center gap-3 p-4 rounded-xl bg-green-50 dark:bg-green-900/30 border border-green-200 dark:border-green-700 text-green-800 dark:text-green-300 text-sm">
                <i class="fas fa-check-circle text-green-500"></i>{{ session('success') }}
            </div>
        @endif
        @if(session('error'))
            <div class="flex items-center gap-3 p-4 rounded-xl bg-red-50 dark:bg-red-900/30 border border-red-200 dark:border-red-700 text-red-800 dark:text-red-300 text-sm">
                <i class="fas fa-exclamation-circle text-red-500"></i>{{ session('error') }}
            </div>
        @endif
        @if($errors->any())
            <div class="p-4 rounded-xl bg-red-50 dark:bg-red-900/30 border border-red-200 dark:border-red-700">
                <p class="text-sm font-semibold text-red-700 dark:text-red-400 mb-1"><i class="fas fa-exclamation-circle mr-1"></i>Terdapat kesalahan:</p>
                <ul class="list-disc list-inside text-sm text-red-600 dark:text-red-400 space-y-0.5">
                    @foreach($errors->all() as $e)<li>{{ $e }}</li>@endforeach
                </ul>
            </div>
        @endif

        {{-- ── Info Pasien & Konsultasi ─────────────────────────────────── --}}
        <div class="kkd-card">
            <div class="flex flex-col sm:flex-row sm:items-center sm:justify-between gap-3 mb-4">
                <h3 class="section-title"><i class="fas fa-user mr-2 text-teal-500"></i>Informasi Pasien</h3>
                @php
                    $sc = match($konsultasi->status_konsultasi ?? '') {
                        'MENUNGGU'       => 'bg-amber-100 text-amber-700 dark:bg-amber-900/40 dark:text-amber-400',
                        'BERLANGSUNG'    => 'bg-blue-100 text-blue-700 dark:bg-blue-900/40 dark:text-blue-400',
                        'SELESAI_KONSUL' => 'bg-teal-100 text-teal-700 dark:bg-teal-900/40 dark:text-teal-400',
                        default          => 'bg-slate-100 text-slate-600',
                    };
                    $sl = match($konsultasi->status_konsultasi ?? '') {
                        'MENUNGGU'       => 'Menunggu',
                        'BERLANGSUNG'    => 'Berlangsung',
                        'SELESAI_KONSUL' => 'Selesai',
                        default          => $konsultasi->status_konsultasi ?? '-',
                    };
                @endphp
                <span class="inline-flex px-3 py-1 rounded-full text-xs font-semibold {{ $sc }}">{{ $sl }}</span>
            </div>

            <div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-4 text-sm">
                <div>
                    <p class="text-xs text-slate-400 uppercase tracking-wider mb-0.5">Nama Pasien</p>
                    <p class="font-semibold text-slate-800 dark:text-white">{{ $konsultasi->pasien->nama_lengkap ?? '-' }}</p>
                </div>
                <div>
                    <p class="text-xs text-slate-400 uppercase tracking-wider mb-0.5">No. Rekam Medis</p>
                    <p class="font-semibold text-slate-800 dark:text-white">{{ $konsultasi->pasien->no_rekam_medis ?? '-' }}</p>
                </div>
                <div>
                    <p class="text-xs text-slate-400 uppercase tracking-wider mb-0.5">Usia / Jenis Kelamin</p>
                    <p class="font-semibold text-slate-800 dark:text-white">
                        {{ $konsultasi->pasien->usia ?? '-' }} tahun /
                        {{ $konsultasi->pasien->jenis_kelamin === 'L' ? 'Laki-laki' : ($konsultasi->pasien->jenis_kelamin === 'P' ? 'Perempuan' : '-') }}
                    </p>
                </div>
                <div>
                    <p class="text-xs text-slate-400 uppercase tracking-wider mb-0.5">No. Konsultasi</p>
                    <p class="font-semibold text-slate-800 dark:text-white">{{ $konsultasi->nomor_konsultasi ?? '-' }}</p>
                </div>
                <div>
                    <p class="text-xs text-slate-400 uppercase tracking-wider mb-0.5">Tanggal Masuk</p>
                    <p class="font-semibold text-slate-800 dark:text-white">{{ $konsultasi->created_at?->translatedFormat('d F Y, H:i') ?? '-' }}</p>
                </div>
                <div>
                    <p class="text-xs text-slate-400 uppercase tracking-wider mb-0.5">Lama Keluhan</p>
                    <p class="font-semibold text-slate-800 dark:text-white">{{ $konsultasi->lama_keluhan ?? '-' }}</p>
                </div>
            </div>

            {{-- Keluhan --}}
            @if($konsultasi->keluhan_pasien)
            <div class="mt-4 p-3 rounded-lg bg-amber-50 dark:bg-amber-900/20 border border-amber-100 dark:border-amber-800">
                <p class="text-xs font-semibold text-amber-700 dark:text-amber-400 uppercase tracking-wider mb-1">
                    <i class="fas fa-comment-medical mr-1"></i>Keluhan Pasien
                </p>
                <p class="text-sm text-slate-700 dark:text-slate-300">{{ $konsultasi->keluhan_pasien }}</p>
            </div>
            @endif
        </div>

        {{-- ── Data Pemeriksaan (jika ada) ─────────────────────────────── --}}
        @if($konsultasi->pemeriksaan)
        @php $pm = $konsultasi->pemeriksaan; @endphp
        <div class="kkd-card">
            <h3 class="section-title mb-4"><i class="fas fa-thermometer-half mr-2 text-orange-500"></i>Data Pemeriksaan Awal</h3>
            <div class="grid grid-cols-2 sm:grid-cols-4 gap-3">
                <div class="p-3 rounded-lg bg-slate-50 dark:bg-slate-800/50 text-center">
                    <p class="text-xs text-slate-400 mb-1">Suhu Tubuh</p>
                    @php
                        $suhuColor = match($pm->kategori_suhu ?? '') {
                            'NORMAL'      => 'text-green-600',
                            'SUBFEBRIS'   => 'text-yellow-600',
                            'DEMAM_RINGAN'=> 'text-orange-500',
                            'DEMAM_SEDANG'=> 'text-orange-600',
                            'DEMAM_TINGGI'=> 'text-red-600',
                            default       => 'text-slate-700',
                        };
                    @endphp
                    <p class="text-xl font-bold {{ $suhuColor }}">{{ $pm->suhu_tubuh ?? '-' }}°C</p>
                    <p class="text-xs text-slate-400 mt-0.5">{{ $pm->kategori_suhu ?? '' }}</p>
                </div>
                <div class="p-3 rounded-lg bg-slate-50 dark:bg-slate-800/50 text-center">
                    <p class="text-xs text-slate-400 mb-1">Tekanan Darah</p>
                    <p class="text-sm font-bold text-slate-700 dark:text-white">{{ $pm->tekanan_darah_formatted ?? ($pm->tekanan_darah ?? '-') }}</p>
                </div>
                <div class="p-3 rounded-lg bg-slate-50 dark:bg-slate-800/50 text-center">
                    <p class="text-xs text-slate-400 mb-1">Detak Jantung</p>
                    <p class="text-sm font-bold text-slate-700 dark:text-white">{{ $pm->detak_jantung ? $pm->detak_jantung . ' bpm' : '-' }}</p>
                </div>
                <div class="p-3 rounded-lg bg-slate-50 dark:bg-slate-800/50 text-center">
                    <p class="text-xs text-slate-400 mb-1">SpO2</p>
                    <p class="text-sm font-bold text-slate-700 dark:text-white">{{ $pm->saturasi_oksigen ? $pm->saturasi_oksigen . '%' : '-' }}</p>
                </div>
            </div>
            @if($pm->keluhan_utama)
            <div class="mt-3 text-sm">
                <span class="text-xs text-slate-400 uppercase tracking-wider">Keluhan saat pemeriksaan: </span>
                <span class="text-slate-700 dark:text-slate-300">{{ $pm->keluhan_utama }}</span>
            </div>
            @endif
        </div>
        @endif

        {{-- ── Form Hasil Konsultasi ────────────────────────────────────── --}}
        @if(in_array($konsultasi->status_konsultasi, ['MENUNGGU', 'BERLANGSUNG']))
        <form action="{{ route('dokter.konsultasi.update', $konsultasi->id) }}" method="POST" enctype="multipart/form-data" id="formKonsultasi">
            @csrf
            @method('PUT')

            {{-- Pemeriksaan Fisik --}}
            <div class="kkd-card mb-5">
                <h3 class="section-title mb-4"><i class="fas fa-search-plus mr-2 text-blue-500"></i>Pemeriksaan Fisik</h3>
                <textarea name="pemeriksaan_fisik" rows="3"
                          placeholder="Deskripsikan hasil pemeriksaan fisik pasien..."
                          class="w-full px-3 py-2 rounded-lg border border-slate-300 dark:border-slate-600 bg-white dark:bg-slate-800 text-sm text-slate-800 dark:text-slate-200 focus:ring-2 focus:ring-teal-400 outline-none resize-none">{{ old('pemeriksaan_fisik', $konsultasi->pemeriksaan_fisik) }}</textarea>
            </div>

            {{-- Diagnosis & Catatan --}}
            <div class="kkd-card mb-5 space-y-4">
                <h3 class="section-title"><i class="fas fa-notes-medical mr-2 text-purple-500"></i>Diagnosis & Catatan</h3>

                <div>
                    <label class="block text-sm font-medium text-slate-700 dark:text-slate-300 mb-1">
                        Diagnosis Utama <span class="text-red-500">*</span>
                    </label>
                    <input type="text" name="diagnosis_utama"
                           value="{{ old('diagnosis_utama', $konsultasi->diagnosis_utama) }}"
                           placeholder="Contoh: Demam tifoid, ISPA, dll"
                           required
                           class="w-full px-3 py-2 rounded-lg border border-slate-300 dark:border-slate-600 bg-white dark:bg-slate-800 text-sm text-slate-800 dark:text-slate-200 focus:ring-2 focus:ring-teal-400 outline-none">
                    @error('diagnosis_utama')<p class="text-xs text-red-500 mt-1">{{ $message }}</p>@enderror
                </div>

                <div>
                    <label class="block text-sm font-medium text-slate-700 dark:text-slate-300 mb-1">Catatan Dokter</label>
                    <textarea name="catatan_dokter" rows="3"
                              placeholder="Catatan tambahan untuk rekam medis..."
                              class="w-full px-3 py-2 rounded-lg border border-slate-300 dark:border-slate-600 bg-white dark:bg-slate-800 text-sm text-slate-800 dark:text-slate-200 focus:ring-2 focus:ring-teal-400 outline-none resize-none">{{ old('catatan_dokter', $konsultasi->catatan_dokter) }}</textarea>
                </div>

                <div>
                    <label class="block text-sm font-medium text-slate-700 dark:text-slate-300 mb-1">Tindakan yang Dilakukan</label>
                    <textarea name="tindakan_dokter" rows="2"
                              placeholder="Contoh: Injeksi antipiretik, nebulisasi, dll"
                              class="w-full px-3 py-2 rounded-lg border border-slate-300 dark:border-slate-600 bg-white dark:bg-slate-800 text-sm text-slate-800 dark:text-slate-200 focus:ring-2 focus:ring-teal-400 outline-none resize-none">{{ old('tindakan_dokter', $konsultasi->tindakan_dokter) }}</textarea>
                </div>

                <div>
                    <label class="block text-sm font-medium text-slate-700 dark:text-slate-300 mb-1">Anjuran Dokter</label>
                    <textarea name="anjuran_dokter" rows="2"
                              placeholder="Istirahat, minum air, kontrol kembali, dll"
                              class="w-full px-3 py-2 rounded-lg border border-slate-300 dark:border-slate-600 bg-white dark:bg-slate-800 text-sm text-slate-800 dark:text-slate-200 focus:ring-2 focus:ring-teal-400 outline-none resize-none">{{ old('anjuran_dokter', $konsultasi->anjuran_dokter) }}</textarea>
                </div>

                <div>
                    <label class="block text-sm font-medium text-slate-700 dark:text-slate-300 mb-1">Rencana Perawatan</label>
                    <textarea name="rencana_perawatan" rows="2"
                              placeholder="Rawat jalan, rawat inap, rujuk, dll"
                              class="w-full px-3 py-2 rounded-lg border border-slate-300 dark:border-slate-600 bg-white dark:bg-slate-800 text-sm text-slate-800 dark:text-slate-200 focus:ring-2 focus:ring-teal-400 outline-none resize-none">{{ old('rencana_perawatan', $konsultasi->rencana_perawatan) }}</textarea>
                </div>
            </div>

            {{-- Resep --}}
            <div class="kkd-card mb-5">
                <h3 class="section-title mb-4"><i class="fas fa-prescription-bottle-alt mr-2 text-green-500"></i>Resep Obat</h3>
                <textarea name="resep_dokter" rows="5"
                          placeholder="Tulis resep obat di sini...&#10;Contoh:&#10;1. Paracetamol 500mg - 3x1 tab (setelah makan)&#10;2. Amoxicillin 500mg - 3x1 tab (7 hari)"
                          class="w-full px-3 py-2 rounded-lg border border-slate-300 dark:border-slate-600 bg-white dark:bg-slate-800 text-sm text-slate-800 dark:text-slate-200 focus:ring-2 focus:ring-teal-400 outline-none resize-none font-mono">{{ old('resep_dokter', $konsultasi->resep_dokter) }}</textarea>
            </div>

            {{-- Upload Berkas --}}
            <div class="kkd-card mb-5">
                <h3 class="section-title mb-1"><i class="fas fa-paperclip mr-2 text-slate-500"></i>Upload Berkas</h3>
                <p class="section-subtitle mb-4">PDF, JPG, PNG — maksimal 5MB per file</p>

                {{-- Berkas yang sudah ada --}}
                @if(!empty($konsultasi->berkas_dokter))
                <div class="mb-4 space-y-2">
                    <p class="text-xs font-semibold text-slate-500 dark:text-slate-400 uppercase tracking-wider">Berkas Tersimpan</p>
                    @foreach($konsultasi->berkas_dokter as $berkas)
                    <div class="flex items-center gap-3 p-2.5 rounded-lg bg-slate-50 dark:bg-slate-800/50 border border-slate-200 dark:border-slate-700">
                        <div class="w-8 h-8 rounded-lg flex items-center justify-center bg-slate-100 dark:bg-slate-700 shrink-0">
                            @if(str_contains($berkas['tipe'] ?? '', 'pdf'))
                                <i class="fas fa-file-pdf text-red-500 text-xs"></i>
                            @else
                                <i class="fas fa-file-image text-blue-500 text-xs"></i>
                            @endif
                        </div>
                        <div class="flex-1 min-w-0">
                            <p class="text-xs font-medium text-slate-700 dark:text-slate-300 truncate">{{ $berkas['nama'] ?? 'File' }}</p>
                            <p class="text-xs text-slate-400">{{ $berkas['ukuran_kb'] ?? 0 }} KB</p>
                        </div>
                        <a href="{{ asset('storage/' . $berkas['path']) }}" target="_blank"
                           class="text-xs text-teal-600 hover:text-teal-700 dark:text-teal-400">
                            <i class="fas fa-external-link-alt"></i>
                        </a>
                    </div>
                    @endforeach
                </div>
                @endif

                {{-- Upload area --}}
                <div id="dropZone"
                     class="border-2 border-dashed border-slate-300 dark:border-slate-600 rounded-xl p-6 text-center cursor-pointer hover:border-teal-400 dark:hover:border-teal-600 transition"
                     onclick="$('#inputBerkas').click()">
                    <i class="fas fa-cloud-upload-alt text-3xl text-slate-300 dark:text-slate-600 mb-2"></i>
                    <p class="text-sm font-medium text-slate-600 dark:text-slate-400">Klik atau seret file ke sini</p>
                    <p class="text-xs text-slate-400 mt-1">PDF, JPG, PNG · Maks. 5MB per file</p>
                </div>
                <input type="file" name="berkas_dokter[]" id="inputBerkas"
                       accept=".pdf,.jpg,.jpeg,.png" multiple class="hidden">
                <div id="previewBerkas" class="mt-3 space-y-2"></div>
            </div>

            {{-- ── Action Buttons ──────────────────────────────────────── --}}
            <div class="kkd-card">
                <div class="flex flex-col sm:flex-row gap-3">
                    {{-- Simpan Draft --}}
                    <button type="submit" name="selesai" value="0"
                            class="flex-1 inline-flex items-center justify-center gap-2 px-5 py-2.5 bg-slate-100 dark:bg-slate-700 hover:bg-slate-200 dark:hover:bg-slate-600 text-slate-700 dark:text-slate-300 text-sm font-semibold rounded-lg transition">
                        <i class="fas fa-save"></i> Simpan Draft
                    </button>

                    {{-- Selesai & Serahkan --}}
                    <button type="button" onclick="konfirmasiSelesai()"
                            class="flex-1 inline-flex items-center justify-center gap-2 px-5 py-2.5 bg-gradient-to-r from-teal-500 to-teal-600 hover:from-teal-600 hover:to-teal-700 text-white text-sm font-semibold rounded-lg shadow transition">
                        <i class="fas fa-check-circle"></i> Selesai & Serahkan ke Admin
                    </button>
                </div>
                <p class="text-xs text-slate-400 dark:text-slate-500 mt-2 text-center">
                    <i class="fas fa-info-circle mr-1"></i>
                    "Simpan Draft" menyimpan tanpa mengakhiri konsultasi. "Selesai" mengakhiri sesi dan menyerahkan berkas ke admin untuk diproses pembayaran.
                </p>
            </div>

        </form>

        @else
        {{-- ── Tampilan Read-Only jika sudah selesai ───────────────────── --}}
        <div class="kkd-card">
            <div class="flex items-center gap-3 mb-5 p-3 rounded-lg bg-teal-50 dark:bg-teal-900/20 border border-teal-200 dark:border-teal-700">
                <i class="fas fa-check-circle text-teal-500"></i>
                <p class="text-sm text-teal-700 dark:text-teal-400 font-medium">Konsultasi ini sudah selesai dan tidak dapat diedit.</p>
            </div>

            <div class="grid grid-cols-1 sm:grid-cols-2 gap-5 text-sm">
                @if($konsultasi->pemeriksaan_fisik)
                <div>
                    <p class="text-xs font-semibold text-slate-400 uppercase tracking-wider mb-1">Pemeriksaan Fisik</p>
                    <p class="text-slate-700 dark:text-slate-300">{{ $konsultasi->pemeriksaan_fisik }}</p>
                </div>
                @endif
                @if($konsultasi->diagnosis_utama)
                <div>
                    <p class="text-xs font-semibold text-slate-400 uppercase tracking-wider mb-1">Diagnosis Utama</p>
                    <p class="font-semibold text-slate-800 dark:text-white">{{ $konsultasi->diagnosis_utama }}</p>
                </div>
                @endif
                @if($konsultasi->catatan_dokter)
                <div class="sm:col-span-2">
                    <p class="text-xs font-semibold text-slate-400 uppercase tracking-wider mb-1">Catatan Dokter</p>
                    <p class="text-slate-700 dark:text-slate-300">{{ $konsultasi->catatan_dokter }}</p>
                </div>
                @endif
                @if($konsultasi->resep_dokter)
                <div class="sm:col-span-2">
                    <p class="text-xs font-semibold text-slate-400 uppercase tracking-wider mb-1">Resep Obat</p>
                    <div class="p-3 rounded-lg bg-green-50 dark:bg-green-900/20 border border-green-100 dark:border-green-800 font-mono text-sm text-slate-700 dark:text-slate-300">
                        {!! nl2br(e($konsultasi->resep_dokter)) !!}
                    </div>
                </div>
                @endif
            </div>
        </div>

        {{-- Tombol Serahkan Berkas (jika belum) --}}
        @if($konsultasi->status_konsultasi === 'SELESAI_KONSUL' && !$konsultasi->diserahkan_ke_admin)
        <div class="kkd-card border-l-4 border-amber-400">
            <div class="flex flex-col sm:flex-row sm:items-center sm:justify-between gap-3">
                <div>
                    <p class="text-sm font-semibold text-slate-800 dark:text-white">Berkas belum diserahkan ke Admin</p>
                    <p class="text-xs text-slate-500 dark:text-slate-400 mt-0.5">Serahkan berkas agar admin dapat memproses pembayaran.</p>
                </div>
                <button onclick="serahkanBerkas({{ $konsultasi->id }})"
                        class="inline-flex items-center gap-2 px-5 py-2.5 bg-amber-500 hover:bg-amber-600 text-white text-sm font-semibold rounded-lg shadow transition whitespace-nowrap">
                    <i class="fas fa-paper-plane"></i> Serahkan ke Admin
                </button>
            </div>
        </div>
        @elseif($konsultasi->diserahkan_ke_admin)
        <div class="kkd-card border-l-4 border-green-500">
            <div class="flex items-center gap-3">
                <i class="fas fa-check-circle text-green-500 text-lg"></i>
                <div>
                    <p class="text-sm font-semibold text-slate-800 dark:text-white">Berkas sudah diserahkan ke Admin</p>
                    <p class="text-xs text-slate-500 mt-0.5">{{ $konsultasi->waktu_serah_berkas?->translatedFormat('d F Y, H:i') }}</p>
                </div>
            </div>
        </div>
        @endif
        @endif

        {{-- Back --}}
        <div>
            <a href="{{ route('dokter.konsultasi.index') }}"
               class="inline-flex items-center gap-2 text-sm text-slate-500 hover:text-teal-600 dark:hover:text-teal-400 transition">
                <i class="fas fa-arrow-left"></i> Kembali ke Daftar Konsultasi
            </a>
        </div>

    </div>

    {{-- Modal Konfirmasi Selesai --}}
    <div id="modalSelesai" class="fixed inset-0 z-50 hidden items-center justify-center bg-black/50 backdrop-blur-sm p-4">
        <div class="bg-white dark:bg-slate-800 rounded-2xl shadow-2xl w-full max-w-md p-6">
            <div class="flex items-center gap-3 mb-4">
                <div class="w-10 h-10 rounded-full bg-teal-100 dark:bg-teal-900/40 flex items-center justify-center">
                    <i class="fas fa-check-circle text-teal-600 dark:text-teal-400"></i>
                </div>
                <h3 class="text-base font-bold text-slate-800 dark:text-white">Selesaikan Konsultasi?</h3>
            </div>
            <div class="p-3 rounded-lg bg-amber-50 dark:bg-amber-900/20 border border-amber-200 dark:border-amber-700 mb-4">
                <p class="text-sm text-amber-700 dark:text-amber-400">
                    <i class="fas fa-exclamation-triangle mr-1"></i>
                    Setelah diselesaikan, form konsultasi <strong>tidak dapat diedit</strong> lagi. Pastikan semua data sudah benar.
                </p>
            </div>
            <p class="text-sm text-slate-600 dark:text-slate-400 mb-5">
                Berkas akan diserahkan ke admin untuk diproses pembayaran.
            </p>
            <div class="flex gap-3">
                <button onclick="tutupModalSelesai()"
                        class="flex-1 px-4 py-2 rounded-lg border border-slate-300 dark:border-slate-600 text-sm font-medium text-slate-700 dark:text-slate-300 hover:bg-slate-50 dark:hover:bg-slate-700 transition">
                    Kembali
                </button>
                <button onclick="submitSelesai()"
                        class="flex-1 px-4 py-2 rounded-lg bg-teal-500 hover:bg-teal-600 text-white text-sm font-semibold transition">
                    <i class="fas fa-check mr-1"></i> Ya, Selesaikan
                </button>
            </div>
        </div>
    </div>

    @push('scripts')
    <script>
    // ── Upload Preview ───────────────────────────────────────────────────────
    $('#inputBerkas').on('change', function () {
        const files = Array.from(this.files);
        let html = '';
        files.forEach(f => {
            const icon = f.type.includes('pdf') ? 'fa-file-pdf text-red-500' : 'fa-file-image text-blue-500';
            const size = (f.size / 1024).toFixed(1);
            html += `
                <div class="flex items-center gap-2 p-2 rounded-lg bg-slate-50 dark:bg-slate-800/50 border border-slate-200 dark:border-slate-700 text-sm">
                    <i class="fas ${icon}"></i>
                    <span class="text-slate-700 dark:text-slate-300 truncate flex-1">${f.name}</span>
                    <span class="text-xs text-slate-400 shrink-0">${size} KB</span>
                </div>`;
        });
        $('#previewBerkas').html(html);
    });

    // Drag & drop
    const dz = document.getElementById('dropZone');
    dz.addEventListener('dragover', e => { e.preventDefault(); dz.classList.add('border-teal-400'); });
    dz.addEventListener('dragleave', () => dz.classList.remove('border-teal-400'));
    dz.addEventListener('drop', e => {
        e.preventDefault();
        dz.classList.remove('border-teal-400');
        document.getElementById('inputBerkas').files = e.dataTransfer.files;
        $('#inputBerkas').trigger('change');
    });

    // ── Modal Selesai ────────────────────────────────────────────────────────
    function konfirmasiSelesai() {
        $('#modalSelesai').removeClass('hidden').addClass('flex');
    }

    function tutupModalSelesai() {
        $('#modalSelesai').addClass('hidden').removeClass('flex');
    }

    function submitSelesai() {
        // Tambahkan input hidden selesai=1 lalu submit form
        $('<input>').attr({ type: 'hidden', name: 'selesai', value: '1' }).appendTo('#formKonsultasi');
        tutupModalSelesai();
        $('#formKonsultasi').submit();
    }

    // Klik backdrop tutup modal
    $('#modalSelesai').on('click', function(e) {
        if (e.target === this) tutupModalSelesai();
    });

    // ── Serahkan Berkas ──────────────────────────────────────────────────────
    function serahkanBerkas(id) {
        if (!confirm('Serahkan berkas konsultasi ini ke admin?')) return;
        $.ajax({
            url: '/dokter/konsultasi/' + id + '/serahkan-berkas',
            method: 'POST',
            data: { _token: '{{ csrf_token() }}' },
            success: function (res) {
                if (res.success) {
                    location.reload();
                } else {
                    alert(res.message || 'Gagal menyerahkan berkas.');
                }
            },
            error: function () { alert('Terjadi kesalahan. Silakan coba lagi.'); }
        });
    }
    </script>
    @endpush
</x-app-layout>