@extends('template')



@section('contenu')
    <div class="navbar-fixed">
        <nav>
            <div class="nav-wrapper">
                <div class="col s12">
                    <a href="/privileges" class="breadcrumb">Gestion Privilege</a>
                
                <ul id="nav-mobile" class="right hide-on-med-and-down">
             <li><a href="/">Home</a></li>
           </ul> 
           </div>
            </div>
        </nav>
    </div>
    
    <div class="container">
       
       
            
           <!-- Medecin Observateur !-->
                <div id="MO" class="section scrollspy">
                    <br>
                    <br>
                    <h4 class="light-blue-text text-darken-1 center">Liste des Enseignant</h4>

                    <div class="input-field">
                        <input id="search" type="search" >
                        <label for="search"><i class="material-icons">search</i></label>
                    </div>
                    <table class="highlight">

                        <thead class="thead">
                        <tr>
                            <th data-field="cin">CIN</th>
                            <th data-field="nom">Nom</th>
                            <th data-field="prenom">Prenom</th>
                            <th></th>

                        </tr>
                        </thead>


                        <tbody class="tbody" id="med">

                        @foreach ($enseignants as $enseignant)
                           <tr id={!! $enseignant->id_Enseignant."css" !!} class="ens" >
                           
                                <td>{!! $enseignant->CIN !!}</td>
                                <td>{!! $enseignant->nom !!}</td>
                                <td>{!! $enseignant->prenom !!}</td>
                            
                            </tr>
                        @endforeach

                        </tbody>
                    </table>
                </div>
                

 <!-- Modal Structure -->
  <div id="modal1" class="modal modal-fixed-footer">
    <div class="modal-content">
      <h5 id="nom_enseignant"><!-- Nom Enseignant !-->Nom Prenom</h5>
       <form  method="post">
              {!! csrf_field() !!}
       <table class="highlight">

                        <thead class="thead">
                        <tr>
                          
                            <th data-field="nom">Privilege</th>
                            <th></th>

                        </tr>
                        </thead>


                        <tbody class="tbody" id="med">

                        @foreach ($privileges as $privilege)
                            <tr id={!! $privilege->id_Privilege.'t' !!} >
                                <td>{!! $privilege->nom !!}</td>
                               
                                <td>
                                    <input type="checkbox" value="yes"
                                           name={!! $privilege->id_Privilege."" !!} id={!! $privilege->id_Privilege."" !!} />
                                    <label for={!! $privilege->id_Privilege !!} >Check</label>
                                </td>
                            </tr>
                        @endforeach

                        </tbody>
                    </table>
                      <input type="submit" value="done" class="waves-effect waves-light btn" />
  </form>
    </div>
    <div class="modal-footer">
    </div>
  
   
          
                 
  
     </div>
    <script type="text/javascript">
        $(document).ready(function () {
            $('.scrollspy').scrollSpy();
        });
       
        $(document).ready(function () {
            $('.tooltipped').tooltip({delay: 50});
        });
        $(document).ready(function () {
            $('select').material_select();
        });
        $(document).ready(function() {
             <?php  echo "var enseignants = ".$enseignants."\n";?>
             <?php  echo "var tab_Privilege = ".$struct_Privilege."\n";?>
             <?php  echo "var privileges = ".$privileges."\n";?>
             $(".ens").click(function(event) {

                var id_Enseignant = $(this).attr('id');
                var id_Enseignant =id_Enseignant.substr(0,id_Enseignant.indexOf('css'))
                $('form').attr('action',"privileges/"+id_Enseignant+"/edit");
                $('#nom_enseignant').text(enseignants[parseInt(id_Enseignant)-1]['nom']+" "+enseignants[parseInt(id_Enseignant)-1]['prenom']);
                //Dans cette partie on parcourus le tableau en cochant chaque privilege de l'enseignant
                var tab = tab_Privilege[id_Enseignant];
                for(var i=0;i<privileges.length;i++){

                     $('#'+privileges[i]['id_Privilege']).prop('checked',false) ;
                }
                if(tab){
                    for(var i=0;i<tab.length;i++){
                         $('#'+tab[i]).prop('checked',true) ;
                    }
                }   
                  $('#modal1').openModal();
                
                 
                
            });
        });
 


    </script>
    <script type="text/javascript">

        <?php  echo "var enseignants = ".$enseignants."\n";?>
        var $_token = $('input[name=_token]').val();
        $(function () {
              function verif(id_Enseignant,data){
                for(i=0;i<data.length;i++){
                    if(id_Enseignant==data[i]['id_Enseignant']){
                        return true;
                    }
                }
                return false;
            }
            //A chaque changement du input search il va une requéte ajax 
            $('#search').on('input', function () {
                var rech = $('#search').val();
                $.ajax({
                    url: "privileges/show",
                    type: "post",
                    dataType : 'json' ,
                    data: {'_token': $_token, 'rech': rech},
                    success: function (data) {

                        for(var i=0;i<enseignants.length;i++){
                            
                            if(verif(enseignants[i]['id_Enseignant'],data)){
                                $('#'+enseignants[i]['id_Enseignant']+"css").css('display','table-row');
                            }else {
                                $('#'+enseignants[i]['id_Enseignant']+"css").css('display','none');
                            }
                        }
                   
                     
                    }
                });
            });

        });
    </script>

@stop

