@extends('layouts.app')

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

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

@section('content')
<div class="bg-white p-6 rounded-lg shadow-lg">
    <h2 class="text-xl font-bold mb-4">Riwayat Pemesanan</h2>
    
    @if(isset($sewas) && count($sewas) > 0)
    <div class="overflow-x-auto">
        <table class="min-w-full bg-white">
            <thead>
                <tr class="bg-gray-100">
                    <th class="py-3 px-4 text-left">No</th>
                    <th class="py-3 px-4 text-left">Paket</th>
                    <th class="py-3 px-4 text-left">Tanggal Mulai</th>
                    <th class="py-3 px-4 text-left">Tanggal Selesai</th>
                    <th class="py-3 px-4 text-left">Lokasi</th>
                    <th class="py-3 px-4 text-left">Kota Tujuan</th>
                    <th class="py-3 px-4 text-left">Ongkir</th>
                    <th class="py-3 px-4 text-left">Total Harga</th>
                    <th class="py-3 px-4 text-left">Nominal Pembayaran</th>
                    <th class="py-3 px-4 text-left">Status Pembayaran</th>
                    <th class="py-3 px-4 text-left">Status</th>
                    <th class="py-3 px-4 text-left">Bukti pembayaran</th>
                    <th class="py-3 px-4 text-left">Jaminan sewa</th>
                    <th class="py-3 px-4 text-left">Tanggal Pembayaran</th>
                    <th class="py-3 px-4 text-left">Aksi</th>
                </tr>
            </thead>
            <tbody>
                @foreach($sewas as $index => $sewa)
                <tr class="border-b hover:bg-gray-50">
                    <td class="py-3 px-4">{{ $index + 1 }}</td>
                    <td class="py-3 px-4">{{ $sewa->paket->nama_paket }}</td>
                    <td class="py-3 px-4">{{ date('d/m/Y', strtotime($sewa->tanggal_mulai)) }}</td>
                    <td class="py-3 px-4">{{ date('d/m/Y', strtotime($sewa->tanggal_selesai)) }}</td>
                    <td class="py-3 px-4">{{ $sewa->lokasi }}</td>
                    <td class="py-3 px-4">
                        @if($sewa->kota)
                            {{ $sewa->kota->nama_kota }}
                        @else
                            <span class="text-gray-500">-</span>
                        @endif
                    </td>
                    <td class="py-3 px-4">
                        @if($sewa->ongkir)
                            Rp {{ number_format($sewa->ongkir, 0, ',', '.') }}
                        @else
                            <span class="text-gray-500">-</span>
                        @endif
                    </td>
                    <td class="py-3 px-4">Rp {{ number_format($sewa->total_harga, 0, ',', '.') }}</td>
                    <td class="py-3 px-4">
                        @if($sewa->nominal_pembayaran)
                            Rp {{ number_format($sewa->nominal_pembayaran, 0, ',', '.') }}
                        @else
                            <span class="text-gray-500">-</span>
                        @endif
                    </td>
                    <td class="py-3 px-4">
                        @if($sewa->nominal_pembayaran)
                            @if($sewa->nominal_pembayaran >= $sewa->total_harga)
                                <span class="px-2 py-1 bg-green-100 text-green-800 rounded-full text-xs font-semibold">
                                    Lunas
                                </span>
                            @else
                                <span class="px-2 py-1 bg-yellow-100 text-yellow-800 rounded-full text-xs font-semibold">
                                    DP {{ number_format(($sewa->nominal_pembayaran / $sewa->total_harga) * 100, 0) }}%
                                </span>
                            @endif
                        @else
                            <span class="px-2 py-1 bg-red-100 text-red-800 rounded-full text-xs font-semibold">
                                Belum Bayar
                            </span>
                        @endif
                    </td>
                    <td class="py-3 px-4">
                        <span class="
                            @if($sewa->status == 'pending') bg-yellow-100 text-yellow-800 
                            @elseif($sewa->status == 'confirmed') bg-blue-100 text-blue-800 
                            @elseif($sewa->status == 'ongoing') bg-purple-100 text-purple-800 
                            @elseif($sewa->status == 'completed') bg-green-100 text-green-800
                            @elseif($sewa->status == 'dibatalkan') bg-red-100 text-red-800
                            @else bg-red-100 text-red-800 
                            @endif
                            px-2 py-1 rounded-full text-xs font-semibold
                        ">
                            @if($sewa->status == 'pending')
                                Menunggu Verifikasi Admin
                            @elseif($sewa->status == 'confirmed')
                                Disetujui
                            @elseif($sewa->status == 'ongoing')
                                Sedang Berjalan
                            @elseif($sewa->status == 'completed')
                                Selesai
                            @elseif($sewa->status == 'dibatalkan')
                                Dibatalkan
                            @else
                                {{ ucfirst($sewa->status) }}
                            @endif
                        </span>
                    </td>
                    <td class="py-3 px-4">
                        @if($sewa->bukti_pembayaran)
                            <a href="{{ Storage::url($sewa->bukti_pembayaran) }}" target="_blank" class="text-blue-600 hover:text-blue-800">
                                <i class="fas fa-receipt mr-1"></i>bukti pembayaran
                            </a>
                        @else
                            <span class="text-gray-500">-</span>
                        @endif
                    </td>
                    <td class="py-3 px-4">
                        @if($sewa->foto_jaminan)
                            <a href="{{ Storage::url($sewa->foto_jaminan) }}" target="_blank" class="text-blue-600 hover:text-blue-800">
                                <i class="fas fa-id-card mr-1"></i>{{ ucfirst($sewa->jenis_jaminan) }}
                            </a>
                        @else
                            <span class="text-gray-500">-</span>
                        @endif
                    </td>
                    <td class="py-3 px-4">
                        @if($sewa->tanggal_pembayaran)
                            {{ date('d/m/Y H:i', strtotime($sewa->tanggal_pembayaran)) }}
                        @else
                            <span class="text-gray-500">-</span>
                        @endif
                    </td>
                    <td class="py-3 px-4">
                        @if(!in_array($sewa->status, ['completed', 'dibatalkan', 'ongoing']))
                            <form action="{{ route('riwayat.batal', $sewa->id) }}" method="POST">
                                @csrf
                                @method('PUT')
                                <button type="submit" 
                                    onclick="return confirm('Apakah Anda yakin ingin membatalkan pesanan ini?' + 
                                        (('{{ $sewa->status }}' === 'confirmed') ? ' Pembatalan setelah disetujui mungkin dikenakan biaya pembatalan.' : ''))"
                                    class="bg-red-500 text-white px-3 py-1 rounded hover:bg-red-600 mb-2">
                                    Batalkan Pesanan
                                </button>
                            </form>
                        @endif
                    </td>
                </tr>
                @endforeach
            </tbody>
        </table>
    </div>
    @else
    <div class="text-center py-8">
        <p class="text-gray-500">Belum ada riwayat pemesanan</p>
        <a href="{{ route('sewa.index') }}" class="mt-4 inline-block bg-blue-800 text-white py-2 px-4 rounded hover:bg-blue-900 transition duration-200">
            Pesan Sekarang
        </a>
    </div>
    @endif
</div>
@endsection