@extends('usuarios.funcionarios.master.layout2')
@section('conteudo')



    
    <nav class="d-none">
        {{ $clientes=\App\Models\Cliente::orderBy('created_at','Asc')->get() }}
        {{ $camposMapa = \App\Models\CampoMapa::where('origem','Geral')->orderBy('prioridade','Asc')->get()}}
        {{ $camposMapaEspecificos = \App\Models\CampoMapa::where('origem','Específico')->orderBy('prioridade','Asc')->get()}}
        {{$camposEspecificos=1}}
    </nav>
    <div class="mb-5 p-4 bg-white shadow-sm text-center">
        <i class="fa fa-plus text-success"></i>
    <h3 class="text-center">Nova indexacão</h3>
    <div id="stepper2" class="bs-stepper">
      <div class="bs-stepper-header" role="tablist">
        <div class="step" data-target="#test-nl-1">
          <button type="button" class="step-trigger" role="tab" id="stepper2trigger1" >
            <span class="bs-stepper-circle">
              <span class="fas fa-user" aria-hidden="true"></span>
            </span>
            <span class="bs-stepper-label">Cliente</span>
          </button>
        </div>
       
        <div class="bs-stepper-line"></div>
        <div class="step" data-target="#test-nl-3">
          <button type="button" class="step-trigger" role="tab" id="stepper2trigger3" >
            <span class="bs-stepper-circle">
              <span class="fa fa-edit" aria-hidden="true"></span>
            </span>
            <span class="bs-stepper-label">Linhas</span>
          </button>
        </div>
      </div>
      <div class="bs-stepper-content">
        <form onSubmit="return false">
          <div id="test-nl-1" role="tabpanel" class="bs-stepper-pane" aria-labelledby="stepper2trigger1">
            <div class="form-group m-4 text-center">
              <label for="exampleInputName1">Cliente</label> <br>
                <select id="SectorCliente" class="position-relative form-control   bg-dark" style="width:300px;" >
                    <option value="0" class="d-none" style="display:none"> Escolha aqui um cliente </option>
                    @foreach($clientes as $cli)
                        <option value="{{$cli->id}}"> {{ $cli->getUser->getPessoa->nome}}</option>
                    @endforeach
                </select>
              <br><br>
             <button class="btn btn-success btn-sm bnIndexar" >Prosseguir <i class="fa fa-angle-right"></i></button>
            </div>
          </div>
          
          <div id="test-nl-3" role="tabpanel" class="bs-stepper-pane text-center" aria-labelledby="stepper2trigger3">
              
               <nav class="container">
                   <h4>Linhas de indexação</h4>
                   <nav class="table-responsive">
                       <table class="table table-hover table-primary table-outline-secondary">
                           <thead>
                               <tr class="tHeadyTr">
                                    <th>#</th>
                                    @foreach($camposMapa as $cMapa)
                                        @if($cMapa->descricao=="Código da recolha")
    
                                        @elseif($cMapa->descricao=="C. Q.")
                                        
                                        @else
                                        <th>{{$cMapa->descricao}}</th>
                                        @endif
                                    @endforeach
                                </tr>
                               
                           </thead>
                           <tbody class="tBody">
                               
                           </tbody>
                       </table>
                   </nav>
                   <br>
                   <hr>
                   <br>
                    <button type="submit" class="btn btn-primary btn-sm mt-5 geralBtn" data-bs-toggle="modal" data-bs-target="#staticBackdrop"><i class="fa fa-plus"></i></button>
                    <button type="submit" class="btn btn-warning btn-sm mt-5 geralBtn"><i class="fa fa-times"></i></button>
                    <button type="submit" class="btn btn-info btn-sm mt-5 geralBtn"><i class="fa fa-edit"></i></button>
               </nav>
            <button type="submit" class="btn btn-success btn-sm mt-5 geralBtn"><i class="fa fa-check"></i> Finalizar Indexação</button>
          </div>
        </form>
      </div>
    </div>
    </div>


