@extends('layouts.app')

@section('content')
<div class="container">
    <h2>Riwayat Pemesanan</h2>

    @if(session('success'))
        <div class="alert alert-success">{{ session('success') }}</div>
    @endif
    @if(session('error'))
        <div class="alert alert-danger">{{ session('error') }}</div>
    @endif

    <table class="table table-bordered">
        <thead>
            <tr>
                <th>No</th>
                <th>Paket</th>
                <th>Kota</th>
                <th>Status</th>
                <th>Total Harga</th>
                <th>Aksi</th>
            </tr>
        </thead>
        <tbody>
            @forelse($sewas as $index => $sewa)
                <tr>
                    <td>{{ $index + 1 }}</td>
                    <td>{{ $sewa->paket->nama_paket ?? '-' }}</td>
                    <td>{{ $sewa->kota->nama_kota ?? '-' }}</td>
                    <td>
                        <span class="badge bg-info">{{ ucfirst($sewa->status) }}</span>
                    </td>
                    <td>Rp {{ number_format($sewa->total_harga, 0, ',', '.') }}</td>
                    <td>
                        <!-- Tombol tandai completed (hanya admin & status confirmed) -->
                        @if(Auth::user()->tipe_pengguna === 'admin' && $sewa->status === 'confirmed')
                        <form action="{{ route('riwayat.updateStatus', $sewa->id) }}" method="POST" style="display:inline;">
                            @csrf
                            <input type="hidden" name="status" value="completed">
                            <button type="submit" class="btn btn-success btn-sm" onclick="return confirm('Tandai sebagai LUNAS / completed?')">Tandai Lunas</button>
                        </form>
                        @endif

                        <!-- Tombol hapus (jika status sudah selesai atau dibatalkan) -->
                        @if($sewa->user_id == Auth::id() && in_array($sewa->status, ['completed', 'dibatalkan']))
                        <form action="{{ url('/riwayat/hapus/'.$sewa->id) }}" method="POST" style="display:inline;">
                            @csrf
                            @method('DELETE')
                            <button type="submit" class="btn btn-danger btn-sm" onclick="return confirm('Yakin ingin menghapus riwayat ini?')">Hapus</button>
                        </form>
                        @endif
                    </td>
                </tr>
            @empty
                <tr>
                    <td colspan="6">Tidak ada riwayat pemesanan.</td>
                </tr>
            @endforelse
        </tbody>
    </table>
</div>
@endsection
