@extends('layouts.app')

@section('title', 'Daftar Sewa - INUFA')

@section('header', 'Daftar Sewa')

@section('content')
<div class="container mx-auto px-4 py-8">
    <div class="bg-white rounded-xl shadow-lg overflow-hidden">
        <div class="p-6">
            <div class="flex justify-between items-center mb-6">
                <h2 class="text-2xl font-bold text-gray-800">
                    {{ $showDetailPenyewa ? 'Detail Semua Penyewaan' : 'Daftar Sewa' }}
                </h2>
            </div>

            <div class="overflow-x-auto">
                <table class="min-w-full divide-y divide-gray-200">
                    <thead class="bg-gray-50">
                        <tr>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">No</th>
                            @if($showDetailPenyewa)
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Penyewa</th>
                            @endif
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Paket</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Tanggal Sewa</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Status</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Total Harga</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Aksi</th>
                        </tr>
                    </thead>
                    <tbody class="bg-white divide-y divide-gray-200">
                        @forelse($sewas as $index => $sewa)
                        <tr>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                                {{ $index + 1 }}
                            </td>
                            @if($showDetailPenyewa)
                            <td class="px-6 py-4 whitespace-nowrap">
                                <div class="flex items-center">
                                    <div>
                                        <div class="text-sm font-medium text-gray-900">
                                            {{ $sewa->user->nama }}
                                        </div>
                                        <div class="text-sm text-gray-500">
                                            {{ $sewa->user->email }}
                                        </div>
                                    </div>
                                </div>
                            </td>
                            @endif
                            <td class="px-6 py-4 whitespace-nowrap">
                                <div class="text-sm text-gray-900">{{ $sewa->paket->nama_paket }}</div>
                                <div class="text-sm text-gray-500">{{ ucfirst($sewa->paket->jenis_paket) }}</div>
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap">
                                <div class="text-sm text-gray-900">
                                    {{ \Carbon\Carbon::parse($sewa->tanggal_mulai)->format('d M Y') }}
                                </div>
                                <div class="text-sm text-gray-500">
                                    {{ \Carbon\Carbon::parse($sewa->tanggal_selesai)->format('d M Y') }}
                                </div>
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap">
                                <span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full 
                                    @if($sewa->status == 'completed') bg-green-100 text-green-800
                                    @elseif($sewa->status == 'confirmed') bg-blue-100 text-blue-800
                                    @elseif($sewa->status == 'ongoing') bg-yellow-100 text-yellow-800
                                    @else bg-gray-100 text-gray-800
                                    @endif">
                                    {{ ucfirst($sewa->status) }}
                                </span>
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                                Rp {{ number_format($sewa->total_harga, 0, ',', '.') }}
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                                <button onclick="showDetail('{{ $sewa->id }}')" 
                                        class="bg-blue-500 hover:bg-blue-600 text-white px-3 py-1 rounded-md text-sm">
                                    Detail
                                </button>
                            </td>
                        </tr>
                        @empty
                        <tr>
                            <td colspan="{{ $showDetailPenyewa ? 7 : 6 }}" class="px-6 py-4 text-center text-gray-500">
                                Tidak ada data sewa
                            </td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<!-- Modal Detail -->
<div id="detailModal" class="fixed inset-0 bg-black bg-opacity-50 hidden items-center justify-center">
    <div class="bg-white rounded-lg p-8 max-w-2xl w-full mx-4">
        <div class="flex justify-between items-center mb-6">
            <h3 class="text-xl font-bold text-gray-900">Detail Penyewaan</h3>
            <button onclick="closeModal()" class="text-gray-400 hover:text-gray-500">
                <svg class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12" />
                </svg>
            </button>
        </div>
        <div id="modalContent" class="space-y-4">
            <!-- Content will be loaded here -->
        </div>
    </div>
</div>

@push('scripts')
<script>
function showDetail(sewaId) {
    fetch(`/sewa/${sewaId}/detail`)
        .then(response => response.json())
        .then(data => {
            const modal = document.getElementById('detailModal');
            const content = document.getElementById('modalContent');
            
            content.innerHTML = `
                <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                    <div class="space-y-3">
                        <div>
                            <h4 class="text-sm font-medium text-gray-500">Informasi Penyewa</h4>
                            <p class="text-gray-900">${data.user.nama}</p>
                            <p class="text-gray-600">${data.user.email}</p>
                            <p class="text-gray-600">${data.user.no_hp || '-'}</p>
                        </div>
                        <div>
                            <h4 class="text-sm font-medium text-gray-500">Paket Disewa</h4>
                            <p class="text-gray-900">${data.paket.nama_paket}</p>
                            <p class="text-gray-600">${data.paket.jenis_paket}</p>
                        </div>
                    </div>
                    <div class="space-y-3">
                        <div>
                            <h4 class="text-sm font-medium text-gray-500">Detail Sewa</h4>
                            <p class="text-gray-600">Mulai: ${data.tanggal_mulai}</p>
                            <p class="text-gray-600">Selesai: ${data.tanggal_selesai}</p>
                            <p class="text-gray-600">Status: ${data.status}</p>
                        </div>
                        <div>
                            <h4 class="text-sm font-medium text-gray-500">Pembayaran</h4>
                            <p class="text-gray-900">Total: Rp ${data.total_harga}</p>
                            <p class="text-gray-600">Dibayar: Rp ${data.nominal_pembayaran || 0}</p>
                        </div>
                    </div>
                    <div class="col-span-2">
                        <h4 class="text-sm font-medium text-gray-500">Lokasi Pengiriman</h4>
                        <p class="text-gray-900">${data.lokasi}</p>
                        <p class="text-gray-600">Kota: ${data.kota ? data.kota.nama_kota : '-'}</p>
                    </div>
                </div>
            `;
            
            modal.classList.remove('hidden');
            modal.classList.add('flex');
        })
        .catch(error => {
            console.error('Error:', error);
            alert('Terjadi kesalahan saat memuat detail');
        });
}

function closeModal() {
    const modal = document.getElementById('detailModal');
    modal.classList.add('hidden');
    modal.classList.remove('flex');
}

// Close modal when clicking outside
document.getElementById('detailModal').addEventListener('click', function(e) {
    if (e.target === this) {
        closeModal();
    }
});
</script>
@endpush
@endsection 