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

function buscarArtic2(b, a){
  const parametros = new FormData();
  parametros.append("txtDescb", document.formArtic2.txtDescb.value);
  parametros.append("txtCodFab", document.formArtic2.txtCodFab.value);
  parametros.append("txtCodb", document.formArtic2.txtCodb.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("tblArtic2");
      jsonJS.artic.forEach(function(i){
        addRowArtic2(i.art_cod, i.art_desc, i.art_pre1);
      });

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

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

      $("#tblArtic2 tbody #btnSelecArtic2").click(function() {
        selecArtic2(a, $(this).parents("tr"));
      });

      alClose();
    }
  });
}

function addRowArtic2(cod, desc, pre1) {
  const markup =  "<tr>"
                    +"<td>"+cod+"</td>"
                    +"<td>"+desc+"</td>"
                    +"<td class='text-right'>"+pre1+"</td>"
                    +"<td><button class='btn btn-sm btn-warning badge' id='btnSelecArtic2'>"+textBtnSelec+"</button></td>"
                  +"</tr>";
  $("#tblArtic2 tbody").append(markup);
}

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

function sumExibus2(){
  $("#dataTotExistbus2").text(tblSumCol("tblExibus2", "dataExistbus2").toFixed(2));
}

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

  tblClear("tblArtic2");
  document.formArtic2.txtDescb.value = "";
  document.formArtic2.txtCodFab.value = "";
  document.formArtic2.txtCodb.value = "";
}

function selecArtic2Exi(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(imgBusArtic2, jsonJS.imgBusArtic, imgPhoto);

      tblClear("tblExibus2")
      jsonJS.exiart.forEach(function(i){
        addRowExibus2(i.bod_nom, i.ea_exist);
      });
      sumExibus2();
    }
  });
}


$("form[name=formArtic2] #txtDescb").focus(function() {
  tblClear("tblExibus2");
  imgBusArtic2.src = imgPhoto;
});