@extends('layouts.frontend')

@section('title', 'Keranjang Belanja')

@section('content')
    <div class="container-fluid page-header py-5">
        <h1 class="text-center text-white display-6">Keranjang Belanja</h1>
        <ol class="breadcrumb justify-content-center mb-0">
            <li class="breadcrumb-item"><a href="{{ url('/') }}">Home</a></li>
            <li class="breadcrumb-item active text-white">Cart</li>
        </ol>
    </div>

    <div class="container-fluid py-5">
        <div class="container py-5">
            {{-- Alert Notification --}}
            @if(session('success'))
                <div class="alert alert-success alert-dismissible fade show" role="alert">
                    <i class="fa fa-check-circle me-2"></i> {{ session('success') }}
                    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                </div>
            @endif

            <div class="table-responsive">
                <table class="table">
                    <thead>
                        <tr>
                            <th scope="col">Produk</th>
                            <th scope="col">Nama</th>
                            <th scope="col">Harga</th>
                            <th scope="col">Jumlah</th>
                            <th scope="col">Total</th>
                            <th scope="col">Aksi</th>
                        </tr>
                    </thead>
                    <tbody>
                        @php $total_belanja = 0; @endphp
                        @forelse($cart as $id => $details)
                            @php
                                $total = $details['price'] * $details['quantity'];
                                $total_belanja += $total;
                            @endphp
                            <tr data-id="{{ $id }}">
                                <th scope="row">
                                    <div class="d-flex align-items-center">
                                        <img src="{{ $details['photo'] ? asset('storage/' . $details['photo']) : asset('template/frontend/img/vegetable-item-3.png') }}"
                                            class="img-fluid me-5 rounded-circle" style="width: 80px; height: 80px;"
                                            alt="{{ $details['name'] }}">
                                    </div>
                                </th>
                                <td>
                                    <p class="mb-0 mt-4">{{ $details['name'] }}</p>
                                </td>
                                <td>
                                    <p class="mb-0 mt-4">Rp {{ number_format($details['price'], 0, ',', '.') }}</p>
                                </td>
                                <td>
                                    <div class="input-group quantity mt-4" style="width: 100px;">
                                        <div class="input-group-btn">
                                            <button type="button" class="btn btn-sm btn-minus-custom rounded-circle bg-light border">
                                                <i class="fa fa-minus"></i>
                                            </button>
                                        </div>

                                        <input type="text" class="form-control form-control-sm text-center border-0 qty-input bg-white"
                                            value="{{ $details['quantity'] }}" readonly>

                                        <div class="input-group-btn">
                                            <button type="button" class="btn btn-sm btn-plus-custom rounded-circle bg-light border">
                                                <i class="fa fa-plus"></i>
                                            </button>
                                        </div>
                                    </div>
                                </td>
                                <td>
                                    <p class="mb-0 mt-4">Rp {{ number_format($total, 0, ',', '.') }}</p>
                                </td>
                                <td>
                                    <form action="{{ route('cart.remove') }}" method="POST">
                                        @csrf
                                        @method('DELETE')
                                        <input type="hidden" name="id" value="{{ $id }}">
                                        <button class="btn btn-md rounded-circle bg-light border mt-4" onclick="return confirm('Hapus produk ini?')">
                                            <i class="fa fa-times text-danger"></i>
                                        </button>
                                    </form>
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="6" class="text-center py-5">
                                    <div class="d-flex flex-column align-items-center">
                                        <i class="fa fa-shopping-basket fa-3x text-muted mb-3"></i>
                                        <h4>Keranjang belanja masih kosong.</h4>
                                        <a href="{{ route('shop') }}"
                                            class="btn btn-primary rounded-pill px-4 py-2 mt-3 text-white">Mulai Belanja</a>
                                    </div>
                                </td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>

            @if (count($cart) > 0)
                <div class="row g-4 justify-content-end">
                    <div class="col-sm-8 col-md-7 col-lg-6 col-xl-4">
                        <div class="bg-light rounded">
                            <div class="p-4">
                                <h1 class="display-6 mb-4">Total <span class="fw-normal">Belanja</span></h1>
                                <div class="d-flex justify-content-between mb-4">
                                    <h5 class="mb-0 me-4">Subtotal:</h5>
                                    <p class="mb-0">Rp {{ number_format($total_belanja, 0, ',', '.') }}</p>
                                </div>
                            </div>
                            <div class="py-4 mb-4 border-top border-bottom d-flex justify-content-between px-4">
                                <h5 class="mb-0 ps-4 me-4">Total</h5>
                                <p class="mb-0 pe-4">Rp {{ number_format($total_belanja, 0, ',', '.') }}</p>
                            </div>
                            
                            {{-- Button Checkout --}}
                            <a href="{{ route('checkout') }}"
                                class="btn border-secondary rounded-pill px-4 py-3 text-primary text-uppercase mb-4 ms-4"
                                type="button">Proses Checkout</a>
                        </div>
                    </div>
                </div>
            @endif
        </div>
    </div>
@endsection

@section('js')
<script>
    $(document).ready(function () {
        $(".quantity button").off("click"); 

        // Event saat tombol Plus Custom diklik
        $(".btn-plus-custom").click(function (e) {
            e.preventDefault(); 
            var ele = $(this);
            var input = ele.closest("tr").find(".qty-input");
            var currentVal = parseInt(input.val()) || 0; 
            var newVal = currentVal + 1;

            input.val(newVal);
            
            updateCart(ele.closest("tr").attr("data-id"), newVal);
        });

        // Event saat tombol Minus Custom diklik
        $(".btn-minus-custom").click(function (e) {
            e.preventDefault();
            var ele = $(this);
            var input = ele.closest("tr").find(".qty-input");
            var currentVal = parseInt(input.val()) || 0;

            if (currentVal > 1) {
                var newVal = currentVal - 1;
                input.val(newVal);
                updateCart(ele.closest("tr").attr("data-id"), newVal);
            }
        });

        // Fungsi AJAX
        function updateCart(id, qty) {
            $.ajax({
                url: "{{ route('cart.update') }}",
                method: "patch",
                data: {
                    _token: '{{ csrf_token() }}', 
                    id: id, 
                    quantity: qty
                },
                success: function (response) {
                    window.location.reload(); 
                }
            });
        }
    });
</script>
@endsection