imgPhoto = "../../img/photo.jpg";
imgBusArtic3.src = imgPhoto;

function buscarArtic3(b, a){
  const parametros = new FormData();
  parametros.append("txtDescb", document.formArtic3.txtDescb.value);
  parametros.append("txtCodb", document.formArtic3.txtCodb.value);
  parametros.append("txtDesdb", document.formArtic3.txtDesdb.value);

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

      tblClear("tblArtic3");
      jsonJS.artic.forEach(function(i){
        addRowArtic3(i.art_cod, i.art_desc, i.art_desd, i.art_pre1, i.art_epas1);
      });

      $("#tblArtic3 tbody tr").click(function() {
        selecArtic3Exi(a, $(this));
      });

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

      $("#tblArtic3 tbody #btnSelecArtic3").click(function() {
        selecArtic3(a, $(this).parents("tr"));
      });

      alClose();
    }
  });
}

function addRowArtic3(cod, desc, descd, pre1, epas1) {
  const markup =  "<tr>"
                    +"<td style='font-size:16px;'>"+cod+"</td>"
                    +"<td style='font-size:16px;'>"+desc+"</td>"
                    +"<td style='font-size:16px;'>"+descd+"</td>"
                    +"<td class='text-right' style='font-size:16px;'>"+pre1+"</td>"
                    +"<td class='text-center' style='font-size:16px;'>"+epas1+"</td>"
                    +"<td><button class='btn btn-warning badge' style='font-size:14px;' id='btnSelecArtic3'>"+textBtnSelec+"</button></td>"
                  +"</tr>";
  $("#tblArtic3 tbody").append(markup);
}

function addRowExibus3(nomb, exist){
  const row = "<tr>"
              +"<td id='dataNomb'>"+nomb+"</td>"
              +"<td id='dataExistbus3' class='text-right'>"+exist+"</td>"
            +"</tr>";
  $("#tblExibus3 tbody").append(row);
}

function sumExibus3(){
  $("#dataTotExistbus3").text(tblSumCol("tblExibus3", "dataExistbus3").toFixed(2));
}

function selecArtic3(a, tr) {
  $("#pills-"+a+"-tab").click();
 console.log(tr.find("td:eq(0)").text());
  $("#"+inputArtic).val(tr.find("td:eq(0)").text());
  $("#"+inputArtic).trigger("change");

  tblClear("tblArtic3");
  document.formArtic3.txtDescb.value = "";
  document.formArtic3.txtCodb.value = "";
  document.formArtic3.txtDesdb.value = "";
}

function selecArtic3Exi(a, tr) {
  const parametros = new FormData();
  parametros.append("txtCod", tr.find("td:eq(0)").text());

  $.ajax({
    data:  parametros,
    url:   "busArtic/dataExi.php",
    type:  "post",
    contentType: false,
    processData: false,
    success: function (jsonPhp) {
      const jsonJS = JSON.parse(jsonPhp);
      
      imgExist(imgBusArtic3, jsonJS.imgBusArtic, imgPhoto);

      tblClear("tblExibus3")
      jsonJS.exiart.forEach(function(i){
        addRowExibus3(i.bod_nom, i.ea_exist);
      });
      sumExibus3();
    }
  });
}


$("form[name=formArtic3] #txtDescb").focus(function() {
  tblClear("tblExibus3");
  imgBusArtic3.src = imgPhoto;
});