//$(document).ready(function (){
//  document.formSuspen.numCods.value =  document.thisform.inputvCod;
//});

function buscarPrestamo(b, a){
  const parametros = new FormData();
  parametros.append("numCod", document.formPrestamo.numCod.value);

  $.ajax({
    data:  parametros,
    url:   "busPrestamo/data.php",
    type:  "post",
    contentType: false,
    processData: false,
    beforeSend: function () {
      alLoad("Consultando...")
    },
    success: function (jsonPhp) {
      console.log(jsonPhp);
      const jsonJS = JSON.parse(jsonPhp);

      tblClear("tblPrestamo");
      jsonJS.pres_mae.forEach(function(i){
        addRowPrestamo(i.pm_cod, i.tp_cod, i.tp_nom, i.pm_desde, i.pm_hasta, i.pm_monto, i.pm_cuotam);
      });

      $("#tblPrestamo tbody tr").dblclick(function() {
        selectPrestamo(a, $(this));
      });

      $("#tblPrestamo tbody #btnSelecPrestamo").click(function() {
        selectPrestamo(a, $(this).parents("tr"));
      });

      alClose();
    }
  });
}

function addRowPrestamo(cod, tipopre, tipon, del, al, monto, cuotam) {
  const markup =  "<tr>"
                    +"<td id='datacod' class='text-center'>"+cod+"</td>"
                    +"<td id='datatipopre' class='text-left d-none'>"+tipopre+"</td>"
                    +"<td id='datatipon' class='text-left'>"+tipon+"</td>"
                    +"<td id='datadel' class='text-center'>"+del+"</td>"
                    +"<td id='dataal' class='text-center'>"+al+"</td>"                    
                    +"<td id='datamonto' class='text-right'>"+monto+"</td>"                    
                    +"<td id='datacuotam' class='text-right'>"+cuotam+"</td>"                    
                    +"<td><button class='btn btn-sm btn-warning badge' id='btnSelecPrestamo'>"+textBtnSelec+"</button></td>"
                  +"</tr>";
  $("#tblPrestamo tbody").append(markup);
}

function selectPrestamo(a, tr) {
  $("#pills-"+a+"-tab").click();
  /*if (inputProvcp == "txtNit") {
    document.thisform.txtNit.value = tr.find("td:eq(0)").text();
    $("form[name='thisform'] #txtNit").trigger("change");
  } else {*/
    $("#"+inputEmple).val(tr.find("td:eq(0)").text());
    $("#"+inputTipopre).val(tr.find("td:eq(1)").text());
    $("#"+inputDel).val(tr.find("td:eq(3)").text());
    $("#"+inputAl).val(tr.find("td:eq(4)").text());
    //if (!nomTF) {
    //  $("#"+inputEmple).trigger("change");
      //$("#"+inputAl).trigger("blur");
    //} else {
    //  $("#"+"txtNom1").val(tr.find("td:eq(1)").text());
    //}
  //}

  //$('html, body').animate( { scrollTop : 0 }, 500 );
  tblClear("tblPrestamo");
}