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

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

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

      tblClear("tblArtic");
      jsonJS.artic.forEach(function(i){
        addRowArtic(i.art_cod, i.art_desc, i.art_desd, i.art_prov, i.art_pre1, i.art_epas1, i.art_epas2, i.art_epas3, i.art_epas4);
      });

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

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

      $("#tblArtic tbody #btnSelecArtic").click(function() {
        selecArtic(a, $(this).parents("tr"));
      });

      alClose();
    }
  });
}

function addRowArtic(cod, desc, descd, genericod, pre1, epas1, epas2, epas3, epas4) {
  const markup =  "<tr>"
                    +"<td style='font-size:12px;'>"+cod+"</td>"
                    +"<td style='font-size:12px;'>"+desc+"</td>"
                    +"<td style='font-size:12px;'>"+descd+"</td>"
                    +"<td style='font-size:12px;'>"+genericod+"</td>"
                    +"<td class='text-right' style='font-size:12px;'>"+pre1+"</td>"
                    +"<td class='text-center' style='font-size:12px;'>"+epas1+"</td>"
                    +"<td class='text-center' style='font-size:12px;'>"+epas2+"</td>"
                    +"<td class='text-center' style='font-size:12px;'>"+epas3+"</td>"
                    +"<td class='text-center' style='font-size:12px;'>"+epas4+"</td>"
                    +"<td><button class='btn btn-warning badge' style='font-size:14px;' id='btnSelecArtic'>"+textBtnSelec+"</button></td>"
                  +"</tr>";
  $("#tblArtic tbody").append(markup);
}

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

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

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

  tblClear("tblArtic");
  document.formArtic.txtDescb.value = "";
  document.formArtic.txtCodFab.value = "";
  document.formArtic.txtCodb.value = "";
  document.formArtic.txtDesdb.value = "";
}

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

      tblClear("tblExibus")
      jsonJS.exiart.forEach(function(i){
        addRowExibus(i.bod_nom, i.ea_exist);
      });
      sumExibus();
    }
  });
}


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