@extends('layouts.index')
@section('content')

<div class="pagetitle">
    <h1>GESTION DES GARES</h1>
</div><!-- End Page Title -->

<section class="section">
    @if ($message = Session::get('success'))
    <div class="alert alert-success alert-block">
        <button type="button" class="close" data-dismiss="alert">×</button>
        <strong>{{ $message }}</strong>
    </div>
    @endif
    <div class="row">
        <div class="col-lg-12">
            <div class="card">
                <div class="card-body">
                    <h5 class="card-title">Informations...</h5>
                    <!-- General Form Elements -->
                    <form action="{{ url('/gare') }}" method="POST">
                        @csrf
                        <div class="row col-lg-12">
                            <div class="col-lg-12">
                                <div class="row mb-3">
                                    <label class="col-sm-2 col-form-label">NOM:</label>
                                    <div class="col-sm-4">
                                        <input type="text" name="nom_gare" class="form-control">
                                    </div>

                                    <label class="col-sm-2 col-form-label">RESPONSABLE :</label>
                                    <div class="col-sm-4">
                                        <select class="form-select" name="responsable" aria-label="Default select example">
                                               <option selected>Choisir Ici</option>
                                               @foreach($allPersonnes as $personne)
                                                <option value="{{$personne->id }}">{{$personne->nom }} - {{$personne->prenom }}</option>
                                              @endforeach
                                        </select>
                                    </div>
                                </div>
                            </div>
                            <div class="col-lg-12">
                                <div class="row mb-3">
                                    <label class="col-sm-2 col-form-label">CONTACT :</label>
                                    <div class="col-sm-4">
                                        <input type="text" name="contact" class="form-control">
                                    </div>

                                    <label class="col-sm-2 col-form-label">COMMUNE:</label>
                                    <div class="col-sm-4">
                                        <select class="form-select" name="ville_gare" aria-label="Default select example">
                                               <option selected>Choisir Ici</option>
                                               @foreach($allCommunes as $commune)
                                                <option value="{{$commune->id }}">{{ $commune->libelle }}</option>
                                              @endforeach
                                        </select>
                                    </div>
                                </div>
                            </div>
                            <div class="col-lg-12">
                                <div class="row mb-3">
                                    <label class="col-sm-2 col-form-label">LATITUDE :</label>
                                    <div class="col-sm-4">
                                        <input type="text" name="latitude" class="form-control">
                                    </div>

                                    <label class="col-sm-2 col-form-label">LONGITUDE :</label>
                                    <div class="col-sm-4">
                                        <input type="text" name="longitide" class="form-control">
                                    </div>
                                </div>
                            </div>
                            
                        </div>
                        <div class="row col-lg-12">
                            <div class="col-lg-4">
                            </div>
                            <div class="col-lg-4">
                                <div class="row mb-3">
                                    <div class="col-sm-10">
                                        <button type="submit" class="btn btn-danger">ENREGISTRER</button>
                                    </div>
                                </div>
                            </div>
                            <div class="col-lg-4">
                                <div class="row mb-3">
                                    <div class="col-sm-10">
                                        <button type="submit" class="btn btn-warning">ANNULER</button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </form>
                    <!-- End General Form Elements -->
                </div>
            </div>
        </div>

        <div class="col-lg-12">
            <div class="card">
                <div class="card-body">
                    <h5 class="card-title">LISTE DES GARES</h5>
                    <table class="table datatable">
                        <thead>
                            <tr>
                                <th scope="col">#</th>
                                <th scope="col">NOM GARE</th>
                                <th scope="col">RESPONSABLE </th>
                                <th scope="col">CONTACT </th>
                                <th scope="col">COMMUNE </th>
                                <th scope="col">LATITUDE </th>
                                <th scope="col">LONGITUDE </th>
                                <th scope="col">ACTION</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach($allgares as $gare)
                            <tr>
                                <th scope="row"> </th>
                                <td>{{ $gare->nom_gare }}</td>
                                <td>{{ $gare->personne->nom }} - {{ $gare->personne->prenom }}</td>
                                <td>{{ $gare->contact }}</td>
                                <td>{{ $gare->commune->libelle }}</td>
                                <td>{{ $gare->latitude }}</td>
                                <td>{{ $gare->longitide }}</td>
                                <td>
                                    <div class="dropdown">
                                        <button class="btn btn-danger dropdown-toggle btn-sm" type="button" id="dropdownMenuButton1" data-bs-toggle="dropdown" aria-expanded="false">
                                            Actions
                                        </button>
                                        <ul class="dropdown-menu" aria-labelledby="dropdownMenuButton1">
                                            <li>
                                                <a class="dropdown-item" href="#" data-bs-toggle="modal" data-bs-target="#ajoutligne_{{ $gare->id }}">Ajouter Ligne</a>
                                            </li>
                                            <li>
                                                <a class="dropdown-item" href="#" data-bs-toggle="modal" data-bs-target="#editegare_{{ $gare->id }}">Modifier</a>
                                            </li>
                                            <li>
                                                <a class="dropdown-item" href="#" data-bs-toggle="modal" data-bs-target="#listligne_{{ $gare->id }}">Liste Lignes</a>
                                            </li>
                                        </ul>
                                    </div>

                                    {{-- Modal POUR EDITER --}}
                                    <form action="{{route('editegare')}}" method="POST">
                                        @csrf
                                        {{--@method('PUT')--}}
                                        <div class="modal fade" id="editegare_{{ $gare->id }}" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
                                            <div class="modal-dialog">
                                                <div class="modal-content">
                                                    <div class="modal-header">
                                                        <h5 class="modal-title" id="exampleModalLabel">Editer</h5>
                                                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                                                    </div>
                                                    <div class="modal-body">
                                                        <input type="hidden" value="{{ $gare->id }}" name="id">
                                                        Voulez vous vraiment modifier la gare : <span class="fw-bold fst-italic">{{ $gare->nom_gare }}</span> ?
                                                        <div class="row mb-3">
                                                            <div class="col-sm-8">
                                                                </br>
                                                            </div>
                                                        </div>

                                                        <div class="row mb-3">
                                                            <label class="col-sm-4 col-form-label">NOM :</label>
                                                            <div class="col-sm-8">
                                                                <input type="text" value="{{ $gare->nom_gare }}" name="nom_gare" class="form-control">
                                                            </div>
                                                        </div>

                                                        <div class="row mb-3">
                                                            <label class="col-sm-4 col-form-label">CONTACT :</label>
                                                            <div class="col-sm-8">
                                                                <input type="text" value="{{ $gare->contact }}" name="contact" class="form-control">
                                                            </div>
                                                        </div>

                                                        <div class="row mb-3">
                                                            <label class="col-sm-4 col-form-label">COMMUNE :</label>
                                                            <div class="col-sm-8">
                                                                <select class="form-select" name="ville" aria-label="Default select example"  readonly = true>
                                                                    <option value="{{$commune->id }}" selected set filter true>{{ $gare->commune->libelle}} </option>
                                                                    @foreach($allCommunes as $commune)
                                                                        <option value="{{$commune->id }}">{{ $commune->libelle }}</option>
                                                                    @endforeach
                                                                </select>
                                                            </div>
                                                        </div>

                                                        <div class="row mb-3">
                                                            <label class="col-sm-4 col-form-label">RESPONSABLE :</label>
                                                            <div class="col-sm-8">
                                                                <select class="form-select" name="responsable" aria-label="Default select example"  readonly = true>
                                                                    <option value="{{$personne->id }}" selected>{{ $gare->personne->nom}} {{ $gare->personne->prenom }}</option>
                                                                    @foreach($allPersonnes as $personne)
                                                                        <option value="{{$personne->id }}">{{ $personne->nom }} {{ $personne->prenom }}</option>
                                                                    @endforeach
                                                                </select>
                                                            </div>
                                                        </div>

                                                        <div class="row mb-3">
                                                            <label class="col-sm-4 col-form-label">LATITUDE :</label>
                                                            <div class="col-sm-8">
                                                                <input type="text" value="{{ $gare->latitude }}" name="latitude" class="form-control">
                                                            </div>
                                                        </div>

                                                        <div class="row mb-3">
                                                            <label class="col-sm-4 col-form-label">LONGITUDE :</label>
                                                            <div class="col-sm-8">
                                                                <input type="text" value="{{ $gare->longitide }}" name="longitide" class="form-control">
                                                            </div>
                                                        </div>

                                                    </div>
                                                    <div class="modal-footer">
                                                        <button type="submit" class="btn btn-primary">VALIDER</button>
                                                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">QUITTER</button>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </form>
                                    {{-- Modal POUR EDITER--}}

                                    {{-- Modal POUR AJOUT LIGNE --}}
                                    <form action="{{route('ajoutligne')}}" method="POST">
                                        @csrf
                                        {{--@method('PUT')--}}
                                        <div class="modal fade" id="ajoutligne_{{ $gare->id }}" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
                                            <div class="modal-dialog">
                                                <div class="modal-content">
                                                    <div class="modal-header">
                                                        <h5 class="modal-title" id="exampleModalLabel">Ajout de Ligne</h5>
                                                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                                                    </div>
                                                    <div class="modal-body">
                                                        <input type="hidden" value="{{ $gare->id }}" name="id">
                                                        FORMULAIRE D'AJOUT DE LIGNE DE A LA GARE : <span class="fw-bold fst-italic">{{ $gare->nom_gare }}</span> ?
                                                        <div class="row mb-3">
                                                            <div class="col-sm-8">
                                                                </br>
                                                            </div>
                                                        </div>

                                                        <div class="row mb-3">
                                                            <label class="col-sm-4 col-form-label">NOM LIGNE :</label>
                                                            <div class="col-sm-8">
                                                                <input type="text" name="nom_ligne" class="form-control">
                                                            </div>
                                                        </div>

                                                        <div class="row mb-3">
                                                            <label class="col-sm-4 col-form-label">VILLE DEPART :</label>
                                                            <div class="col-sm-8">
                                                                <input type="text" value="{{$gare->commune->libelle }}" name="ville_deb" class="form-control" readonly = true>
                                                            </div>
                                                        </div>

                                                        <div class="row mb-3">
                                                            <label class="col-sm-4 col-form-label">QUART. DEPART :</label>
                                                            <div class="col-sm-8">
                                                                <input type="text" name="quartier_deb" class="form-control">
                                                            </div>
                                                        </div>

                                                        <div class="row mb-3">
                                                            <label class="col-sm-4 col-form-label">VILLE ARRIVEE :</label>
                                                            <div class="col-sm-8">
                                                                <select class="form-select" name="ville_fin" aria-label="Default select example"  readonly = true>
                                                                    <option selected>Choisir Ici</option>
                                                                    @foreach($allCommunes as $commune)
                                                                        <option value="{{$commune->libelle }}">{{ $commune->libelle }}</option>
                                                                    @endforeach
                                                                </select>
                                                            </div>
                                                        </div>

                                                        <div class="row mb-3">
                                                            <label class="col-sm-4 col-form-label">QUART. ARRIVE :</label>
                                                            <div class="col-sm-8">
                                                                <input type="text"  name="quartier_arr" class="form-control">
                                                            </div>
                                                        </div>

                                                        <div class="row mb-3">
                                                            <label class="col-sm-4 col-form-label"> RESPONSABLE :</label>
                                                            <div class="col-sm-8">
                                                                <select class="form-select" name="personne_id" aria-label="Default select example">
                                                                    <option selected>Choisir Ici</option>
                                                                    @foreach($allPersonnes as $personne)
                                                                        <option value="{{$personne->id }}">{{ $personne->nom }} - {{ $personne->prenom }}</option>
                                                                    @endforeach
                                                                </select>
                                                            </div>
                                                        </div>

                                                        <div class="row mb-3">
                                                            <label class="col-sm-4 col-form-label">COÛT :</label>
                                                            <div class="col-sm-8">
                                                                <input type="text" name="distance" class="form-control">
                                                            </div>
                                                        </div>
                                                    </div>
                                                    <div class="modal-footer">
                                                        <button type="submit" class="btn btn-primary">VALIDER</button>
                                                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">QUITTER</button>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </form>
                                    {{-- Modal POUR EDITER--}}

                                    {{-- Modal POUR LISTE --}}
                                    <form action="{{route('listligne')}}" method="POST">
                                        @csrf
                                        <div class="modal fade" id="listligne_{{ $gare->id }}" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
                                            <div class="modal-dialog larg">
                                                <div class="modal-content">
                                                    <div class="modal-header">
                                                        <h5 class="modal-title" id="exampleModalLabel">AFFICHAGE</h5>
                                                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                                                    </div>
                                                    <div class="modal-body">
                                                        <input type="hidden" value="{{ $gare->id }}" name="id">
                                                        
                                                        <div class="col-lg-12">
                                                            <div class="card">
                                                                <div class="card-body">
                                                                    <h5 class="card-title">LISTE DES LIGNES RATTACHEES A LA GARE </h5>
                                                                    <table class="table datatable">
                                                                        <thead>
                                                                            <tr>
                                                                                <th scope="col">NOM LIGNE</th>
                                                                                <th scope="col">LIEU DEP. </th>
                                                                                <th scope="col">LIEU ARR. </th>
                                                                                <th scope="col">DISTANCE </th>
                                                                            </tr>
                                                                        </thead>
                                                                        <tbody>
                                                                            @foreach($gare->lignes as $ligne)
                                                                            <tr>
                                                                                <td>{{ $ligne->nom_ligne }}</td>
                                                                                <td>{{ $ligne->ville_deb }}</td>
                                                                                <td>{{ $ligne->ville_fin }}</td>
                                                                                <td>{{ $ligne->distance }}</td>
                                                                            </tr>
                                                                            @endforeach
                                                                        </tbody>
                                                                    </table>
                                                                </div>
                                                            </div>
                                                        </div>
                                                        
                                                    </div>
                                                    <div class="modal-footer">
                                                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">QUITTER</button>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </form>
                                    {{-- Modal POUR LISTE--}}
                                </td>
                            </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

    </div>
</section>
@endsection
