<!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">
        <div id="myModal" class="modal">
            <div class="modal-content">
                <div class="modal-header">
                    <span class="close">&times;</span>
                    <h1>OPERATEUR</h1>
                </div>
                <div class="modal-body">
                    <p id="email"></p>
                    <p id="tel"></p>
                </div>
            </div>
        </div>
        <div id="myModal-alert" class="modal-alert">
            <div class="modal-content-alert">
                <div class="modal-header-alert">
                    <span class="close">&times;</span>
                    <h1>PVO DOWN</h1>
                </div>
                <div class="modal-body-alert">
                    <p>Envoie SMS telephone / EMAIL vers un e-mail dédié qui doit être configurable dans le .env</p>
                </div>
                <div>
                    <form action="" method=POST>
                        @csrf
                        <button>Envoyer</button>
                    </form>
                </div>
            </div>
        </div>
        <section>

            @if ($B2L == true)
                <span style="font-size: 44px">{{ $bee2link_not_sent }} (B2L)&ensp;<i
                        class="fa-solid fa-triangle-exclamation" style="color: orange"></i></span><br>
            @endif
            @if ($PVO !== true)
                <span style="font-size: 44px">{{ $pvo_not_sent }} (PVO)&ensp;<i class="fa-solid fa-triangle-exclamation"
                        style="color: orange"></i></span><br>
            @endif
            @if ($B2L !== true)
                <span style="font-size: 34px">Dernier véhicule B2L : X</span><br>
            @endif
            @if ($PVO !== true)
                <span style="font-size: 34px">Dernier véhicule PVO :
                    {{ date('d-m-Y', strtotime($last_vehicle[0]->pvo_sent_at)) }} - {{ date('H:i:s', strtotime($last_vehicle[0]->pvo_sent_at)) }}</span><br>
            @endif
            <br>
            <br>
            <span style="font-size: 21px">Véhicules non remontés depuis le {{ date('d-m-Y - H:i:s', strtotime($date_prise_en_compte)) }}</span>
            <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>{{ $lastVehicleNotSendToPvo->name }}</strong></span>
            <table id="dashboard-vehicules" class="searchlist">
                <tr class="header">
                    <th>Origine</th>
                    <th>Immatriculation</th>
                    @if ($DASHBOARD_DEBUG == true)
                        <th>Manager_ID</th>
                    @endif
                    <th>Entreprise</th>
                    <th>Concession</th>
                    {{-- <th>Client_ID</th> --}}
                    @if ($B2L == true)
                        @if ($DASHBOARD_DEBUG == true)
                            <th>id_client_bee2link</th>
                        @endif
                        <th>Etat envoi Bee2link</th>
                    @endif
                    @if ($PVO == true)
                        @if ($DASHBOARD_DEBUG == true)
                            <th>id_client_pvo</th>
                        @endif
                        <th>Etat envoi PVO</th>
                    @endif
                    <th>Date de mise à jour</th>
                    <th>Date de création</th>
                    <th>Opérateur Contacté</th>
                    <th>Détails</th>
                </tr>
                @foreach ($vehicles as $vehicle)
                    <tr class="header">
                        @php
                            $pattern_immat_1 = '/^[A-Z]{2}[0-9]{3}[A-Z]{2}$/i';
                            $pattern_immat_2 = '/^[A-Z]{2}[-][0-9]{3}[-][A-Z]{2}$/i';
                            $pattern_immat_3 = '/^[A-Z]{2}[ ][0-9]{3}[ ][A-Z]{2}$/i';
                            $pattern_chassis = '/^[0-9ABCDEFGHJKLMNPRSTUVWXYZ]{17}$/i';
                            $string_matching = $vehicle->name;
                        @endphp

                        @if (preg_match($pattern_immat_1, $string_matching) || preg_match($pattern_immat_2, $string_matching) || preg_match($pattern_immat_3, $string_matching) || preg_match($pattern_chassis, $string_matching))
                            <td><img class="flag" src="{{ asset("assets/images/flags/Flag_of_Europe.svg.png") }}"></td>
                        @else
                            <td class="td-rouge">Invalide</td>
                        @endif

                        <td><a href='https://bo.carlab.fr/clients/{{ $vehicle->client_id }}/vehicule/{{ $vehicle->id }}' target="_blank">{{ $vehicle->name }}</td>
                            @if ($DASHBOARD_DEBUG == true)
                                @if ($vehicle->manager_id)
                                    <td>{{ $vehicle->manager_id }}</td>
                                @else
                                    <td>Inconnu</td>
                                @endif
                            @endif
                        @if (isset($vehicle->manager_id))

                            @for ($i = 0; $i < count($users); $i++)
                                @if ($users[$i]->id === $vehicle->manager_id)
                                    @if (isset($users[$i]->entreprise))
                                        <td>{{ $users[$i]->entreprise }}</td>
                                    @else
                                        <td>{{ $users[$i]->name }}</td>
                                    @endif
                                @endif
                            @endfor

                        @else
                            <td>Inconnu</td>
                        @endif

                        @for ($i = 0; $i < count($users); $i++)
                            @if ($users[$i]->id === $vehicle->client_id)
                                @if (isset($users[$i]->name))
                                    <td>{{ $users[$i]->name }}</td>
                                @else
                                    <td>No Name</td>
                                @endif
                            @endif
                        @endfor
                        {{-- <td>{{ $vehicle->client_id }}</td> --}}
                        @if ($B2L === true)
                            @if ($vehicle->bee2link_sent === null && ($vehicle->id_bee2link === null && $vehicle->id_client_bee2link === null))
                                @if ($DASHBOARD_DEBUG == true)
                                    <td class="td-gris"> Pas de compte </td>
                                @endif
                                <td class="td-gris"> Pas de compte B2L </td>
                            @endif
                            @if ($vehicle->bee2link_sent === null && ($vehicle->id_bee2link != null || $vehicle->id_client_bee2link != null))
                                @if ($DASHBOARD_DEBUG == true)
                                    <td class="td-rouge"> Active </td>
                                @endif
                                <td class="td-rouge"> Véhicule non remonté </td>
                            @endif
                            @if ($vehicle->bee2link_sent === 1 && ($vehicle->id_bee2link != null || $vehicle->id_client_bee2link != null))
                                @if ($DASHBOARD_DEBUG == true)
                                    <td class="td-vert"> Active </td>
                                @endif
                                <td class="td-vert"> OK </td>
                            @endif
                        @endif
                        @if ($PVO === true)
                            @if ($vehicle->pvo_sent === null && ($vehicle->id_pvo === null && $vehicle->id_client_pvo === null))
                                @if ($DASHBOARD_DEBUG == true)
                                    <td class="td-gris"> Pas de compte </td>
                                @endif
                                <td class="td-gris"> Pas de compte PVO </td>
                            @endif
                            @if ($vehicle->pvo_sent === null && ($vehicle->id_pvo != null || $vehicle->id_client_pvo != null))
                                @if ($DASHBOARD_DEBUG == true)
                                    <td class="td-rouge"> Active </td>
                                @endif
                                <td class="td-rouge"> Véhicule non remonté </td>
                            @endif
                            @if ($vehicle->pvo_sent === 1 && ($vehicle->id_pvo != null || $vehicle->id_client_pvo != null))
                                @if ($DASHBOARD_DEBUG == true)
                                    <td class="td-vert"> Active </td>
                                @endif
                                <td class="td-vert"> OK </td>
                            @endif
                        @endif
                        <td>{{ date('d-m-Y H:i:s', strtotime($vehicle->updated_at)) }}</td>
                        <td>{{ date('d-m-Y H:i:s', strtotime($vehicle->created_at)) }}</td>

                        @switch($vehicle->mail_sent_operator)
                            @case(null)
                                <td class="td-rouge">Non</td>
                            @break

                            @case(1)
                                <td class="td-vert">1 fois</td>
                            @break

                            @case(2)
                                <td class="td-orange">2 fois</td>
                            @break

                            @case(3)
                                <td class="td-rouge">3 fois</td>
                            @break

                            @default
                        @endswitch

                        @for ($i = 0; $i < count($users); $i++)
                            @if ($users[$i]->id === $vehicle->operator_id)
                                @if (isset($users[$i]->email) && isset($users[$i]->tel1))
                                    @php
                                        $email = $users[$i]->email;
                                        $tel = $users[$i]->tel1;
                                    @endphp
                                    <td onclick="modalOperator('{{ $email }}', '{{ $tel }}')">
                                        <i class="fa-solid fa-eye"></i>
                                    </td>
                                @else
                                    <td>Email Non renseigné</td>
                                @endif
                            @endif
                        @endfor
                    </tr>
                @endforeach
            </table>
        </section>
        <div>
            {{ $vehicles->links('vendor.pagination.custom') }}
        </div>
        @php
            header('Refresh:60');
        @endphp
    </div>
</body>
</html>