<!-- Modal -->
<div class="modal fade" id="staticBackdrop" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
  <div class="modal-dialog modal-lg">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="staticBackdropLabel">Nova linha de indexação</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <form>
          
          <div class="modal-body container">
            <nav class="row p-3">
                <label>Campos gerais</label>
                <hr>
                @foreach($camposMapa as $cMapa)
                    @if($cMapa->descricao=="Código da recolha")

                    @elseif($cMapa->descricao=="C. Q.")
                    
                    @else
                    
                        @if($cMapa->chaveestrangeira=="Sim")
                            
                                @if($cMapa->nome=='funcionario_id')
                                
                                @else
                                    <nav class="col-3 p-2">
                                        <nav class="row p-2">
                                                    
                                            <label>{{$cMapa->descricao}}</label> 
                                            </nav>
                                             <nav class="row p-3">
                                                
                                            <select id="{{$cMapa->nome}}" class="form-select">
        
                                            </select>
                                     
                                        </nav> 
                                    </nav> 
                                @endif
                        @else
                       <nav class="col-3 p-2">
                            @if($cMapa->tipo=="integer")
                                    <nav class="row p-2">
                                        
                                        <label>{{$cMapa->descricao}}</label> 
                                    </nav>
                                    <nav class="row p-3">
                                            
                                        <input class="form-control" type="number">
                                     
                                    </nav>
                            @elseif($cMapa->tipo=="text")
                                    <nav class="row p-2">
                                        
                                        <label>{{$cMapa->descricao}}</label> 
                                    </nav>
                                    <nav class="row p-3">
                                            
                                       <textarea class="form-control" rows="1" value=" " placeholder="({{$cMapa->descricao}})">
                                           
                                       </textarea> 
                                     
                                    </nav>
                            @elseif($cMapa->tipo=="string" || $cMapa->tipo=="varchar")
                                    <nav class="row p-2">
                                        
                                        <label>{{$cMapa->descricao}}</label> 
                                    </nav>
                                    <nav class="row p-3">
                                            
                                        <input class="form-control" type="text">
                                     
                                    </nav>
                            @elseif($cMapa->tipo=="data")
                                    <nav class="row p-2">
                                        
                                        <label>{{$cMapa->descricao}}</label> 
                                    </nav>
                                    <nav class="row p-3">
                                            
                                        <input class="form-control" type="date">
                                     
                                    </nav>
                            @elseif($cMapa->tipo=="enum")
                             
                                    <nav class="row p-2">
                                        
                                    <label>{{$cMapa->descricao}}</label> 
                                    </nav>
                                     <nav class="row p-3">
                                        
                                    <select class="form-select">
                                        @if($cMapa->possivelvalore1!="")
                                            <option>{{$cMapa->possivelvalore1}}</option>
                                        @endif
                                        @if($cMapa->possivelvalore2!="")
                                            <option>{{$cMapa->possivelvalore2}}</option>
                                        @endif
                                        @if($cMapa->possivelvalore3!="")
                                            <option>{{$cMapa->possivelvalore3}}</option>
                                        @endif
                                    </select>
                                 
                                    </nav>
                            @endif
                        </nav>
                        @endif
                    @endif
                @endforeach
            </nav>
            <nav class="row camposDinamicos p-3">
                <label>Campos específicos</label>
                <hr>
                <nav class="campDinm">
                   
                    @foreach($camposMapaEspecificos as $campoMapaEspecifico)
                        <nav class="col-4 p-2 campo_especifico campo_especificoCount_{{$camposEspecificos++}} campo_especifico_{{$campoMapaEspecifico->cliente_id}}">
                             @if($campoMapaEspecifico->tipo=="integer")
                                    <nav class="row p-2">
                                        
                                        <label>{{$campoMapaEspecifico->descricao}}</label> 
                                    </nav>
                                    <nav class="row p-3">
                                            
                                        <input class="form-control" type="number">
                                     
                                    </nav>
                            @elseif($campoMapaEspecifico->tipo=="text")
                                    <nav class="row p-2">
                                        
                                        <label>{{$campoMapaEspecifico->descricao}}</label> 
                                    </nav>
                                    <nav class="row p-3">
                                            
                                       <textarea class="form-control" rows="1" value=" " placeholder="({{$campoMapaEspecifico->descricao}})">
                                           
                                       </textarea> 
                                     
                                    </nav>
                            @elseif($campoMapaEspecifico->tipo=="string" || $campoMapaEspecifico->tipo=="varchar")
                                    <nav class="row p-2">
                                        
                                        <label>{{$campoMapaEspecifico->descricao}}</label> 
                                    </nav>
                                    <nav class="row p-3">
                                            
                                        <input class="form-control" type="text">
                                     
                                    </nav>
                            @elseif($campoMapaEspecifico->tipo=="data")
                                    <nav class="row p-2">
                                        
                                        <label>{{$campoMapaEspecifico->descricao}}</label> 
                                    </nav>
                                    <nav class="row p-3">
                                            
                                        <input class="form-control" type="date">
                                     
                                    </nav>
                            @elseif($campoMapaEspecifico->tipo=="enum")
                             
                                    <nav class="row p-2">
                                        
                                    <label>{{$campoMapaEspecifico->descricao}}</label> 
                                    </nav>
                                    <nav class="row p-3">
                                        
                                    <select class="form-select">
                                        @if($campoMapaEspecifico->possivelvalore1!="")
                                            <option>{{$campoMapaEspecifico->possivelvalore1}}</option>
                                        @endif
                                        @if($campoMapaEspecifico->possivelvalore2!="")
                                            <option>{{$campoMapaEspecifico->possivelvalore2}}</option>
                                        @endif
                                        @if($campoMapaEspecifico->possivelvalore3!="")
                                            <option>{{$campoMapaEspecifico->possivelvalore3}}</option>
                                        @endif
                                    </select>
                                 
                                    </nav>
                            @endif
                        </nav>
                    @endforeach
                </nav>
            </nav>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-success btn-sm"><i class="fa fa-plus"></i> Adiccionar</button>
          </div>
      </form>
    </div>
  </div>
