<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0"/>
    <title>Detail Permintaan Peminjaman - {{ config('app.name', 'Laravel') }}</title>
    
    <script src="https://cdn.tailwindcss.com"></script>
    <script src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js" defer></script>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
    
    <style>
        :root {
            --bg: #f8fafc; --panel: #ffffff; --muted: #6b7280; --accent: #0b5ed7;
            --accent-600: #0b63d6; --danger: #dc2626; --card-border: #eef2f7; --soft: #f8fafc;
            --font-main: 'Inter', system-ui, -apple-system, sans-serif;
        }
        * { box-sizing: border-box; }
        
        body { 
            margin: 0; 
            font-family: var(--font-main); 
            background: var(--bg); 
            color: #111827; 
            -webkit-font-smoothing: antialiased;
        }
        
        .app { display: flex; min-height: 100vh; }
        
        /* Content area adjustments for responsiveness */
        .content { 
            flex: 1; 
            padding: 32px; 
            width: 100%;
            transition: all 0.3s ease;
        }

        /* Responsive Styles */
        @media (max-width: 1024px) {
            .app { display: block; }
            .content { padding: 20px; padding-top: 80px; }
        }
        
        @media (max-width: 640px) {
            .content { padding: 16px; padding-top: 80px; }
        }
    </style>
