<?php

namespace App\Http\Controllers\web\home;

use App\Http\Controllers\Controller;
use App\Models\Cart;
use App\Models\Product;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class cartController extends Controller
{
    public function view_cart(){
        return view('web.home.cart.cart');
    }
    public function updateCart(Request $request)
    {

        $cartid = $request->input('cartid');
        $quantity = $request->input('quantity');
        $cartItem = Cart::where('id', $cartid)->first();


        // Update the quantity in the cart

        if ($cartItem) {
            $cartItem->qty = $quantity;
            $cartItem->total=$quantity * $cartItem->product_price;
            $cartItem->save();
            return response()->json(['success' => true]);
        }

        return response()->json(['success' => false, 'message' => 'Cart item not found']);
    }

    public function remove_cart($id){
        $data=Cart::find($id);
        $result=$data->delete();
        if($result){
            return redirect()->back();
        }
    }

    public function cart_checkout(){
        return view('web.home.checkout.cart-checkout');
    }


}
