<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Http;
use Illuminate\Http\Request;

class CategoryController extends Controller
{    
    public function index(){        
        $response = Http::withHeaders([            
        'Authorization' => 'Bearer '.env('VITE_BEARER_TOKEN__GEN_API'),
        'Content-Type' => 'application/json',
        ])->get(env('VITE_SERVICES_API').'category/list');
        $data = $response->json();            
        return $data;
    }

    public function productsCategory(Request $request){             
        $objCategoryController = new CategoryController();        
        $objCategoryBanner = $objCategoryController->CategoryControllerList($request->url);
        $categoriasProducts = $objCategoryController->ProductsControllerList($request->url);
        return view('productcategory',['objCategory'=> $categoriasProducts,'objCategoryBanner'=> $objCategoryBanner]);
    }

    public function ProductsControllerList($url){
       $response = Http::withHeaders([            
        'Authorization' => 'Bearer '.env('VITE_BEARER_TOKEN__GEN_API'),
        'Content-Type' => 'application/json',
        ])->get(env('VITE_SERVICES_API').'products/category/'.$url);
        $objlistCategory = collect($response->json());  
        $objHomeController = new ProductsController();
        $categoriasProducts = $objlistCategory->map(function ($categoria) use ($objHomeController) {
            $detalle = $objHomeController->productDetail($categoria['url']);
            $categoria['productodetail'] = $detalle;
            return $categoria;
        });
        return $categoriasProducts;
    }

    public function CategoryControllerList($url){
        $objCategoryController = new CategoryController();
        return $objCategoryBanner = $objCategoryController->categoryId($url);
    }
    
     public function categoryId($url){        
        $objCategoryController = new CategoryController();
        $objCategory = $objCategoryController->index();   
        $data = collect($objCategory)->firstWhere('url',$url);   
        return $data;

    }

     public function categoryList($url,$ord){     

      $objCategoryController = new CategoryController();
      $objCategoryBanner = $objCategoryController->CategoryControllerList($url);
      $objCategoryNew = $objCategoryController->ProductsControllerList($url);     
        
        if($ord == "0"){
            $objCategoryList = $objCategoryNew;
        }
        if($ord == "1"){
            $objCategoryList = $objCategoryNew->sortByDesc('titulo')->values();
        }
        if($ord == "2"){
            $objCategoryList = $objCategoryNew->sortBy('titulo')->values();
        }
        if($ord == "3"){
            $objCategoryList = $objCategoryNew->sortByDesc('precio_original')->values();
        }
        if($ord == "4"){
            $objCategoryList = $objCategoryNew->sortBy('precio_original')->values();
        } 
    

        $body ="";         
        $body.="<ul id='sec-main-category' class='sec-main__con2 row row--f'>";
        foreach ($objCategoryList as $a) {
        $body.="<li class='sec-main__ite col col--f'>";  
        $body.="<a aria-hidden='false' class='sxs-pcrd sxs-pcrd--squared' href='".env('VITE_HTML_BASE').'productos/'.$a['url']."'>";
        $body.="<div class='sxs-pcrd__samp'><span class='sxs-pcrd__samp-wtm-1'></span><span class='sxs-pcrd__samp-wtm-2'></span>";
        $body.="<figure class='figure media-con media-cover'><img loading='lazy' alt='' class='lazy loaded t-opacity' src='".$a['galeria'][0]['img']."'></figure>";
        $body.="</div>";
        $body.="</a>";
        $body.="<div class='sxs-pcrd__text'>";
        $body.="<a aria-hidden='false' class='sxs-pcrd sxs-pcrd--squared' href='".env('VITE_HTML_BASE').'productos/'.$a['url']."'>";
        $body.="<div class='sxs-pcrd__txt'>";
        $body.="<div class='sxs-pcrd__tit'>".$a['titulo']."</div>";
        $body.="<div class='sxs-pcrd__pce'>$ ".$a['productodetail']['precio_original']." MXN</div>";
        $body.="</div>";
        $body.="</a>";
        $body.="<div class='sxs-pcrd__ftr'>"; 
        if(isset($a['stock'])){  
        if(strlen(trim($a['stock'])) > 0 && trim($a['stock']) != '0'){
        $body.="<div class='sxs-qpckr sxs-qpckr--v1'>";
        $body.="<button type='button' data-url='".$a['url']."' data-id='amt".$a['url']."' data-a='amt' data-tope='2' onclick=agritem(this) class='sxs-qpckr__btn' aria-label='Disminuir cantidad' aria-disabled='false'><i class='icon icon-misc1 menos'></i></button>";
        $body.="<div id='amt".$a['url']."' class='sxs-qpckr__amt' aria-live='polite' aria-atomic='true' role='status'>1</div>";
        $body.="<button type='button' data-url='".$a['url']."' data-id='amt".$a['url']."' data-a='amt' data-tope='1' onclick=agritem(this) class='sxs-qpckr__btn' aria-label='Aumentar cantidad'><i class='icon icon-misc1 mas'></i></button>";
        $body.="</div>";
        $body.="<button type='button' data-url='".$a['url']."' data-a='amt' data-tope='1' onclick=addcard(this) aria-hidden='false' class='sxs-btn sxs-btn--primary sxs-btn--style-primary sxs-pcrd__btn'>";
        $body.="<div class='sxs-btn__con0'><span class='sxs-btn__wv'></span>";
        $body.="<div class='sxs-btn__con1'>";
        $body.="<div class='sxs-btn__con2'><span class=''><span class='sas-bs' style='--sas-idx: 0;'>A</span><span class='sas-bs' style='--sas-idx: 1;'>g</span><span class='sas-bs' style='--sas-idx: 2;'>r</span><span class='sas-bs' style='--sas-idx: 3;'>e</span><span class='sas-bs' style='--sas-idx: 4;'>g</span><span class='sas-bs' style='--sas-idx: 5;'>a</span><span class='sas-bs' style='--sas-idx: 6;'>r</span></span></div>";
        $body.="<div class='sxs-btn__con2'><span class=''><span class='sas-bs' style='--sas-idx: 0;'>A</span><span class='sas-bs' style='--sas-idx: 1;'>g</span><span class='sas-bs' style='--sas-idx: 2;'>r</span><span class='sas-bs' style='--sas-idx: 3;'>e</span><span class='sas-bs' style='--sas-idx: 4;'>g</span><span class='sas-bs' style='--sas-idx: 5;'>a</span><span class='sas-bs' style='--sas-idx: 6;'>r</span></span></div>";
        $body.="</div>";
        $body.="</div>";
        $body.="</button>";                                 
        }else{
        $body.="<span>Agotado</span>";
        }
        }else{
        $body.="<span>Agotado</span>";
        } 
        $body.="</div>";
        $body.="</div>";
        $body.="</li>";                                
        }
        $body.="</ul>";        
        return $body;
    }

 
}
