<!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>{{ ($manager_name[0]->entreprise) ? $manager_name[0]->entreprise : $manager_name[0]->name }}</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>
<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>
    <section>
        <br><br>
        <table id="dashboard-vehicules" class="searchlist">
            <tr class="header">
                <th>Manager</th>
                <th>Nbs voitures B2L</th>
                <th>Nbs voitures PVO</th>
                <th>Dernier VO remonté</th>
                <th>Date de prise en compte</th>
            </tr>
            <tr class="header">
                <td>{{ ($manager_name[0]->entreprise) ? $manager_name[0]->entreprise : $manager_name[0]->name }}</td>
                <td>{{ $bee2link_not_sent }}</td>
                <td>{{ $pvo_not_sent }}</td>
                @if(isset($last_vehicle[0]->name))
                    <td> {{ $last_vehicle[0]->name }} <br> {{ $last_vehicle[0]->pvo_sent_at }} </td>
                @else
                    <td>Inconnu</td>
                @endif
                <td>{{ date('d-m-Y H:i:s', strtotime($date_prise_en_compte)) }}</td>
            </tr>
        </table>
        <br>
        <table id="dashboard-vehicules" class="searchlist">
            <tr class="header">
                <th>Origine</th>
                <th>Immatriculation</th>
                <th>Concession</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://mycarlab.fr/clients/{{ $vehicle->client_id }}/vehicule/{{ $vehicle->id }}'
                            target="_blank">{{ $vehicle->name }}</td>

                    @for ($i = 0; $i < count($users); $i++)
                        @if ($users[$i]->id === $vehicle->client_id)
                            @if (isset($users[$i]->name))
                                <td><form action={{ route('client', ['name' => $users[$i]->name ]) }} target="_blank" method="POST">
                                    @csrf
                                    <button type="submit">{{ $users[$i]->name }}</button>
                                </form></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>
