<div class="container col d-flex justify-content-center">
   <div class="row">
      <div class="col-lg-12">
         <div class="card shadow-lg p-3 mb-5 bg-white ">
            <div class="card-header">
               <h3> Grupos de Trabajo </h3>
            </div>
            <div class="card-body">
               <!-- COMIENZO DEL FORMULARIO -->
               <form id="createform" action="/links/creategrupos" method="POST" class="needs-validation" novalidate>
                  <div class="form-row">
                     <!-- LABELS E INPUTS DEL LA PRIMERA FILA  -->

                     <!-- LABELS E INPUTS DE  LA FECHA -->

                    <div class="col-md-5 mb-3">
                      <label class="font-weight-bold" for="desformula">Descripcion del grupo</label> 
                      <div class="input-group">
                        <input class="form-control" type="text" placeholder="Descripcion del Grupo" id="desgrupo"   name="descripcion"  value = ""  required>                     
                      </div>
                    </div>

                      <div class="input-group">
                        <input type="hidden"   class="form-control" placeholder="" name="indextable"  value="0">                                               
                      </div>

                      <div class="input-group">
                        <input type="hidden"   class="form-control" placeholder="" name="inputtabladetalle" >                         
                        <input type="hidden"  class="form-control" placeholder="" name="tipo_de_submit"  value=0 >
                      </div>

                  </div> <!-- FIN DE LABELS E INPUTS DEL LA PRIMERA FILA  -->

                  <div class="form-row">
                            <div class="col-md-2 mb-3">
                            <label class="font-weight-bold" for="cliente">Trabajador</label>
                            <div class="input-group">
                                <input type="text" class="form-control" placeholder="" name="idtrabajador"     onkeypress="return false;">
                                <div class="input-group-append">
                                <button href="#" class="btn btn-secondary"  data-toggle="modal" data-target="#modalPlanes"  type="button">     <i class="fa fa-search"></i> 
                                </button>
                                </div>  
                            </div>
                            </div>

                            <div class="col-md-3 mb-3">
                            <label class="font-weight-bold" for="descliente">Nombres</label> 
                            <div class="input-group">
                                <input class="form-control" type="text" placeholder=""  readonly id="idnombres" name="nombres" > 
                                
                            </div>
                            </div>


                     <div class="col-md-2 mb-3">
                        <label class="font-weight-bold" for="turno">Rol</label>
                        <select name="rol" class="custom-select" >
                           <option value="Ayudante"></option>
                           <option value="Ayudante">Ayudante</option>
                           <option value="Operador">Operador</option>
                        </select>
                     </div>


                            <div class="col-md-2 mb-3">
                            <label class="font-weight-bold" for="descliente">Accion</label> 
                            <div class="input-group">
                                <input type="button" id="btn-ok" value="Agregar Trabajador" name="agregar"     class="btn btn-primary accept"/>
                            </div>
                            </div>



                  </div>  <!-- FIN DE LABELS E INPUTS DEL LA segunda FILA  -->
               
                 <div class="form-row">

       <div class="row">
<div style="height: 200px;overflow: scroll;">
            <div class="col-lg-12 mx-auto bg-white rounded shadow">                    

                      <div class="col">
                         <table id="tabla123" class="table table-hover"      class="table-responsive"  class="table table-fit" >
                              <thead>
                                 <tr>
                                    <th width="5%" >ID</th>
                                    <th width="75%">Nombres</th>
                                    <th width="10%">Rol</th>
                                    <th width="10%">Acciones</th>
                                 </tr>
                              </thead>
                              <tbody  >

                              </tbody>
                        </table>


                </div>
             </div>               
 </div>

                    </div>

                 </div>
                  <!-- FIN DE la TERCERA linea ROW-->

                  <div class="form-row">
                     <!-- COMIENZO DE la CUARTA linea ROW-->
                            <div class="col-md-8 mb-3">
                              <div class="input-group">                               

                              </div>
                            </div>
                            <div class="col-md-2 mb-3">
                              <div class="input-group">                               
                               <button class="btn btn-success"  onclick= "cambiarstatus(0)"  type="submit">Save</button>
                              </div>
                            </div>
                            <div class="col-md-2 mb-3">
                              <div class="input-group">                               
                                <button id="btnCrear"    class="btn btn-danger" onclick= "cambiarstatus(1)"  >Close</button>
                              </div>
                            </div>
                  </div>


               </form>
            </div>
         </div>
      </div>
   </div>
