<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0">
    <meta name="csrf-token" content="{{ csrf_token() }}">

    <title>{{ config('app.name', 'Laravel') }}</title>

    <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Nunito:wght@400;600;700&display=swap">
    <link rel="stylesheet" href="{{ asset('css/app.css') }}">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/tiny-slider/2.9.4/tiny-slider.css">
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">

    <script src="https://kit.fontawesome.com/b5101eab7c.js" crossorigin="anonymous"></script>
    <script src="{{ asset('js/app.js') }}" defer></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/tiny-slider/2.9.2/min/tiny-slider.js"></script>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
</head>

<body id="dashboard" onload="alertDashboard('{{ $alert ?? '' }}')">
    <div id="page">

        <section>
            @if (!empty($B2L))
                <span style="font-size: 44px">{{ $bee2link_not_sent ?? 0 }} (B2L)&ensp;
                    <i class="fa-solid fa-triangle-exclamation" style="color: orange"></i>
                </span><br>
            @endif
            @if (empty($PVO))
                <span style="font-size: 44px">{{ $pvo_not_sent ?? 0 }} (PVO)&ensp;
                    <i class="fa-solid fa-triangle-exclamation" style="color: orange"></i>
                </span><br>
            @endif
            <br><br>

            <span style="font-size: 21px">
                Véhicules non remontés depuis le {{ isset($date_prise_en_compte) ? date('d-m-Y - H:i:s', strtotime($date_prise_en_compte)) : 'Aucune donnée' }}
            </span>
            <br>

            <span id="info-vehicle" style="display: none">
                <i class="fa-solid fa-triangle-exclamation" id="warning-pvo" style="color: orange; font-size: 22px"></i>&ensp;
                Dernier véhicule non remonté chez PVO :
                <strong>
                    {{ !empty($lastVehicleNotSendToPvo) ? $lastVehicleNotSendToPvo->name : 'Aucun véhicule' }}
                </strong>
            </span>
            <br>

            <table id="dashboard-vehicules" class="searchlist">
                <tr class="header">
                    <th>Origine</th>
                    <th>Immatriculation</th>
                    <th>Entreprise</th>
                    <th>Concession</th>
                    <th>Date de mise à jour</th>
                    <th>Date de création</th>
                    <th>Opérateur Contacté</th>
                </tr>

                @foreach ($vehicles as $vehicle)
                    <tr>
                        <td><img class="flag" src="{{ asset('assets/images/flags/Flag_of_Europe.svg.png') }}"></td>

                        <td>
                            <a href="https://bo.carlab.fr/clients/{{ $vehicle->client_id }}/vehicule/{{ $vehicle->id }}" target="_blank">
                                {{ $vehicle->name ?? 'Inconnu' }}
                            </a>
                        </td>

                        <td>
                            @php
                                $entreprise = 'Inconnu';
                                foreach ($users as $user) {
                                    if ($user->id === $vehicle->manager_id) {
                                        $entreprise = $user->entreprise ?? $user->name ?? 'Inconnu';
                                        break;
                                    }
                                }
                            @endphp
                            {{ $entreprise }}
                        </td>

                        <td>
                            @php
                                $concession = 'Inconnu';
                                foreach ($users as $user) {
                                    if ($user->id === $vehicle->client_id) {
                                        $concession = $user->name ?? 'No Name';
                                        break;
                                    }
                                }
                            @endphp
                            {{ $concession }}
                        </td>

                        <td>{{ date('d-m-Y H:i:s', strtotime($vehicle->updated_at ?? 'now')) }}</td>
                        <td>{{ date('d-m-Y H:i:s', strtotime($vehicle->created_at ?? 'now')) }}</td>

                        <td>
                            @php
                                $email = null;
                                $tel = null;
                                foreach ($users as $user) {
                                    if ($user->id === $vehicle->operator_id) {
                                        $email = $user->email ?? null;
                                        $tel = $user->tel1 ?? null;
                                        break;
                                    }
                                }
                            @endphp

                            @if ($email && $tel)
                                <td onclick="modalOperator('{{ $email }}', '{{ $tel }}')">
                                    <i class="fa-solid fa-eye"></i>
                                </td>
                            @else
                                <td>Email Non renseigné</td>
                            @endif
                        </td>
                    </tr>
                @endforeach
            </table>
        </section>

        <div>
            {{ $vehicles->links('vendor.pagination.custom') }}
        </div>

        @php
            header('Refresh:60');
        @endphp
    </div>
</body>

</html>
