<?php

use Livewire\Component;

return 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 = round(getenv('PAYPAL_TAX') * $subtotal, 2);
        $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();
    }

    protected function view($data = [])
    {
        return app('view')->file('D:\laragon\2026\es0701\storage\framework\views/livewire/views/163633a4.blade.php', $data);
    }
};
