<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ProductsController extends Controller
{
    public function index(Request $request, $catalogs = null)
    {
        $catalogs = explode('/', $catalogs);
        $catalogSlug = array_pop($catalogs);
        $applications = $request->get('tags');
        $currentApplications = explode(',', $applications);
        $currentApplications = array_values(array_filter($currentApplications));
        $perPageItems = 10;
        $page = $request->get("p");
        $page = (!$page || $page < 1) ? 1 : $page;
        $pageOffset = $perPageItems * ($page - 1);

        $productRows = \App\Models\Product::where('is_front_show', '=', true)
            ->orderByRaw("CASE
                WHEN tip = 'none' THEN 1
                WHEN tip = 'new' THEN 2
                WHEN tip = 'on_sale' THEN 3
            END DESC")
            ->orderBy('id', 'DESC');
            
        $dataCatalogs = \App\Models\ProductCatalog::where('is_front_show', '=', true)
            ->orderBy('lft', 'asc')
            ->select('id', 'slug', 'name', \DB::raw('(
                SELECT COUNT(id) FROM products 
                WHERE products.product_catalog_id = product_catalogs.id AND products.is_front_show = true
                ) as qty'));

        $dataApplications = \App\Models\ProductApplication::where('is_front_show', '=', true)
            ->orderBy('lft', 'asc')
            ->select('id', 'slug', 'name');

        $currentCatalogName = \App\Models\ProductCatalog::where('slug', 'like', $catalogSlug)->first();
        if ($currentCatalogName) 
        {
            $currentCatalogName = $currentCatalogName->name;
        }

        if (count($currentApplications) > 0)
        {
            $productRows = $productRows->whereHas('productApplications', 
                function($query) use ($currentApplications) {
                    $query->whereIn('slug', $currentApplications);
                });
        }

        if ($catalogSlug) 
        {
            $productRows = $productRows->whereHas('productCatalog',
                function($query) use ($catalogSlug) {
                    $query->where('slug', 'like', $catalogSlug);
                });
        }
        
        
        $productCount =  $productRows->count();
        $productRows = $productRows->skip($pageOffset)->take($perPageItems)->get();
        foreach ($productRows as $row)
        {
            $row->{'applications'} = \App\Models\Product::find($row->id)->productApplications()->get();
            
        }
        
        return view('products', [
            'dataCatalogs' => $dataCatalogs->get(),
            'currentCatalogName' => $currentCatalogName,
            'dataApplications' => $dataApplications->get(),
            'currentApplications' => $currentApplications,
            'dataRows' => $productRows,
            'dataCurrentPage' => $page,
            'dataPerPageItems' => $perPageItems,
            'dataTotalCount' => $productCount,
        ]);
    }

    public function catalogs($catalogs, Request $request)
    {
        return $this->index($request, $catalogs);
    }

    public function detail($product, Request $request)
    {
        $dataRow = \App\Models\Product::where('slug', 'like', $product)
            ->where('is_front_show', '=', true)
            ->first();
        
        $catalogName = '';
        $catalogSlug = '';
        $catalog = $dataRow->productCatalog()->first();
        if ($catalog)
        {
            $catalogName = $catalog->name;
            $catalogSlug = $catalog->slug;
        }

        return view('products_detail', [
            'dataRow' => $dataRow,
            'catalogName' => $catalogName,
            'catalogSlug' => $catalogSlug,
            'productApplications' => $dataRow->productApplications()->get(),
        ]);
    }

}
