<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> Formulas  </h3>
            </div>
            <div class="card-body">
               <!-- COMIENZO DEL FORMULARIO -->
               <form id="editform" action="/links/edit-formulas" 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-2 mb-3">
                      <label class="font-weight-bold" for="desformula">Codigo</label> 
                      <div class="input-group">
                        <input class="form-control" type="text" placeholder="" id="id-area"   name="idcodigo"  value = "{{lista.1.newParam.codigo}}"  required >                     
                      </div>
                    </div>


                    <div class="col-md-5 mb-3">
                      <label class="font-weight-bold" for="desformula">Descripcion de la Formula</label> 
                      <div class="input-group">
                        <input class="form-control" type="text" placeholder="Descripcion del Area" id="desarea"   name="descripcion"  value = "{{lista.1.newParam.descripcion}}"  required>                     
                      </div>
                    </div>

                      <div class="input-group">
                        <input type="hidden"  style="text-align:right;" class="form-control" placeholder="" name="indextable"  value="0">                                               
                      </div>

                      <div class="input-group">
                        <input type="hidden"  style="text-align:right;" class="form-control"  name="inputtabladetalle" >                         
                        <input type="hidden"  style="text-align:right;" class="form-control"  name="tipo_de_submit"  value=0 >
                        <input type="hidden"   class="form-control" placeholder=""   name="Id_registro"  value = {{lista.1.newParam.idreg}} > 
                      </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">Materia Prima</label>
                            <div class="input-group">
                                <input type="text" class="form-control" placeholder="" name="idmateriaprima"     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">Descripcion</label> 
                            <div class="input-group">
                                <input class="form-control" type="text" placeholder=""  readonly id="desmaquinaid" name="des-materiaprima" > 
                                
                            </div>
                            </div>


                            <div class="col-md-2 mb-3">
                            <label class="font-weight-bold" for="descliente">Porcentaje</label> 
                            <div class="input-group">
                                <input type="number"  style="text-align:right;" class="form-control" placeholder="Porcentaje" name="porcentaje"   >                         
                            </div>
                            </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 Materia Prima" 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%">Descripcion</th>
                                                <th width="10%">Porcentaje</th>
                                                <th width="10%">Acciones</th>
                                             </tr>
                                          </thead>
                                          <tbody>
                                             {{#each lista}}
                                             {{#each lista_detalle}}
                                             <tr>
                                                <td>{{id_materiaprima}} </td>
                                                <td>{{descripcion}} </td>
                                                <td>{{porcentaje}} </td>
                                                <td class="text-center"> <a class="delete" title="Delete" data-toggle="tooltip"><i
                                                         class="fas fa-trash"></i></a></td>
                                             </tr>
                                             {{/each}}
                                             {{/each}}
                                          </tbody>
                                       </table>
                                    </div>
                                 </div>
                              </div>
                           </div>
                 </div>
                  <!-- FIN DE la TERCERA linea ROW-->

                  <div class="form-row"> <!-- COMIENZO DE LA QUINTA FILA linea ROW-->
                    <div class="col-md-11 mb-3">
                      <label class="font-weight-bold" for="observaciones">Usos</label>
                      <div class="input-group">
                        <textarea class="form-control" id="idobservaciones" rows="2" name="usos">{{lista.1.newParam.notas}}</textarea>
                      </div>
                     </div>
                  </div>  <!-- FIN DE la CUARTA 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 addmateriaprima(value) {
   var id_maquina, descripcion, trHTML;
   id_materiaprima = document.getElementsByName('idmateriaprima')[0].value ; 
   descripcion = document.getElementsByName('des-materiaprima')[0].value ; 
   porcentaje = parseInt('0' + document.getElementsByName('porcentaje')[0].value) ; 
   if (id_materiaprima.length > 0) {
        trHTML = '<tr> <td width="5%">' + id_materiaprima + '</td>' + '<td width="75%">' + descripcion +  '</td><td width="10%">'   + porcentaje  +  '</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('idmateriaprima')[0].value = '';
        document.getElementsByName('des-materiaprima')[0].value = ''; 
        document.getElementsByName('porcentaje')[0].value = 0 ; 
   }
}


</script>

<script>

$(document).ready(function() { 

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

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


});


</script>

<script>

   // Handle form submission event
   $('#editform').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', 'tablaMp')
                .val(json)
         );
   });


function storeTblValues()
{
    var TableData = new Array();
    var IdsMaq = new Array();
    var Porcs = 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();
                        IdsMaq.push(campo1)
                        break;
                     case 2:
                        campo2 = $(this).text();
                        Porcs.push(campo2)
                        break;

                  }                  
               });

         });

         for(var r = 0; r < table2.rows.length; r++) {
          if (r < (table2.rows.length) - 1) {
            TableData.push({
              "Id_MP" : IdsMaq[r]
            , "Porcentaje" : Porcs[r]
              })                
          }          
        }

    return TableData;
}

</script>

{{>catalogomateria-prima }}