@extends('layouts.user')

@section('title', 'Keranjang')

@section('content')
<div class="container mt-4">
    <h2>🛒 Keranjang Belanja</h2>

    @if($cartItems->isEmpty())
        <p>Keranjang kosong 😋</p>
    @else
        <table class="table table-bordered">
            <tr>
                <th>Pizza</th>
                <th>Jumlah</th>
                <th>Harga (Satuan)</th>
                <th>Total Harga</th>
                <th>Aksi</th>
            </tr>

            @php $grandTotal = 0; @endphp

            @foreach ($cartItems as $item)
                @php
                    // Antisipasi item cart yang kehilangan relasi pizza
                    $pizza = $item->pizza;
                    $total = $pizza ? ($pizza->harga * $item->jumlah) : 0;
                    $grandTotal += $total;
                @endphp

                <tr>
                    <td>{{ $pizza->nama ?? 'Produk sudah dihapus' }}</td>
                    <td>{{ $item->jumlah }}</td>
                    <td>
                        {{ $pizza ? 'Rp ' . number_format($pizza->harga, 0, ',', '.') : '-' }}
                    </td>
                    <td>
                        {{ $pizza ? 'Rp ' . number_format($total, 0, ',', '.') : '-' }}
                    </td>
                    <td>
                       <form action="{{ route('user.cart.delete', $item->id) }}" method="POST">
    @csrf
    @method('DELETE')
    <button type="submit">Hapus</button>
</form>


                        </form>
                    </td>
                </tr>
            @endforeach
        </table>

        <div class="mt-3 p-3 bg-light rounded">
            <h4>Total Belanja: <strong>Rp {{ number_format($grandTotal, 0, ',', '.') }}</strong></h4>

            <a href="{{ route('user.checkout') }}" class="btn btn-success mt-2">
                Checkout 📦
            </a>
        </div>
    @endif
</div>
@endsection
