<?php

use Livewire\Component;

new class extends Component
{
    public function render()
    {
        $subtotal = 0;
        $carts = \App\Models\Cart::query()->with('product')->where('session_id', session()->getId())->where('is_buy', 0)->get();
        foreach ($carts as $cart) {
            $unitTotal = $cart->numbers * $cart->product->price;
            $unitTotal = round($unitTotal, 2);
            $cart->singletotal = number_format($unitTotal, 2, '.', ',');

            $subtotal += $unitTotal;
        }
        $tax = getenv('PAYPAL_TAX') * $subtotal;
        $shipping = 0;
        $total = $subtotal + $tax;
        $subtotal = number_format($subtotal, 2, '.', ',');
        $tax = number_format($tax, 2, '.', ',');
        $total = number_format($total, 2, '.', ',');
        $count = count($carts);
        return $this->view(compact('tax', 'subtotal', 'total', 'carts','count'));
    }

    public function incOrDec(string $type, int $id): void
    {
        $cart = \App\Models\Cart::query()->with('product')->findOrFail($id);
        if ($type === 'plus') {
            $cart->numbers = $cart->numbers + 1;
        } else {
            if ($cart->numbers > 1) {
                $cart->numbers = $cart->numbers - 1;
            }
        }
        $cart->singletotal = round($cart->numbers * $cart->product->price, 2);
        $cart->save();

    }

    public function destroy(int $id)
    {
        \App\Models\Cart::query()->where('id', $id)->delete();
    }
};
?>

<div class="cart-section section pt-90 pt-lg-70 pt-md-60 pt-sm-50 pt-xs-45  pb-70 pb-lg-50 pb-md-40 pb-sm-30 pb-xs-20">
    <div class="container">
        <div class="row">

            <div class="col-12">
                <!-- Cart Table -->
                <div class="cart-table table-responsive mb-30">
                    <table class="table">
                        <thead>
                        <tr>
                            <th class="pro-thumbnail"></th>
                            <th class="pro-title">Producto</th>
                            <th class="pro-price">Precio</th>
                            <th class="pro-quantity">Cantidad</th>
                            <th class="pro-subtotal">Subtotal</th>
                            <th class="pro-remove">Borrar</th>
                        </tr>
                        </thead>
                        <tbody>
                        @foreach($carts as $cart)
                            <tr>
                                <td class="pro-thumbnail"><a href="{{ $cart->product->url }}"><img src="{{ $cart->product->okpcode }}" alt="{{ ucfirst($cart->product->locale) }} {{ $cart->product->jianjie2 }} - ({{ $cart->product->jianjie1 }})"></a></td>
                                <td class="pro-title"><a href="{{ $cart->product->url }}">
                                    @if ($cart->product->category === 'battery')
                                    Batería {{ $cart->product->jianjie2 }} {{ $cart->product->dl }} - ({{ $cart->product->jianjie1 }})
                                    @else
                                    Cargador {{ $cart->product->jianjie2 }} {{ $cart->product->bzq }} - ({{ $cart->product->jianjie1 }})
                                    @endif
                                </a></td>
                                <td class="pro-price"><span>{{ $cart->product->price_symbol }}</span></td>
                                <td class="pro-quantity">
                                    <div class="pro-qty">
                                        <button class="dec qtybtn"  wire:click="incOrDec('minus',{{ $cart->id }})">-</button>
                                        <input type="number" value="{{ $cart->numbers }}">
                                        <button class="inc qtybtn" wire:click="incOrDec('plus',{{ $cart->id }})">+</button>
                                    </div>
                                </td>
                                <td class="pro-subtotal"><span>{{ $cart->singletotal }} €</span></td>
                                <td class="pro-remove"><a href="javascript:;"  wire:click="destroy({{ $cart->id }})" wire:confirm="¿Desea eliminarlo?"><svg data-v-a6a0ac03="" id="MIcon-PublishTrashbin24-v-19" fill="currentColor" aria-hidden="true" viewBox="0 0 24 24" height="1.5rem" width="1.5rem"><path d="M18.08 8a1 1 0 00-1.08.92L16.08 20H7.92L7 8.92a1 1 0 10-2 .16l1 12A1 1 0 007 22h10a1 1 0 001-.92l1-12A1 1 0 0018.08 8zM19 5h-3.77l-.65-2.27a1 1 0 00-1-.73h-3.2a1 1 0 00-1 .73L8.77 5H5a1 1 0 000 2h14a1 1 0 000-2zm-8.62-2h3.24l.57 2H9.81z"></path><path d="M12.5 18v-8a.5.5 0 00-1 0v8a.5.5 0 001 0zM14.25 18.5a.5.5 0 00.5-.47l.5-8a.5.5 0 00-.47-.53.49.49 0 00-.53.47l-.5 8a.5.5 0 00.47.53zM9.75 18.5a.5.5 0 00.47-.53l-.5-8a.49.49 0 00-.53-.47.5.5 0 00-.47.53l.5 8a.5.5 0 00.53.47z"></path></svg></a></td>
                            </tr>
                        @endforeach

                        </tbody>
                    </table>
                </div>

                <div class="row">

                    <div class="col-lg-6 col-12 mb-5">
                        <h2>
                            <svg fill="none" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24" stroke-width="1.5" aria-hidden="true"><path d="M16.19 9.12L11.19 14.12L8.38 11.31M18.59 15.66L12 20.12L5.41 15.66L5.38 5.25L12 3.75L18.56 5.25L18.59 7.71V15.66Z" stroke="currentColor"></path></svg>
                              Compra 100% segura
                        </h2>
                        <p>Ponemos a tu disposición múltiples formas de pago para que lo realices como te resulte más cómodo. Con toda la confianza y seguridad de trabajar con los principales proveedores de pago en línea.</p>
                        <p>¡Tus compras protegidas y seguras!</p>
                        <!-- Calculate Shipping -->

                        <!-- Discount Coupon -->
                    </div>

                    <!-- Cart Summary -->
                    <div class="col-lg-6 col-12 mb-30 d-flex">
                        <div class="cart-summary">
                            <div class="cart-summary-wrap">
                                <h4>Resumen del pedido</h4>
                                <p>Subtotal <span>{{ $subtotal }} €</span></p>
                                <p>Coste de envío <span>Gratis</span></p>
                                <p>Impuestos <span>{{ symbol($tax) }}</span></p>
                                <h2>Total (impuestos inc.) <span>{{ $total }} €</span></h2>
                            </div>
                            <div class="cart-summary-button">
                                <livewire:carts.pay />
                            </div>
                        </div>
                    </div>

                </div>

            </div>

        </div>
    </div>
</div>