</div>
 
<script>
    $(function(){
         $(".bnIndexar").hide();
         $(".geralBtn").hide();
        $("textarea").text("");
         var stepper2
         stepper2 = new Stepper(document.querySelector('#stepper2'), {
            linear: false
          })
          
         
        
        var results= '8';
        @php $val = "<script>document.write(results)</script>" @endphp
        
        
        $("#SectorCliente").select2({});
        $("#SectorCliente").change(function(){
            $("#orgaoestrutura_id").empty();
            var val=$(this).val();
            
            
            if (val==0){
                $(".bnIndexar").hide();
                $(".geralBtn").hide();
            }else{
                $(".bnIndexar").show();
                $(".geralBtn").show();
                $.post( "{{ route('us.servext.indexacao.return.oes') }}",
                { 
                    '_token':  '{{ csrf_token()  }}',
                    'cliente_id':val
                },
                function(data){


                $("#orgaoestrutura_id").append(data);
                
                    //location.reload(true);
                
                }).fail(function(){
                    alert("Infelizmente falhou o acesso à esta função");
                });
                $.post( "{{ route('us.servext.indexacao.return.sds') }}",
                { 
                    '_token':  '{{ csrf_token()  }}',
                    'cliente_id':val
                },
                function(data){


                $("#seriedocumental_id").append(data);
                 
                
                }).fail(function(){
                    alert("Infelizmente falhou o acesso à esta função");
                });
                
                //$('.campDinm').empty();
                var camposEsp='{{$camposMapaEspecificos->count()}}';
                var camposEsp2="{{$camposMapaEspecificos2=\App\Models\CampoMapa::where('origem','Específico')->where('cliente_id',$val)->orderBy('prioridade','Asc')->count()}}";
                console.log(camposEsp2);
                for (i=1;i<=camposEsp;i++){
                    
                    
                    //Pegar todas as classes da referida nav
                    var classList = $('.campo_especificoCount_'+i).attr('class').split(/\s+/);
                    //Encontrar a última classe
                    var secondList=classList[4].split('_');
                    var cliente_id_encontrado=secondList[2];
                    if (cliente_id_encontrado===val){
                        
                    }
                    else{
                        $('.campo_especificoCount_'+i).remove();
                    }
                    /*$.each(classList, function(index, item) {
                        if (item === 'someClass') {
                            //do something
                        }
                    });

                    /*if($(').hasClass('campo_especifico'+val)){
                        
                    }else{
                        $('.campo_especificoCount_'+i).hide();
                    }*/
                }
            }
        });
        $(".bnIndexar").click(function (){
            stepper2.next();
            $(".geralBtn").show();
        });
    });
    
</script>

@endsection