<!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>Riwayat Peminjaman - {{ config('app.name') }}</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">
    <script src="https://unpkg.com/lucide@latest"></script>
    
    <style>
        :root {
            --bg: #f8fafc; --panel: #ffffff; --muted: #6b7280; --accent: #0b5ed7;
            --card-border: #e2e8f0; --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 { 
            flex: 1; 
            padding: 32px; 
            width: 100%;
            transition: all 0.3s ease;
        }

        @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 class="content">
            <div class="max-w-6xl mx-auto space-y-6">
                
                {{-- Header --}}
                <div class="flex flex-col md:flex-row md:items-center justify-between gap-4 border-b border-gray-200 pb-6">
                    <div>
                        <h1 class="text-2xl md:text-3xl font-bold text-gray-900 tracking-tight flex items-center gap-3">
                            <i data-lucide="history" class="w-8 h-8 text-blue-600"></i>
                            Riwayat Peminjaman
                        </h1>
                        <p class="text-sm text-gray-500 mt-1">Daftar seluruh aktivitas peminjaman yang telah selesai, ditolak, atau dibatalkan.</p>
                    </div>
                </div>

                {{-- List Riwayat --}}
                <div class="bg-white rounded-xl shadow-sm border border-gray-200 overflow-hidden">
                    
                    {{-- 1. Tampilan Desktop (Table) --}}
                    <div class="hidden md:block overflow-x-auto">
                        <table class="min-w-full divide-y divide-gray-200">
                            <thead class="bg-gray-50">
                                <tr>
                                    <th scope="col" class="px-6 py-4 text-left text-xs font-bold text-gray-500 uppercase tracking-wider">Barang</th>
                                    <th scope="col" class="px-6 py-4 text-left text-xs font-bold text-gray-500 uppercase tracking-wider">Waktu Pinjam</th>
                                    <th scope="col" class="px-6 py-4 text-left text-xs font-bold text-gray-500 uppercase tracking-wider">Waktu Selesai</th>
                                    <th scope="col" class="px-6 py-4 text-left text-xs font-bold text-gray-500 uppercase tracking-wider">Kondisi Akhir</th>
                                    <th scope="col" class="px-6 py-4 text-center text-xs font-bold text-gray-500 uppercase tracking-wider">Status</th>
                                </tr>
                            </thead>
                            <tbody class="divide-y divide-gray-100 bg-white">
                                @forelse($myLoanHistory as $history)
                                <tr class="hover:bg-gray-50 transition-colors">
                                    <td class="px-6 py-4">
                                        <div class="font-medium text-gray-900">{{ $history->item->nama }}</div>
                                        <div class="text-xs text-gray-500 mt-1 bg-gray-100 inline-block px-1.5 py-0.5 rounded">{{ $history->jumlah }} Unit</div>
                                    </td>

                                    <td class="px-6 py-4 text-sm text-gray-600 whitespace-nowrap">
                                        {{ \Carbon\Carbon::parse($history->created_at)->format('d M Y, H:i') }}
                                    </td>

                                    <td class="px-6 py-4 text-sm text-gray-600 whitespace-nowrap">
                                        @if($history->updated_at)
                                            <div class="flex flex-col">
                                                <span class="font-medium text-gray-900">{{ \Carbon\Carbon::parse($history->updated_at)->format('d M Y') }}</span>
                                                <span class="text-xs text-gray-500">{{ \Carbon\Carbon::parse($history->updated_at)->format('H:i') }}</span>
                                            </div>
                                        @else
                                            <span class="text-gray-400 italic">-</span>
                                        @endif
                                    </td>

                                    <td class="px-6 py-4 whitespace-nowrap">
                                        @if($history->status == 'returned')
                                            @if($history->return_condition == 'baik')
                                                <span class="inline-flex items-center gap-1 px-2 py-1 rounded-md text-xs font-medium bg-green-50 text-green-700">
                                                    <i data-lucide="check-circle" class="w-3.5 h-3.5"></i> Baik
                                                </span>
                                            @elseif($history->return_condition == 'rusak_ringan')
                                                <span class="inline-flex items-center gap-1 px-2 py-1 rounded-md text-xs font-medium bg-yellow-50 text-yellow-700">
                                                    <i data-lucide="alert-triangle" class="w-3.5 h-3.5"></i> Rusak Ringan
                                                </span>
                                            @elseif($history->return_condition == 'rusak_berat')
                                                <span class="inline-flex items-center gap-1 px-2 py-1 rounded-md text-xs font-medium bg-red-50 text-red-700">
                                                    <i data-lucide="alert-octagon" class="w-3.5 h-3.5"></i> Rusak Berat
                                                </span>
                                            @endif
                                        @else
                                            <span class="text-gray-400 text-xs italic">N/A (Batal/Tolak)</span>
                                        @endif
                                    </td>

                                    <td class="px-6 py-4 whitespace-nowrap text-center">
                                        @if($history->status == 'returned')
                                            <span class="px-3 py-1 rounded-full text-[11px] font-bold bg-blue-100 text-blue-700 uppercase tracking-wide">Kembali</span>
                                        @elseif($history->status == 'rejected')
                                            <span class="px-3 py-1 rounded-full text-[11px] font-bold bg-red-100 text-red-700 uppercase tracking-wide">Ditolak</span>
                                        @elseif($history->status == 'cancelled')
                                            <span class="px-3 py-1 rounded-full text-[11px] font-bold bg-gray-100 text-gray-700 uppercase tracking-wide">Batal</span>
                                        @endif
                                    </td>
                                </tr>
                                @empty
                                <tr>
                                    <td colspan="5" class="px-6 py-16 text-center">
                                        <div class="flex flex-col items-center">
                                            <div class="bg-gray-50 p-4 rounded-full mb-4">
                                                <i data-lucide="clipboard-list" class="w-12 h-12 text-gray-300"></i>
                                            </div>
                                            <h3 class="text-gray-900 font-semibold">Belum Ada Riwayat</h3>
                                            <p class="text-gray-500 text-sm max-w-xs mx-auto mt-1">Seluruh data peminjaman yang telah selesai akan muncul secara otomatis di sini.</p>
                                        </div>
                                    </td>
                                </tr>
                                @endforelse
                            </tbody>
                        </table>
                    </div>

                    {{-- 2. Tampilan Mobile (Card Stack) --}}
                    <div class="md:hidden divide-y divide-gray-100">
                        @forelse($myLoanHistory as $history)
                        <div class="p-4 bg-white space-y-4">
                            <div class="flex justify-between items-start">
                                <div>
                                    <h3 class="text-sm font-bold text-gray-900">{{ $history->item->nama }}</h3>
                                    <p class="text-xs text-gray-500 mt-0.5">{{ $history->jumlah }} Unit</p>
                                </div>
                                @if($history->status == 'returned')
                                    <span class="text-[10px] font-bold px-2 py-0.5 rounded bg-blue-50 text-blue-600 uppercase">Returned</span>
                                @else
                                    <span class="text-[10px] font-bold px-2 py-0.5 rounded bg-gray-100 text-gray-600 uppercase">{{ $history->status }}</span>
                                @endif
                            </div>
                            
                            <div class="grid grid-cols-2 gap-4 pt-2">
                                <div class="space-y-1">
                                    <p class="text-[10px] uppercase text-gray-400 font-bold">Pinjam</p>
                                    <p class="text-xs text-gray-700 font-medium">{{ \Carbon\Carbon::parse($history->created_at)->format('d/m/y') }}</p>
                                </div>
                                <div class="space-y-1">
                                    <p class="text-[10px] uppercase text-gray-400 font-bold">Selesai</p>
                                    <p class="text-xs text-gray-700 font-medium">{{ \Carbon\Carbon::parse($history->updated_at)->format('d/m/y') }}</p>
                                </div>
                            </div>

                            @if($history->status == 'returned')
                            <div class="bg-gray-50 p-2 rounded flex justify-between items-center mt-2">
                                <span class="text-[10px] text-gray-500 font-semibold uppercase">Kondisi:</span>
                                <span class="text-xs font-bold text-gray-700">{{ ucfirst(str_replace('_', ' ', $history->return_condition)) }}</span>
                            </div>
                            @endif
                        </div>
                        @empty
                        <div class="p-12 text-center text-gray-400 text-sm">
                            Tidak ada data ditemukan.
                        </div>
                        @endforelse
                    </div>
                </div>

                {{-- Pagination --}}
                <div class="mt-6">
                    {{ $myLoanHistory->links() }}
                </div>
            </div>
        </main>
    </div>
    <script>lucide.createIcons();</script>
</body>
</html>