  <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>Plantilla de Consumo </h3>
            </div>
            <div class="card-body">
               <!-- COMIENZO DEL FORMULARIO -->
               <form id="createplantilla" action="/links/edit-plantillas" 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">Descripción</label> 
                      <div class="input-group">
                        <input class="form-control" type="text" placeholder="Descripcion" id="desarea"   name="descripcion"  value = "{{lista.1.newParam.descripcion}}"  required>                     
                      </div>
                    </div>
                                        

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

                      <div class="input-group">
                        <input type="hidden"  class="form-control"  name="Id_reg" value = "{{lista.1.newParam.id_plantilla}}" > 
                        <input type="hidden"   class="form-control"  name="tipo_de_submit"  value=0 >
                        <input type="hidden"  class="form-control"   name="unidad"  value = "" > 
                        <input type="hidden"  class="form-control"   name="tabladeeliminados"  value = "" > 
                        <input type="hidden"   class="form-control"   name="tablamateriaprima"  value = "" > 
                      </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"  name="idmateriaprima"     onkeypress="return false;">
                                <div class="input-group-append">
                                <button href="#" class="btn btn-secondary"  id= "btn-mpct" 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="des-materiaprimaid" name="des-materiaprima" > 
                                
                            </div>
                            </div>


                            <div class="col-md-2 mb-3">
                            <label class="font-weight-bold" for="descliente">Cantidad</label> 
                            <div class="input-group">
                                <input type="number"  style="text-align:right;" class="form-control" placeholder="Cantidad" name="cantidad1">                         
                            </div>
                            </div>


                            <div class="col-md-2 mb-3">
                            <label class="font-weight-bold" for="descliente">Accion</label> 
                            <div class="input-group">
                                 <button type="button" id="btnCrear" class="btn btn-outline-primary   mt-8"    onclick="addmateriaprima(0)"><i class='fas fa-plus-circle'  ></i> </button> 
                            </div>
                            </div>


                  </div>  <!-- FIN DE LABELS E INPUTS DEL LA segunda FILA  -->
               
                 <div class="form-row">
                    <div style="height: 300px;overflow: scroll;"> 
                      <div class="col">
                         <table id="tabla123" class="table table-hover"   class="table table-responsive" class="table table-fit" >
                              <thead>
                                 <tr>
                                    <th >ID</th>
                                    <th   width="40%" >Materia Prima</th>
                                    <th   width="10%" >Unidad</th>
                                    <th   width="20%" >Cantidad</th>
                                    <th  class='text-center'  width="10%">Acciones</th>
                                 </tr>
                              </thead>                                                               
                              <tbody>
                                    {{#each lista}}
                                    {{#each lista_materiaprima}}
                                    <tr>
                                       <td >{{id_materiaprima}} </td>
                                       <td>{{descripcion}} </td>
                                       <td>{{unidad}} </td>
                                       <td> <input type="number"  style="text-align:right;" class="form-control" placeholder="cantidad" value = "{{cantidad}}" name="cantidad"      ></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 class="form-row"> <!-- COMIENZO DE LA QUINTA FILA linea ROW-->
                    <div class="col-md-11 mb-3">
                      <label class="font-weight-bold" for="observaciones">Observaciones Generales</label>
                      <div class="input-group">
                        <textarea class="form-control" id="idobservaciones" rows="4" name="observaciones">{{lista.1.newParam.observaciones}}</textarea>
                      </div>
                     </div>
                  </div>  <!-- FIN DE la CUARTA linea ROW-->

                  <div class="form-row">
                        <div  style="display:none">
                        <table id="tabledeleted">
                              <thead>
                                 <tr>
                                    <th>ID</th>
                                 </tr>
                              </thead>   
                              <tbody>

                              </tbody>    
                        </table>
                        </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 addmateriaprima(value) {
   event.preventDefault();
   event.stopPropagation();
   var id_materiaprima, descripcion, cantidad, unidad,trHTML;
   id_materiaprima = document.getElementsByName('idmateriaprima')[0].value ; 
   descripcion = document.getElementsByName('des-materiaprima')[0].value ; 
   cantidad = parseFloat('0' + document.getElementsByName('cantidad1')[0].value) ;
   unidad = document.getElementsByName('unidad')[0].value ; 

   if (id_materiaprima.length > 0) {
        trHTML = '<tr> <td width="5%">' + id_materiaprima + '</td>' + '<td width="45%">' + descripcion +  '</td><td width="10%">'   + unidad  +  '</td> <td> <input type="number"  style="text-align:right;" class="form-control" placeholder="" value = "' + cantidad + '" name="cantidad"></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('cantidad1')[0].value = 0 ; 
   }
}

//**********************************************
function addeliminados1(id) {
   var id_materiaprima, trHTML;
   id_materiaprima = id; 
   trHTML = '<tr> <td>' + id_materiaprima + '</td> </tr>';
   $('#tabledeleted').append(trHTML);
}


//*********************
function mostrarcatalogo() {

         var url1 = "/links/listar-materiaprima9";
         var table9 = $('#tablaproductos').DataTable();

            table9.clear()
            table9.draw();

            $.ajax({
                type: "GET",
                url: url1,
                datatype: "json",
                traditional: true
            })
            .done(function (data) {
                  var groupColumn = 0;        
                  var table = $('#tablaproductos').DataTable({
                      data:  data,
                      destroy: true,
              "columns":[
               { "data": "id", "title": "ID" },
               { "data": "codigo", "title": "Código" },
               { "data": "descripcion", "title": "Descripción" },
               { "data": "unidad", "title": "Unidad" },
               { "defaultContent": "<div class='text-center'> <div class='btn-group'> <button class='btn btn-info btn-sm btnAceptarP'>Seleccionar</button> </div> </div>" }
             ],
             pageLength : 10,
             lengthMenu: [[5, 10, 20, -1], [5, 10, 20, 'Todos']],
            "scrollY": 400,
            "scrollX": true,
            "order": [[ groupColumn, 'asc' ]],
            "displayLength": 25
    }); // fin de datatable declaracion     
            $('#modalPlanes').on('shown.bs.modal', function (e) {
            $('#tablaproductos').DataTable().draw(true);
            })          

            });

}

//****************
$(document).ready(function() { 

 //***********************************

$('form #btn-mpct').click(function(e) {
    mostrarcatalogo();    
 });
 

$(document).on("click", ".delete", function(){
      var fila = $(this).closest("tr");
      var id_mp = parseInt(fila.find('td:eq(0)').text());
      addeliminados1(id_mp) ;
      $(this).parents("tr").remove();
		$(".add-new").removeAttr("disabled");
});


});

//****************

</script>


<script>

   // Handle form submission event
function anexartabla(val) {
      var TableData;
          TableData = storeTblValues()
          TableData = JSON.stringify(TableData);
      var json =  TableData;
      document.getElementsByName('tablamateriaprima')[0].value = json;
      anexartablaEliminados();
      $("#createplantilla").submit();
}; 

   $('#createplantilla').on('submit', function(e){
      var TableData;
          TableData = storeTblValues()
          TableData = JSON.stringify(TableData);
      var json =  TableData;
      document.getElementsByName('tablamateriaprima')[0].value = json;
      anexartablaEliminados();
   });
//**************************************************************************

function anexartablaEliminados() {
      var TableData;
          TableData = storeTblValuesEliminados()
          TableData = JSON.stringify(TableData);
      var json =  TableData;
      document.getElementsByName('tabladeeliminados')[0].value = json;
};

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

               });

         });

        for(var r = 0; r < table2.rows.length; r++) {
          if (r < (table2.rows.length) - 1) {
            TableData.push({
              "Id_materiaprima" : IdsMp[r]
            , "cantidad" : document.getElementsByName('cantidad')[r].value
                })                
          }          
        }
    return TableData;
}
//*******************************************
function storeTblValuesEliminados()
{
    var TableData = new Array();
    var IdsMp = new Array();
         var table2 = document.getElementById('tabledeleted');
         $("#tabledeleted tbody tr").each(function (index) {
            var campo1 = "";
               $(this).children("td").each(function (index2) {
                  switch (index2) {
                     case 0:
                        campo1 = $(this).text().trim();
                        IdsMp.push(campo1)
                        break;
                  }

               });

         });

        for(var r = 0; r < table2.rows.length; r++) {
          if (r < (table2.rows.length) - 1) {
            TableData.push({
              "Id_materiaprimaD" : IdsMp[r]
               })      
          }          
        }
    return TableData;
}

//******************************************

</script>



{{>catalogomateria-prima2 }}