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

function buscarArticmp(b, a){
  const parametros = new FormData();
  parametros.append("txtDescb", document.formArticmp.txtDescb.value);
  parametros.append("txtCodFab", document.formArticmp.txtCodFab.value);
  parametros.append("txtCodb", document.formArticmp.txtCodb.value);

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

      tblClear("tblArticmp");
      jsonJS.artic.forEach(function(i){
        addRowArticmp(i.art_cod, i.art_desc);
      });

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

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

      $("#tblArticmp tbody #btnSelecArticmp").click(function() {
        selecArticmp(a, $(this).parents("tr"));
      });

      alClose();
    }
  });
}

function addRowArticmp(cod, desc) {
  const markup =  "<tr>"
                    +"<td>"+cod+"</td>"
                    +"<td>"+desc+"</td>"
                    +"<td><button class='btn btn-sm btn-warning badge' id='btnSelecArticmp'>"+textBtnSelec+"</button></td>"
                  +"</tr>";
  $("#tblArticmp tbody").append(markup);
}

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

function sumExibusmp(){
  $("#dataTotExistbus").text(tblSumCol("tblExibus", "dataExistbus").toFixed(2));
}

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

  tblClear("tblArticmp");
  document.formArticmp.txtDescb.value = "";
  document.formArticmp.txtCodFab.value = "";
  document.formArticmp.txtCodb.value = "";
}

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

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

      tblClear("tblExibusmp")
      jsonJS.exiart.forEach(function(i){
        addRowExibusmp(i.bod_nom, i.ea_exist);
      });
      sumExibusmp();
    }
  });
}


$("form[name=formArtic] #txtDescb").focus(function() {
  tblClear("tblExibus");
  imgBusArticmp.src = imgPhoto;
});