<?php

namespace App\Http\Livewire\Frontend\Product;

use App\Models\ProductManagement\Category;
use App\Models\ProductManagement\Product;
use App\Services\CartService;
use Gloudemans\Shoppingcart\Facades\Cart;
use Jantinnerezo\LivewireAlert\LivewireAlert;
use Livewire\Component;
use Livewire\WithPagination;

class ShopProductsComponent extends Component{
    use WithPagination, LivewireAlert;

    protected $paginationTheme = 'bootstrap';
    public $paginationLimit = 12;
    public $slug;
    public $sortingBy = 'default';

    public function addToCart($productId){
        $product = Product::whereId($productId)->active()->activeCategory()->firstOrFail();
        try {
            (new CartService)->addToList('default', $product);
            $this->emit('update_cart');
            $this->alert('success', 'added to Cart.');
        } catch(\Exception $exception) {
            $this->alert('warning', $exception->getMessage());
        }
    }

    public function addToWishList($id){
        $product = Product::whereId($id)->active()->activeCategory()->firstOrFail();
        try {
            (new CartService)->addToList('wishlist', $product);
            $this->emit('update_wishlist');
            $this->alert('success', 'added to Wishlist.');
        } catch(\Exception $exception) {
            $this->alert('warning', $exception->getMessage());
        }
    }

    public function render(){
        switch ($this->sortingBy) {
            case 'popularity':
                $sortField = 'id';
                $sortType = 'desc';
                break;
            case 'low-high':
                $sortField = 'price';
                $sortType = 'asc';
                break;
            case 'high-low':
                $sortField = 'price';
                $sortType = 'desc';
                break;
            default:
                $sortField = 'id';
                $sortType = 'asc';
        }

        $products = Product::with("media");

        if ($this->slug == '') {
            $products = $products->activeCategory();
        } else {
            $category = Category::whereSlug($this->slug)->active()->first();

            //dd($category->allProducts()->get()->pluck('id')->toArray());
            $products = $category->allProducts();

//            if (!$category->parent) {
//
//                $categoriesIds = $category->children()->active()->pluck('id')->toArray();
//                $products = $products->whereHas('category', function ($query) use ($categoriesIds) {
//                                        $query->whereIn('id', $categoriesIds);
//                                    });
//
//               // dd(count($products->get()),$products->get()->pluck('id')->toArray());
//
//            } else {
//                $products = $products->with('category')
//                                    ->whereHas('category', function ($query) {
//                                        $query->where(['slug' => $this->slug, 'active' => true]);
//                                    });
//            }
        }

        $products = $products->active()
                     ->orderBy($sortField, $sortType)
                     ->paginate($this->paginationLimit);

        return view('livewire.frontend.product.shop-products-component', compact('products'));
    }
}
