<x-app-layout>
    @section('title', 'Detail Resep')

    <div class="space-y-6">
        <!-- Breadcrumb -->
        <nav class="flex" aria-label="Breadcrumb">
            <ol class="inline-flex items-center space-x-1 md:space-x-3">
                <li>
                    <a href="{{ route('resep.index') }}" class="text-[#7A7FAE] hover:text-[#4A538F]">Resep</a>
                </li>
                <li>
                    <div class="flex items-center">
                        <svg class="w-4 h-4 text-[#7A7FAE]" fill="currentColor" viewBox="0 0 20 20">
                            <path fill-rule="evenodd" d="M7.293 14.707a1 1 0 010-1.414L10.586 10 7.293 6.707a1 1 0 011.414-1.414l4 4a1 1 0 010 1.414l-4 4a1 1 0 01-1.414 0z" clip-rule="evenodd"/>
                        </svg>
                        <span class="ml-1 text-[#2F347A] font-medium">Detail</span>
                    </div>
                </li>
            </ol>
        </nav>

        <div class="flex items-center justify-between">
            <h1 class="text-2xl font-bold text-[#2F347A]">Detail Resep: {{ $resep->no_resep }}</h1>
            <div class="flex gap-2">
                <x-btn type="secondary" href="{{ route('resep.print', $resep) }}" target="_blank">
                    <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17 17h2a2 2 0 002-2v-4a2 2 0 00-2-2H5a2 2 0 00-2 2v4a2 2 0 002 2h2m2 4h6a2 2 0 002-2v-4a2 2 0 00-2-2H9a2 2 0 00-2 2v4a2 2 0 002 2zm8-12V5a2 2 0 00-2-2H9a2 2 0 00-2 2v4h10z"/>
                    </svg>
                    Print
                </x-btn>
                @if(auth()->user()->canManageResep() && $resep->status !== 'selesai')
                    <x-btn type="secondary" href="{{ route('resep.edit', $resep) }}">
                        <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z"/>
                        </svg>
                        Edit
                    </x-btn>
                @endif
                <x-btn type="secondary" href="{{ route('resep.index') }}">Kembali</x-btn>
            </div>
        </div>

        {{-- Flash messages --}}
        @if(session('error'))
            <div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded-lg">
                {{ session('error') }}
            </div>
        @endif
        @if(session('success'))
            <div class="bg-green-100 border border-green-400 text-green-700 px-4 py-3 rounded-lg">
                {{ session('success') }}
            </div>
        @endif

        <div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
            <!-- Left Column - Patient & Prescription Info -->
            <div class="lg:col-span-1 space-y-6">
                <!-- Info Resep -->
                <x-card class="p-6">
                    <h3 class="text-lg font-semibold text-[#2F347A] mb-4 pb-2 border-b border-[#E5E7F2]">Informasi Resep</h3>
                    <dl class="space-y-3">
                        <div class="flex justify-between">
                            <dt class="text-[#7A7FAE]">No. Resep</dt>
                            <dd class="font-medium text-[#2F347A]">{{ $resep->no_resep }}</dd>
                        </div>
                        <div class="flex justify-between">
                            <dt class="text-[#7A7FAE]">Tanggal</dt>
                            <dd class="font-medium text-[#2F347A]">{{ $resep->tanggal_resep->format('d M Y') }}</dd>
                        </div>
                        <div class="flex justify-between">
                            <dt class="text-[#7A7FAE]">Dokter</dt>
                            <dd class="font-medium text-[#2F347A]">{{ $resep->nama_dokter ?? $resep->user->name ?? 'N/A' }}</dd>
                        </div>
                        <div class="flex justify-between">
                            <dt class="text-[#7A7FAE]">No. SIP</dt>
                            <dd class="font-medium text-[#2F347A]">{{ $resep->no_sip ?? '-' }}</dd>
                        </div>
                        <div class="flex justify-between">
                            <dt class="text-[#7A7FAE]">Jenis Penjamin</dt>
                            <dd class="font-medium text-[#2F347A]">{{ $resep->jenis_penjamin ?? '-' }}</dd>
                        </div>
                        <div class="flex justify-between">
                            <dt class="text-[#7A7FAE]">Jenis Layanan</dt>
                            <dd class="font-medium text-[#2F347A]">{{ $resep->jenis_layanan ?? '-' }}</dd>
                        </div>
                        <div class="flex justify-between">
                            <dt class="text-[#7A7FAE]">Status</dt>
                            <dd>
                                <x-badge :type="$resep->status_color">{{ ucfirst($resep->status) }}</x-badge>
                            </dd>
                        </div>
                    </dl>
                </x-card>

                <!-- Info Pasien -->
                <x-card class="p-6">
                    <h3 class="text-lg font-semibold text-[#2F347A] mb-4 pb-2 border-b border-[#E5E7F2]">Data Pasien</h3>
                    <dl class="space-y-3">
                        <div class="flex justify-between">
                            <dt class="text-[#7A7FAE]">No. RM</dt>
                            <dd class="font-medium text-[#2F347A]">{{ $resep->no_rm ?? '-' }}</dd>
                        </div>
                        <div class="flex justify-between">
                            <dt class="text-[#7A7FAE]">Nama</dt>
                            <dd class="font-medium text-[#2F347A]">{{ $resep->nama_pasien }}</dd>
                        </div>
                        <div class="flex justify-between">
                            <dt class="text-[#7A7FAE]">Jenis Kelamin</dt>
                            <dd class="font-medium text-[#2F347A]">{{ $resep->jenis_kelamin == 'L' ? 'Laki-laki' : ($resep->jenis_kelamin == 'P' ? 'Perempuan' : '-') }}</dd>
                        </div>
                        @if($resep->umur_pasien)
                        <div class="flex justify-between">
                            <dt class="text-[#7A7FAE]">Umur</dt>
                            <dd class="font-medium text-[#2F347A]">{{ $resep->umur_pasien }} tahun</dd>
                        </div>
                        @endif
                        @if($resep->berat_badan)
                        <div class="flex justify-between">
                            <dt class="text-[#7A7FAE]">Berat Badan</dt>
                            <dd class="font-medium text-[#2F347A]">{{ $resep->berat_badan }} kg</dd>
                        </div>
                        @endif
                        @if($resep->alamat_pasien)
                        <div class="flex justify-between">
                            <dt class="text-[#7A7FAE]">Alamat</dt>
                            <dd class="font-medium text-[#2F347A] text-right">{{ $resep->alamat_pasien }}</dd>
                        </div>
                        @endif
                    </dl>
                </x-card>

                <!-- Diagnosa & Catatan -->
                @if($resep->diagnosa || $resep->catatan)
                <x-card class="p-6">
                    <h3 class="text-lg font-semibold text-[#2F347A] mb-4 pb-2 border-b border-[#E5E7F2]">Diagnosa & Catatan</h3>
                    @if($resep->diagnosa)
                    <div class="mb-4">
                        <dt class="text-sm text-[#7A7FAE] mb-1">Diagnosa</dt>
                        <dd class="text-[#2F347A]">{{ $resep->diagnosa }}</dd>
                    </div>
                    @endif
                    @if($resep->catatan)
                    <div>
                        <dt class="text-sm text-[#7A7FAE] mb-1">Catatan</dt>
                        <dd class="text-[#2F347A]">{{ $resep->catatan }}</dd>
                    </div>
                    @endif
                </x-card>
                @endif
            </div>

            <!-- Right Column - Medicine List -->
            <div class="lg:col-span-2">
                <x-card class="overflow-hidden">
                    <div class="px-6 py-4 table-header-custom">
                        <h2 class="text-lg font-semibold">Daftar Obat yang Diresepkan</h2>
                    </div>
                    <div class="overflow-x-auto">
                        <table class="w-full">
                            <thead class="bg-[#F4F6FF] border-b border-[#E5E7F2]">
                                <tr>
                                    <th class="px-6 py-3 text-left text-xs font-medium text-[#7A7FAE] uppercase tracking-wider">No</th>
                                    <th class="px-6 py-3 text-left text-xs font-medium text-[#7A7FAE] uppercase tracking-wider">Nama Obat</th>
                                    <th class="px-6 py-3 text-left text-xs font-medium text-[#7A7FAE] uppercase tracking-wider">Jumlah</th>
                                    <th class="px-6 py-3 text-left text-xs font-medium text-[#7A7FAE] uppercase tracking-wider">Aturan Pakai</th>
                                </tr>
                            </thead>
                            <tbody class="bg-white divide-y divide-[#E5E7F2]">
                                @forelse($resep->items as $index => $item)
                                    <tr class="hover:bg-[#F4F6FF]">
                                        <td class="px-6 py-4 whitespace-nowrap text-sm text-[#7A7FAE]">
                                            {{ $index + 1 }}
                                        </td>
                                        <td class="px-6 py-4 whitespace-nowrap">
                                            <div class="font-medium text-[#2F347A]">{{ $item->nama_obat }}</div>
                                            @if($item->obatMasuk)
                                                <div class="text-sm text-[#7A7FAE]">Batch: {{ $item->obatMasuk->kode_batch }}</div>
                                            @endif
                                        </td>
                                        <td class="px-6 py-4 whitespace-nowrap text-sm text-[#2F347A]">
                                            {{ $item->jumlah }} {{ $item->obatMasuk->satuan->nama ?? '' }}
                                        </td>
                                        <td class="px-6 py-4 whitespace-nowrap text-sm text-[#2F347A]">
                                            {{ $item->aturan_pakai ?? '-' }}
                                        </td>
                                    </tr>
                                @empty
                                    <tr>
                                        <td colspan="4" class="px-6 py-12 text-center text-[#7A7FAE]">
                                            Tidak ada obat dalam resep ini
                                        </td>
                                    </tr>
                                @endforelse
                            </tbody>
                        </table>
                    </div>
                    <div class="px-6 py-4 border-t border-[#E5E7F2] bg-[#F4F6FF]">
                        <div class="flex justify-between items-center">
                            <span class="text-[#7A7FAE]">Total Item</span>
                            <span class="font-bold text-[#2F347A]">{{ $resep->items->count() }} obat</span>
                        </div>
                    </div>
                </x-card>
            </div>
        </div>
    </div>
</x-app-layout>