</div>

<script src="/jquery/dist/jquery.js"></script>

<script>
   function cambiarstatus(valor) {
      document.getElementsByName('tipo_de_submit')[0].value = valor;
   }
</script>

<script>

   (function () {
      'use strict';
      window.addEventListener('load', function () {
         // Fetch all the forms we want to apply custom Bootstrap validation styles to
         var forms = document.getElementsByClassName('needs-validation');
         // Loop over them and prevent submission
         var validation = Array.prototype.filter.call(forms, function (form) {
            form.addEventListener('submit', function (event) {
               if (form.checkValidity() === false) {
                  if (parseInt(document.getElementsByName('tipo_de_submit')[0].value) <= 0) {
                      event.preventDefault();
                      event.stopPropagation();
                  }
               }
               form.classList.add('was-validated');
            }, false);
         });
      }, false);
   })();


</script>

<script>

function addtrabajador(value) {
   var id_trabajador, nombres, rol, trHTML;
   id_trabajador = document.getElementsByName('idtrabajador')[0].value ; 
   nombres = document.getElementsByName('nombres')[0].value ; 
   rol = document.getElementsByName('rol')[0].value ; 
   if (id_trabajador.length > 0) {
        trHTML = '<tr> <td width="5%">' + id_trabajador + '</td>' + '<td width="75%">' + nombres +  '</td><td width="10%">'   + rol.trim()  +  '</td> <td class="text-center"> <a class="delete" title="Delete" data-toggle="tooltip"><i class="fas fa-trash"></i></a></td></tr> ';
        $('#tabla123').append(trHTML);
        document.getElementsByName('idtrabajador')[0].value = '';
        document.getElementsByName('nombres')[0].value = ''; 
   }
}


</script>

<script>

$(document).ready(function() { 

 $('form #btn-ok').click(function(e) {
        addtrabajador(0);     
 });

$(document).on("click", ".delete", function(){
        $(this).parents("tr").remove();
		$(".add-new").removeAttr("disabled");
   });


});


</script>

<script>

   // Handle form submission event
   $('#createform').on('submit', function(e){
      var form = this;
      
      var TableData;
         TableData = storeTblValues()
         TableData = JSON.stringify(TableData);
         var json =  TableData;

      // Create a hidden element
       $(form).append(
             $('<input>')
                .attr('type', 'hidden')
                .attr('name', 'tablaTrabajadores')
                .val(json)
         );
   });


function storeTblValues()
{
    var TableData = new Array();
    var IdsTrab = new Array();
    var roles = new Array();
         var table2 = document.getElementById('tabla123');
         $("#tabla123 tbody tr").each(function (index) {
            var campo1 = "";
            var campo2 = "";
               $(this).children("td").each(function (index2) {
                  switch (index2) {
                     case 0:
                        campo1 = $(this).text();
                        IdsTrab.push(campo1)
                        break;
                     case 2:
                        campo2 = $(this).text();
                        roles.push(campo2)
                        break;

                  }
                  

               });

         });

         for(var r = 0; r < table2.rows.length; r++) {
          if (r < (table2.rows.length) - 1) {
            TableData.push({
              "Id_trabajador" : IdsTrab[r]
            , "rol" : roles[r].trim()
              })                
          }          
        }

    return TableData;
}

</script>

{{>catalagotrabajadores }}