$(document).ready(function (){
  document.formPlani.dtFecIni.value = dateToday();
  document.formPlani.dtFecFin.value = dateToday();
});
function buscarPlani(b, a){
  const parametros = new FormData();
  parametros.append("dtFecIni", document.formPlani.dtFecIni.value);
  parametros.append("dtFecFin", document.formPlani.dtFecFin.value);

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

      tblClear("tblPlani");
      jsonJS.ctrpla.forEach(function(i){
        addRowPlani(i.ctrp_num, i.ctrp_fini, i.ctrp_ffin);
      });

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

      $("#tblPlani tbody #btnSelecPlani").click(function() {
        selecPlani(a, $(this).parents("tr"));
      });

      alClose();
    }
  });
}

function addRowPlani(num, fecini, fecfin) {
  const markup =  "<tr>"
                    +"<td style='width: 150px;'>"+num+"</td>"
                    +"<td style='width: 200px;'>"+YMDToDMY(fecini)+"</td>"
                    +"<td style='width: 200px;'>"+YMDToDMY(fecfin)+"</td>"
                    +"<td><button class='btn btn-sm btn-warning badge' id='btnSelecPlani'>"+textBtnSelec+"</button></td>"
                  +"</tr>";
  $("#tblPlani tbody").append(markup);
}

function selecPlani(a, tr) {
  if (inputPlani == "numNum") {
    document.thisform.numNum.value = tr.find("td:eq(0)").text();
    $("form[name='thisform'] #numNum").trigger("change");
  } else {
    $("#"+inputPlani).val(tr.find("td:eq(0)").text());
  }

  $("#pills-"+a+"-tab").click();
  $('html, body').animate( { scrollTop : 0 }, 500 );
  tblClear("tblPlani");
}