<?php

namespace App\Traits;

use App\Models\AccessRoute;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Route;
use GuzzleHttp\Client;

trait Menue
{

    static function home()
    {

        // $routes = Route::getRoutes()->getRoutes();
        // $html = '';


        // $url = 'https://master.jadara.work/api/management/project/uEwdmEqbQgZ5INvKoF';

        // $client = new Client();

        // $guzzleResponse = $client->get($url);

        // if ($guzzleResponse->getStatusCode() == 200) {

        //     $my_routes = json_decode($guzzleResponse->getBody(), true);

        //     if ($my_routes['status'] != false) {

        //         $my_routes = collect($my_routes['data']['management_home_routes']);

        //         $i = 0;

        //         foreach ($routes as $route) {

        //             if ($routes && in_array($route->getName(), $my_routes->pluck('name_route')->toArray())) {

        //                 $parents = $my_routes[$i]['childs'];

        //                 if (count($parents) > 0  && $my_routes[$i]['active'] == true) {

        //                     $count = $my_routes[$i]['show_count'] == true ? getCount($my_routes[$i]['name_table'] , $my_routes[$i]['show_count']) : '';

        //                     $html .= '<div class="col-xl-3 col-md-6 mb-4">
        //                             <div class="card border-left-primary shadow h-100 py-2">
        //                                 <div class="card-body">
        //                                     <div class="row no-gutters align-items-center">
        //                                         <div class="col mr-2">
        //                                             <a href="' . route($my_routes[$i]['name_route']) . '">
        //                                                 <div class="text-xs font-weight-bold text-primary text-uppercase mb-1">' . $my_routes[$i]['name_model_ar'] . '</div>
        //                                             </a>
        //                                             <div class="h5 mb-0 font-weight-bold text-gray-800">' . $count . '</div>
        //                                         </div>
        //                                         <div class="col-auto">
        //                                             <img src="' . $my_routes[$i]['icon_model'] . '" width="50px" />
        //                                         </div>
        //                                     </div>
        //                                 </div>
        //                             </div>
        //                         </div>';
        //                 }

        //                 $i++;
        //             }
        //         }

        //         return $html;
        //     } else {
        //         return $html;
        //     }
        // } else {
        //     return 'data not fount';
        // }


        $routes = Route::getRoutes()->getRoutes();
        $html = '';



        $my_routes = AccessRoute::has('childs')->get();
        

        if ($my_routes->isNotEmpty()) {

            $i = 0;

            foreach ($routes as $key => $route) {

                if ($routes && in_array($route->getName(), $my_routes->pluck('name_route')->toArray())) {
                 
                    $parents = $my_routes[$i]->childs;

                    if (count($parents) > 0) {

                        $count = $my_routes[$i]->show_count == true ? getCount($my_routes[$i]->name_table, $my_routes[$i]->show_count) : '';

                        $html .='<div class="col-xl-3 col-md-6 mb-4">
                                    <div class="card border-left-primary shadow h-100 py-2">
                                        <div class="card-body">
                                            <div class="row no-gutters align-items-center">
                                                <div class="col mr-2">
                                                    <a href="' . route($my_routes[$i]->name_route) . '">
                                                        <div class="text-xs font-weight-bold text-primary text-uppercase mb-1">' . $my_routes[$i]->name_model . '</div>
                                                    </a>
                                                    <div class="h5 mb-0 font-weight-bold text-gray-800">' . $count . '</div>
                                                </div>
                                                <div class="col-auto">
                                                    <img src="' . $my_routes[$i]->icon_model . '" width="50px" />
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>';
                    }

                    $i++;
                }
            }

            return $html;
        } else {
            return $html;
        }
    }
}
