@extends('layouts.admin')

@section('content')
<div class="container mx-auto px-4 py-8 max-w-3xl">
    <div class="mb-6">
        <a href="{{ route('admin.peminjaman.index') }}" class="text-blue-600 hover:text-blue-800 font-medium">&larr; Kembali ke Daftar Sirkulasi</a>
    </div>

    <div class="bg-white shadow-lg rounded-xl overflow-hidden border border-gray-100 mb-8">
        <div class="p-8 bg-gray-900 text-center">
            <h1 class="text-2xl font-extrabold text-white mb-2">Pindai Barcode Buku</h1>
            <p class="text-gray-400 text-sm mb-6">Arahkan scanner ke label BIBID pada buku yang dikembalikan.</p>
            
            <form action="{{ route('admin.peminjaman.proses_scan') }}" method="POST" class="max-w-md mx-auto relative">
                @csrf
                <input type="text" name="bibid" autofocus autocomplete="off" placeholder="Scan Barcode di sini..." class="w-full pl-12 pr-4 py-4 text-center text-xl font-mono border-2 border-gray-700 bg-gray-800 text-white rounded-lg shadow-sm focus:border-blue-500 focus:ring-blue-500 focus:bg-gray-900 transition-colors">
                <svg class="w-6 h-6 text-gray-500 absolute left-4 top-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v1m6 11h2m-6 0h-2v4m0-11v3m0 0h.01M12 12h4.01M16 20h4M4 12h4m12 0h.01M5 8h2a1 1 0 001-1V5a1 1 0 00-1-1H5a1 1 0 00-1 1v2a1 1 0 001 1zm14 0h2a1 1 0 001-1V5a1 1 0 00-1-1h-2a1 1 0 00-1 1v2a1 1 0 001 1zM5 20h2a1 1 0 001-1v-2a1 1 0 00-1-1H5a1 1 0 00-1 1v2a1 1 0 001 1z"></path></svg>
            </form>
        </div>
    </div>

    @if(session('error'))
    <div class="bg-red-100 border-l-4 border-red-500 text-red-700 p-4 mb-6 rounded shadow-sm" role="alert">
        <p class="font-bold">Pemindaian Gagal</p>
        <p>{{ session('error') }}</p>
    </div>
    @endif

    @if(isset($peminjaman))
    <div class="bg-white shadow-xl rounded-xl overflow-hidden border-2 border-green-500">
        <div class="p-6 bg-green-50 border-b border-green-100 flex justify-between items-center">
            <h2 class="text-xl font-bold text-green-900">Data Transaksi Ditemukan</h2>
            <span class="bg-green-200 text-green-800 px-3 py-1 rounded-full text-xs font-bold uppercase">#{{ $peminjaman->id_peminjaman }}</span>
        </div>
        
        <div class="p-6 grid grid-cols-1 md:grid-cols-2 gap-8">
            <div class="space-y-4">
                <div>
                    <span class="block text-xs font-bold text-gray-500 uppercase">Peminjam</span>
                    <span class="block text-lg font-bold text-gray-900">{{ $peminjaman->anggota?->nama ?? ($peminjaman->user?->name ?? 'Anonim') }}</span>
                </div>
                <div>
                    <span class="block text-xs font-bold text-gray-500 uppercase">Aset Buku</span>
                    <span class="block text-base font-bold text-gray-900">{{ $buku->judul }}</span>
                    <span class="block text-sm text-gray-600 font-mono">{{ $buku->bibid }}</span>
                </div>
                <div>
                    <span class="block text-xs font-bold text-gray-500 uppercase">Tenggat Waktu</span>
                    <span class="block text-sm font-medium {{ $denda > 0 ? 'text-red-600' : 'text-gray-900' }}">
                        {{ \Carbon\Carbon::parse($peminjaman->tanggal_kembali)->format('d F Y') }}
                    </span>
                </div>
            </div>

            <div class="flex flex-col space-y-4">
                <div class="bg-blue-900 rounded-xl p-5 text-center text-white shadow-inner flex-1 flex flex-col justify-center border border-blue-800">
                    <span class="block text-xs font-bold text-blue-300 uppercase tracking-widest mb-2">Panduan Pengembalian Rak Fisik</span>
                    <span class="block text-4xl font-black mb-1 drop-shadow-md">{{ $lokasi['rak'] }}</span>
                    <span class="block text-sm text-blue-100 font-medium bg-blue-950/50 py-1 rounded-full w-max mx-auto px-4 mt-2 border border-blue-700/50">{{ $lokasi['area'] }}</span>
                </div>
                
                @if($denda > 0)
                <div class="bg-red-50 border-2 border-red-200 rounded-xl p-4 text-center">
                    <span class="block text-xs font-bold text-red-600 uppercase mb-1">Denda Keterlambatan</span>
                    <span class="block text-2xl font-black text-red-700">Rp {{ number_format($denda, 0, ',', '.') }}</span>
                </div>
                @endif
            </div>
        </div>

        <div class="p-6 border-t border-gray-100 bg-gray-50 flex justify-end">
            <form action="{{ route('admin.peminjaman.kembalikan', $peminjaman->id_peminjaman) }}" method="POST" onsubmit="return confirm('Selesaikan transaksi dan pulihkan stok buku?');">
                @csrf
                @method('PUT')
                <button type="submit" class="bg-green-600 hover:bg-green-700 text-white font-bold py-4 px-8 rounded-lg shadow-md transition duration-300 w-full md:w-auto text-lg flex items-center gap-2">
                    <svg class="w-6 h-6" 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>
                    Konfirmasi Pengembalian
                </button>
            </form>
        </div>
    </div>
    @endif
</div>
@endsection
