@extends('layouts.app')

@section('content')
    <div class="container" style="width: 100%; padding: 5%">
        <div class="row">
            @include('admin.sidebar')

            <div class="col-md-12">
                <div class="card">
                    <div class="card-body">
                        <div class="row">
                            <div class="col-md-3">
                                <a href="{{ url('/admin/jury/create') }}" class="btn btn-success btn-sm" title="Créer Jury">
                                    <i class="fa fa-plus" aria-hidden="true"></i> Créer
                                </a>
                                <a href="#" class="btn btn-info btn-sm" id="download_jurys">
                                    Télecharger <i class="fa fa-download"></i>
                                </a>
                            </div>
                            <div class="col-md-3">
                                <div class="input-group">
                                    <input type="text" class="form-control" id="jury-search" placeholder="Search...">
                                </div>
                            </div>
                            <div class="col-md-3">
                                <div class="form-group">
                                    <select class="form-control" id="departement_filter"  required>
                                        <option value="all"> -- Tous les departements -- </option>
                                        @foreach ($departements as $departement)
                                            <option value="{{ $departement->idDepartement }}">
                                                {{ $departement->sigledept }} ({{ $departement->desrdept }})
                                            </option>
                                        @endforeach
                                    </select>
                                </div>
                            </div>
                            <div class="col-md-3">
                                <div class="form-group">
                                    <select class="form-control" id="filiere_filter"  required>
                                        <option departement="all" value="all"> -- Toutes les filieres -- </option>
                                        @foreach ($filieres as $filiere)
                                            <option
                                                value="{{ $filiere->idFiliere }}"
                                                departement="{{ $filiere->idDepartement }}"
                                            >
                                            {{ $filiere->intituleFilieres }} ({{ $filiere->intituleFilieres }})
                                            </option>
                                        @endforeach
                                    </select>
                                </div>
                            </div>
                        </div>
                        <br/>
                        <br/>
                        <div class="table-responsive">
                            <table class="table" >
                                <thead>
                                    <tr id="head_tr">
                                        <th>#</th>
                                        <th>Filiere</th>
                                        <th>President</th>
                                        <th>Maitre memoire</th>
                                        <th>Membres</th>
                                        <th>Etudiants</th>
                                        <th>Sujet</th>
                                        <th>Salle</th>
                                        <th>Date</th>
                                        <th>Heure de debut</th>
                                        <th>Actions</th>
                                    </tr>
                                </thead>
                                <tbody id="jury-table-body">
                                @foreach($jury as $item)
                                    <tr
                                        class="jury_tr"
                                        id_jury="{{ $item->id }}"
                                        id_departement="{{ $item->idDepartement }}"
                                        id_filiere="{{ $item->id_filiere }}"
                                    >
                                        <td>{{ $loop->iteration }}</td>
                                        <td>{{ $item->filiere }}</td>
                                        <td>{{ $item->president_nom }} {{ $item->president_prenom }}</td>
                                        <td>{{ $item->maitre_memoire_nom }} {{ $item->maitre_memoire_prenom }}</td>
                                        <td> 
                                            <ul>
                                            @foreach($item->membres as $membre)
                                                <li> {{ $membre->nom }} {{ $membre->prenoms }} </li>
                                            @endforeach
                                            </ul>
                                         </td>
                                        <td>
                                            <ul>
                                                @foreach($item->etudiants as $etudiant)
                                                    <li> {{ $etudiant->nom }} {{ $etudiant->prenom }} ({{$etudiant->matricule}}) </li>
                                                @endforeach
                                            </ul>
                                        </td>
                                        <td>{{ $item->sujet }}</td>
                                        <td>{{ $item->salle }}</td>
                                        <td>{{ $item->date }}</td>
                                        <td>{{ $item->heure_debut }}</td>
                                        <td>
                                            <a href="{{ url('/admin/jury/' . $item->id) }}" title="View Jury"><button class="btn btn-info btn-sm"><i class="fa fa-eye" aria-hidden="true"></i> Voir fiche </button></a>
                                            <a href="{{ url('/admin/jury/' . $item->id . '/edit') }}" title="Edit Jury"><button class="btn btn-primary btn-sm"><i class="fa fa-pencil-square-o" aria-hidden="true"></i> Modifier </button></a>

                                            <form method="POST" action="{{ url('/admin/jury' . '/' . $item->id) }}" accept-charset="UTF-8" style="display:inline">
                                                {{ method_field('DELETE') }}
                                                {{ csrf_field() }}
                                                <button type="submit" class="btn btn-danger btn-sm" title="Delete Jury" onclick="return confirm(&quot;Confirm delete?&quot;)"><i class="fa fa-trash-o" aria-hidden="true"></i> Supprimer </button>
                                            </form>
                                        </td>
                                    </tr>
                                @endforeach
                                </tbody>
                                <tbody id="jury-table-body-save" style="display: none">
                                </tbody>
                            </table>
                            <div class="pagination-wrapper"> {!! $jury->appends(['search' => Request::get('search')])->render() !!} </div>
                        </div>

                    </div>
                </div>
            </div>
        </div>
    </div>
