@extends('layouts.app')

@section('content')

<div class="container mt-5">
    <h1>Your Cart</h1>
    @if (session(key: 'cart') && count(session('cart'))>0)

    <table class="table table-bordered">
        <thead>
            <tr>
                <th>Image</th>
                <th>Name</th>
                <th>Price</th>
                <th>Qty</th>
                <th>Total</th>
                <th>Action</th>
            </tr>
        </thead>
        <tbody>
            @foreach (session('cart') as $id=>$item )
            <tr>
                <td>
                    <img src="{{ asset('storage/' .$item['image']) }}" width="50">
                </td>
                <td>{{ $item['name'] }}</td>
                <td>${{ $item['price'] }}</td>
                <td>
                    <form action="{{ route('cart.update') }}" method="post">
                        @csrf
                        <input type="hidden" name="id" value="{{ $id }}">
                        <input type="number" name="quantity" value="{{ $item['quantity'] }}" min="1">
                        <button type="submit" class="btn btn-sm btn-succes">Update</button>
                    </form>
                </td>
                <td>${{ $item['price'] * $item['quantity']}}</td>
                <td>
                    <form action="{{ route('cart.remove' ,$id) }}" method="POST">
                        @csrf
                        <button type="submit" class="btn btn-sm btn-danger">Remove</button>
                    </form>
                </td>
            </tr>      
            @endforeach
        </tbody>
    </table>
    @else
    <p>Your cart is Empty...</p>
    @endif
    
</div>

<a href="{{ route('cart.checkout') }}" class="btn btn-secondary">Place Order</a>

@endsection