@extends('layouts.app')

@section('title', "Your Shopping Cart - MJ's Bikes")

@section('content')

<!-- BREADCRUMBS -->
<div class="bg-light py-3 border-bottom mb-4">
    <div class="container">
        <nav aria-label="breadcrumb">
            <ol class="breadcrumb mb-0">
                <li class="breadcrumb-item"><a href="{{ route('home') }}" class="text-danger text-decoration-none">Home</a></li>
                <li class="breadcrumb-item"><a href="{{ route('marketplace.index') }}" class="text-danger text-decoration-none">Showroom</a></li>
                <li class="breadcrumb-item active" aria-current="page">Cart</li>
            </ol>
        </nav>
    </div>
</div>

<div class="container mb-5">
    <h2 class="brand-font text-uppercase text-secondary mb-4"><i class="bi bi-cart3 text-danger me-2"></i>Shopping <span class="text-danger">Cart</span></h2>

    @if(count($cart) > 0)
        <div class="row g-4">
            
            <!-- CART ITEMS GRID -->
            <div class="col-lg-8">
                <div class="card border-0 shadow-sm rounded-4 p-3 bg-white mb-3">
                    <div class="table-responsive d-none d-md-block">
                        <table class="table table-hover align-middle mb-0">
                            <thead>
                                <tr class="text-uppercase text-secondary" style="font-size: 11px;">
                                    <th scope="col" style="width: 50%;">Product Details</th>
                                    <th scope="col" class="text-center">Price</th>
                                    <th scope="col" class="text-center" style="width: 15%;">Quantity</th>
                                    <th scope="col" class="text-center">Total</th>
                                    <th scope="col"></th>
                                </tr>
                            </thead>
                            <tbody>
                                @php $subtotal = 0; @endphp
                                @foreach($cart as $id => $item)
                                    @php 
                                        $subtotal += $item['price'] * $item['quantity'];
                                        $itemImg = mjs_image($item['image'] ?? null, $id);
                                    @endphp
                                    <tr data-id="{{ $id }}">
                                        <td>
                                            <div class="d-flex align-items-center gap-3">
                                                <img src="{{ $itemImg }}" class="rounded-3 border" style="width: 70px; height: 70px; object-fit: cover;" alt="{{ $item['name'] }}">
                                                <div>
                                                    <span class="text-danger text-uppercase fw-bold d-block" style="font-size: 10px;">{{ $item['brand'] ?? 'Generic' }}</span>
                                                    <h6 class="fw-bold text-secondary mb-0">{{ $item['name'] }}</h6>
                                                </div>
                                            </div>
                                        </td>
                                        <td class="text-center fw-semibold text-secondary">
                                            KSh {{ number_format($item['price']) }}
                                        </td>
                                        <td class="text-center">
                                            <input type="number" class="form-control form-control-sm rounded-pill text-center update-cart-qty" min="1" max="{{ max(1, $item['stock_quantity'] ?? 10) }}" value="{{ $item['quantity'] }}">
                                        </td>
                                        <td class="text-center fw-bold text-danger">
                                            KSh {{ number_format($item['price'] * $item['quantity']) }}
                                        </td>
                                        <td class="text-end">
                                            <button type="button" class="btn btn-outline-danger btn-sm rounded-circle remove-from-cart" title="Remove Item"
                                                    data-swal-url="{{ route('cart.remove') }}"
                                                    data-swal-title="Remove item?"
                                                    data-swal-text="Remove this item from your cart?">
                                                <i class="bi bi-trash"></i>
                                            </button>
                                        </td>
                                    </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>

                    <!-- Mobile Responsive Cart Rows -->
                    <div class="d-md-none">
                        @foreach($cart as $id => $item)
                            @php 
                                $itemImg = mjs_image($item['image'] ?? null, $id);
                            @endphp
                            <div class="card p-3 border-light rounded-4 shadow-sm mb-3 position-relative cart-item-mobile-row" data-id="{{ $id }}">
                                <div class="d-flex gap-3 align-items-center">
                                    <img src="{{ $itemImg }}" class="rounded-3 border" style="width: 80px; height: 80px; object-fit: cover;" alt="{{ $item['name'] }}">
                                    <div class="flex-grow-1">
                                        <span class="text-danger text-uppercase fw-bold d-block" style="font-size: 9px;">{{ $item['brand'] ?? 'Generic' }}</span>
                                        <h6 class="fw-bold text-secondary mb-1" style="font-size: 14px;">{{ $item['name'] }}</h6>
                                        <div class="fw-bold text-danger mb-2" style="font-size: 15px;">KSh {{ number_format($item['price']) }}</div>
                                        
                                        <div class="d-flex align-items-center justify-content-between">
                                            <div class="d-flex align-items-center gap-2">
                                                <span class="small text-muted">Qty:</span>
                                                <input type="number" class="form-control form-control-sm rounded-pill text-center update-cart-qty" min="1" max="{{ max(1, $item['stock_quantity'] ?? 10) }}" value="{{ $item['quantity'] }}" style="width: 70px;">
                                            </div>
                                            <button type="button" class="btn btn-sm btn-outline-danger rounded-circle remove-from-cart" style="width: 32px; height: 32px; padding: 0;"
                                                    data-swal-url="{{ route('cart.remove') }}"
                                                    data-swal-title="Remove item?"
                                                    data-swal-text="Remove this item from your cart?">
                                                <i class="bi bi-trash"></i>
                                            </button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        @endforeach
                    </div>
                </div>

                <div class="d-flex justify-content-between align-items-center mb-4">
                    <a href="{{ route('marketplace.index') }}" class="btn btn-outline-secondary rounded-pill px-4">
                        <i class="bi bi-arrow-left me-1"></i> Continue Shopping
                    </a>
                    
                    <form action="{{ route('cart.clear') }}" method="POST" class="swal-confirm-form"
                          data-title="Clear entire cart?"
                          data-text="All items will be removed from your cart. This cannot be undone."
                          data-confirm-text="Yes, clear cart">
                        @csrf
                        <button type="submit" class="btn btn-outline-danger rounded-pill px-4">Clear Cart</button>
                    </form>
                </div>
            </div>

            <!-- ORDER SUMMARY PANEL -->
            <div class="col-lg-4">
                <div class="card border-0 shadow-sm rounded-4 p-4 bg-white sticky-lg-top" style="top: 100px; z-index: 10;">
                    <h5 class="fw-bold text-secondary mb-4 border-bottom pb-2">Order Summary</h5>
                    
                    <div class="d-flex justify-content-between mb-2">
                        <span class="text-muted">Subtotal</span>
                        <span class="fw-semibold text-secondary">KSh {{ number_format($subtotal) }}</span>
                    </div>
                    <div class="d-flex justify-content-between mb-3">
                        <span class="text-muted">Delivery Charges</span>
                        <span class="fw-semibold text-success">KSh 350.00</span>
                    </div>
                    
                    <hr>
                    
                    <div class="d-flex justify-content-between align-items-center mb-4">
                        <span class="fw-bold text-secondary fs-5">Estimated Total</span>
                        <span class="fw-extrabold text-danger fs-4">KSh {{ number_format($subtotal + 350) }}</span>
                    </div>

                    <a href="{{ route('checkout.index') }}" class="btn btn-primary-mj btn-lg w-100 rounded-pill py-3 text-uppercase fs-6">
                        Proceed to Checkout <i class="bi bi-credit-card ms-2"></i>
                    </a>
                    
                    <div class="text-center mt-3">
                        <small class="text-muted"><i class="bi bi-shield-lock me-1"></i>Secure checkout handled via M-Pesa or Cash on Delivery</small>
                    </div>
                </div>
            </div>

        </div>
    @else
        <!-- EMPTY CART STATE -->
        <div class="card border-0 shadow-sm rounded-4 p-5 text-center my-5 bg-white">
            <div class="text-danger fs-1 mb-3"><i class="bi bi-cart-x"></i></div>
            <h3 class="fw-bold text-secondary mb-2">Your Cart is Empty</h3>
            <p class="text-muted mb-4">You haven't added any products to your shopping cart yet. Visit our showroom catalog to discover premium bikes and accessories.</p>
            <div class="d-flex justify-content-center">
                <a href="{{ route('marketplace.index') }}" class="btn btn-primary-mj rounded-pill px-5">Explore Showroom Catalog</a>
            </div>
        </div>
    @endif
</div>

@endsection

@section('scripts')
<script>
    document.addEventListener('DOMContentLoaded', function () {
        // Handle Cart Quantity Change
        const qtyInputs = document.querySelectorAll('.update-cart-qty');
        qtyInputs.forEach(input => {
            input.addEventListener('change', function () {
                const tr = this.closest('[data-id]');
                const id = tr.getAttribute('data-id');
                const qty = this.value;
                
                fetch("{{ route('cart.update') }}", {
                    method: 'PATCH',
                    headers: {
                        'Content-Type': 'application/json',
                        'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').getAttribute('content')
                    },
                    body: JSON.stringify({ id: id, quantity: qty })
                })
                .then(res => res.json())
                .then(data => {
                    if (data.success) {
                        location.reload();
                    }
                });
            });
        });

    });
</script>
@endsection