<script src="{{ asset('ample-admin-new/plugins/bower_components/jquery/dist/jquery.min.js')}}"></script>
<script type="text/javascript">
    $('#jury-table-body-save').html(
        $('#jury-table-body').html()
    )
    $('#jury-search').bind('input', function() {
        var jury_table_body_clone = $('#jury-table-body-save').clone()
        var input_value = $(this).val().toLowerCase()
        if( input_value== "" ) $('tr').removeAttr('style') 
        else {
            jury_table_body_clone.find('tr').each(function() {
                var content =  $(this).text().replace(/\n/gi, "").toLowerCase()
                if( !content.includes(input_value) ) {
                    $(this).remove()
                }
            })
        }
        $('#jury-table-body').html(jury_table_body_clone.html())
        jury_table_body_clone.remove();
    })
    $('#download_jurys').click(function() {
        $('#jury-table-body tr').each(function() {
            if ($(this).css('display') != 'none') {
                var host = "{{ url('') }}";
                var url = "/download_jury/" + $(this).attr('id_jury')+"/jury"+ $(this).attr('id_jury')
                console.log('host :', host, ',  url:', url)
                window.open(host  + url, "_blank");
            }
        })
    })
    $('#departement_filter').change(function() {
        // filter filier select
        var departement_id = $(this).val();
        $('#filiere_filter option').each(function() {
            if ( $(this).attr('departement') == departement_id || departement_id == "all") {
                $(this).css('display','block')
            } else {
                $(this).css('display','none')
            }
        })

        // filter filier jurys
        var jury_table_body_clone = $('#jury-table-body-save').clone()
        var input_value = $(this).val().toLowerCase()
        if( input_value== "" ) $('tr').removeAttr('style') 
        else {
            jury_table_body_clone.find('tr').each(function() {
                if( !($(this).attr('id_departement') == departement_id || departement_id == "all")) {
                    $(this).remove()
                }
            })
        }
        $('#jury-table-body').html(jury_table_body_clone.html())
        jury_table_body_clone.remove();

    })
    $('#filiere_filter').change(function() {
        var filiere_id = $(this).val();
        // filter filier jurys
        var jury_table_body_clone = $('#jury-table-body-save').clone()
        var input_value = $(this).val().toLowerCase()
        if( input_value== "" ) $('tr').removeAttr('style') 
        else {
            jury_table_body_clone.find('tr').each(function() {
                if( !($(this).attr('id_filiere') == filiere_id || filiere_id == "all")) {
                    $(this).remove()
                }
            })
        }
        $('#jury-table-body').html(jury_table_body_clone.html())
        jury_table_body_clone.remove();
    })

</script>
@endsection