</head>
<body>
    <div class="app">
        {{-- Sidebar --}}
        @include('partials.sidebar')

        {{-- Main Content --}}
        <main class="content">
            <div class="max-w-4xl mx-auto">
                {{-- Header --}}
                <div class="flex flex-col md:flex-row md:items-start justify-between mb-6 md:mb-8 gap-4">
                    <div>
                        <h1 class="text-2xl md:text-3xl font-semibold text-gray-900">Detail Permintaan</h1>
                        <p class="mt-1 text-sm text-gray-600">Informasi lengkap tentang permintaan peminjaman.</p>
                    </div>
                    <div class="flex flex-col sm:flex-row gap-3 w-full md:w-auto">
                        <a href="{{ route('borrow.index') }}" class="inline-flex justify-center items-center px-4 py-2 rounded-lg bg-white border border-gray-300 text-sm font-medium text-gray-700 shadow-sm hover:bg-gray-50 transition-colors">
                            <svg class="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 19l-7-7m0 0l7-7m-7 7h18"></path></svg>
                            Kembali
                        </a>
                        <a href="{{ route('borrow.select') }}" class="inline-flex justify-center items-center px-4 py-2 rounded-lg bg-blue-600 hover:bg-blue-700 text-white text-sm font-medium shadow transition-colors">
                            <svg class="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"></path></svg>
                            Buat Baru
                        </a>
                    </div>
                </div>

                <div class="bg-white rounded-xl shadow-sm border border-gray-200 overflow-hidden">
                    <div class="p-5 md:p-6">
                        <div class="grid grid-cols-1 md:grid-cols-2 gap-6 md:gap-8 mb-8">
                            
                            <!-- Barang Section -->
                            <div>
                                <h2 class="text-lg font-bold text-gray-900 flex items-center gap-2 mb-4 pb-2 border-b border-gray-100">
                                    <svg class="w-5 h-5 text-blue-500" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M20 7l-8-4-8 4m16 0l-8 4m8-4v10l-8 4m0-10L4 7m8 4v10M4 7v10l8 4"></path></svg>
                                    Informasi Barang
                                </h2>
                                <div class="bg-gray-50 rounded-lg p-4 border border-gray-100">
                                    <div class="space-y-3">
                                        <div>
                                            <span class="text-xs font-semibold text-gray-500 uppercase tracking-wider block mb-1">Nama Barang</span>
                                            <span class="text-base font-medium text-gray-900">{{ $borrowRequest->item->nama ?? '-' }}</span>
                                        </div>
                                        <div>
                                            <span class="text-xs font-semibold text-gray-500 uppercase tracking-wider block mb-1">Deskripsi</span>
                                            <span class="text-sm text-gray-600">{{ $borrowRequest->item->deskripsi ?? 'Tidak ada deskripsi' }}</span>
                                        </div>
                                        @if(optional($borrowRequest->item->divisi)->nama)
                                        <div>
                                            <span class="text-xs font-semibold text-gray-500 uppercase tracking-wider block mb-1">Divisi Pemilik</span>
                                            <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-blue-100 text-blue-800">
                                                {{ $borrowRequest->item->divisi->nama }}
                                            </span>
                                        </div>
                                        @endif
                                    </div>
                                </div>
                            </div>

                            <!-- Peminjam Section -->
                            <div>
                                <h2 class="text-lg font-bold text-gray-900 flex items-center gap-2 mb-4 pb-2 border-b border-gray-100">
                                    <svg class="w-5 h-5 text-blue-500" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M16 7a4 4 0 11-8 0 4 4 0 018 0zM12 14a7 7 0 00-7 7h14a7 7 0 00-7-7z"></path></svg>
                                    Detail Peminjaman
                                </h2>
                                <div class="space-y-4">
                                    <div class="flex justify-between border-b border-gray-50 pb-2">
                                        <span class="text-sm text-gray-500">Peminjam</span>
                                        <span class="text-sm font-medium text-gray-900 text-right">{{ optional($borrowRequest->user)->name ?? '-' }}</span>
                                    </div>
                                    <div class="flex justify-between border-b border-gray-50 pb-2">
                                        <span class="text-sm text-gray-500">Tanggal Pinjam</span>
                                        <span class="text-sm font-medium text-gray-900 text-right">{{ optional($borrowRequest->tanggal_pinjam)->format('d M Y, H:i') ?? '-' }}</span>
                                    </div>
                                    <div class="flex justify-between border-b border-gray-50 pb-2">
                                        <span class="text-sm text-gray-500">Rencana Kembali</span>
                                        <span class="text-sm font-medium text-gray-900 text-right">{{ $borrowRequest->tanggal_kembali ? $borrowRequest->tanggal_kembali->format('d M Y, H:i') : '-' }}</span>
                                    </div>
                                    @if($borrowRequest->actual_return_date)
                                    <div class="flex justify-between border-b border-gray-50 pb-2 bg-green-50 px-2 rounded -mx-2">
                                        <span class="text-sm text-green-700 font-medium">Dikembalikan</span>
                                        <span class="text-sm font-bold text-green-700 text-right">{{ \Carbon\Carbon::parse($borrowRequest->actual_return_date)->format('d M Y, H:i') }}</span>
                                    </div>
                                    @endif
                                    <div class="flex justify-between border-b border-gray-50 pb-2">
                                        <span class="text-sm text-gray-500">Jumlah</span>
                                        <span class="text-sm font-medium text-gray-900 text-right">{{ $borrowRequest->jumlah ?? '-' }} Unit</span>
                                    </div>
                                    <div class="flex justify-between items-center pt-1">
                                        <span class="text-sm text-gray-500">Status Saat Ini</span>
                                        @php 
                                            $st = strtolower($borrowRequest->status ?? 'pending'); 
                                            $badgeColor = match($st) {
                                                'approved', 'final_approved' => 'bg-green-100 text-green-800',
                                                'rejected' => 'bg-red-100 text-red-800',
                                                'returned' => 'bg-gray-100 text-gray-800',
                                                'revision_requested' => 'bg-yellow-100 text-yellow-800',
                                                'staff_approved', 'kepala_approved' => 'bg-blue-100 text-blue-800',
                                                default => 'bg-yellow-50 text-yellow-700'
                                            };
                                            $label = ucfirst(str_replace('_', ' ', $st));
                                        @endphp
                                        <span class="inline-flex px-3 py-1 text-xs font-bold rounded-full {{ $badgeColor }}">
                                            {{ $label }}
                                        </span>
                                    </div>
                                </div>
                            </div>
                        </div>

                        {{-- Action Buttons (Mobile: Vertical Stack, Desktop: Horizontal) --}}
                        <div class="flex flex-col sm:flex-row sm:items-center sm:justify-end gap-3 pt-6 border-t border-gray-100">
                            {{-- Logic Tombol untuk Staff Bidang --}}
                            @if(auth()->user()->role === 'staff_bidang' && $borrowRequest->status === 'pending')
                                <form action="{{ route('borrow.approve', $borrowRequest->id) }}" method="POST" class="w-full sm:w-auto">
                                    @csrf @method('PATCH')
                                    <button type="submit" class="w-full sm:w-auto justify-center inline-flex items-center px-4 py-2 rounded-lg bg-green-600 hover:bg-green-700 text-white text-sm font-medium shadow transition-colors">
                                        <svg class="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"></path></svg>
                                        Setujui
                                    </button>
                                </form>

                                <form action="{{ route('borrow.reject', $borrowRequest->id) }}" method="POST" class="w-full sm:w-auto">
                                    @csrf @method('PATCH')
                                    <button type="submit" class="w-full sm:w-auto justify-center inline-flex items-center px-4 py-2 rounded-lg bg-red-600 hover:bg-red-700 text-white text-sm font-medium shadow transition-colors">
                                        <svg class="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"></path></svg>
                                        Tolak
                                    </button>
                                </form>

                                <form action="{{ route('borrow.revision', $borrowRequest->id) }}" method="POST" class="w-full sm:w-auto">
                                    @csrf @method('PATCH')
                                    <button type="submit" class="w-full sm:w-auto justify-center inline-flex items-center px-4 py-2 rounded-lg bg-yellow-500 hover:bg-yellow-600 text-white text-sm font-medium shadow transition-colors">
                                        <svg class="w-4 h-4 mr-2" 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"></path></svg>
                                        Minta Revisi
                                    </button>
                                </form>
                            @endif

                            {{-- Logic Tombol untuk User Biasa (Revisi) --}}
                            @if(auth()->user()->role === 'user' && $borrowRequest->status === 'revision_requested')
                                <a href="{{ route('borrow.edit', $borrowRequest->id) }}" class="w-full sm:w-auto justify-center inline-flex items-center px-4 py-2 rounded-lg bg-yellow-500 hover:bg-yellow-600 text-white text-sm font-medium shadow transition-colors">
                                    <svg class="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15.232 5.232l3.536 3.536m-2.036-5.036a2.5 2.5 0 113.536 3.536L6.5 21.036H3v-3.572L16.732 3.732z"></path></svg>
                                    Revisi Permintaan
                                </a>
                            @endif
                        </div>
                    </div>

                    {{-- Note Section --}}
                    @if($borrowRequest->comment)
                    <div class="px-5 md:px-6 pb-6">
                        <div class="bg-gray-50 border border-gray-100 rounded-lg p-4">
                            <h3 class="text-sm font-bold text-gray-800 mb-2 flex items-center gap-2">
                                <svg class="w-4 h-4 text-gray-500" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M7 8h10M7 12h4m1 8l-4-4H5a2 2 0 01-2-2V6a2 2 0 012-2h14a2 2 0 012 2v8a2 2 0 01-2 2h-3l-4 4z"></path></svg>
                                Catatan Peminjaman
                            </h3>
                            <p class="text-sm text-gray-600 leading-relaxed">{{ $borrowRequest->comment }}</p>
                        </div>
                    </div>
                    @endif

                    {{-- History Timeline --}}
                    <div class="px-5 md:px-6 pb-6 md:pb-8">
                        <h3 class="text-base font-bold text-gray-900 mb-4">Riwayat Aktivitas</h3>
                        @if($borrowRequest->histories && $borrowRequest->histories->count())
                            <div class="relative pl-4 border-l-2 border-gray-100 space-y-6">
                                @foreach($borrowRequest->histories as $h)
                                    <div class="relative">
                                        <!-- Dot Indicator -->
                                        <div class="absolute -left-[21px] top-1.5 w-3 h-3 rounded-full border-2 border-white 
                                            {{ $h->action == 'rejected' ? 'bg-red-500' : ($h->action == 'approved' || $h->action == 'final_approved' ? 'bg-green-500' : 'bg-blue-500') }} shadow-sm"></div>
                                        
                                        <div class="flex flex-col sm:flex-row sm:items-baseline sm:justify-between gap-1">
                                            <p class="text-sm font-semibold text-gray-800">
                                                {{ ucfirst(str_replace('_', ' ', $h->action)) }}
                                                <span class="text-xs font-normal text-gray-500 ml-1">oleh {{ $h->user->name ?? 'System' }} ({{ $h->role ?? '-' }})</span>
                                            </p>
                                            <span class="text-xs text-gray-400 whitespace-nowrap">{{ optional($h->created_at)->format('d M Y, H:i') }}</span>
                                        </div>
                                        @if($h->comment)
                                            <div class="mt-1 p-2 bg-gray-50 rounded text-xs text-gray-600 border border-gray-100">
                                                "{{ $h->comment }}"
                                            </div>
                                        @endif
                                    </div>
                                @endforeach
                            </div>
                        @else
                            <div class="text-center py-6 bg-gray-50 rounded-lg border border-dashed border-gray-200">
                                <p class="text-sm text-gray-500">Belum ada riwayat aktivitas.</p>
                            </div>
                        @endif
                    </div>
                </div>
            </div>
        </main>
    </div>
</body>
</html>