// Codigo ajax formulario 
var total2 = 0;
var totid2 = 0;
var array2 = ['0'];
var id = '';
var pro = '';
var pre = '';
var can = '';
var bod = '';
jQuery(document).ready(function() {
    $("#anadir2").on("submit", function(e){
        var idcliente=document.getElementById("cliente").value;
        var pre=document.getElementById("precio").value;
        e.preventDefault();
        var formData = $(this).serialize();
        var action = $(this).attr('action');
        $.ajax({
            type: "POST",
            url: r+"pedido/producto2.php",
            dataType: 'JSON',
            data: formData+'&idcliente='+idcliente,
            success: function(response){
                var len = response.length;
                for(var i=0; i<len; i++){
                    var id = response[i].rid;
                    var pro = response[i].rpro;
                    var can = parseInt(response[i].rcan);
                    var iva = response[i].riva;
                    var bod = parseInt(response[i].rbod);
                    var totid2 = response[i].rtotid2;
                    console.log(bod+'<'+can);
                    if(bod<can){
                        $("#mesage").html('<div class="alert alert-success padding" role="alert" style="background:#00ff3d;color:#dc3545"><strong>NO HAY EXISTENCIA SUFICIENTE, solo hay '+bod+' paquetes!</strong></div>').slideDown(400);
                         
                        }
                    else{
                    if (array2.indexOf(id) === -1) {
                        array2.push(id);
                        var tr_str = '<tr id="trow'+id+'">' +
                        '<td><input type="hidden" value="' + id + '" name="iden'+id+'"/>' + pro + '</td>' +
                        '<td><input type="hidden" value="' + can + '" name="cant'+id+'"/>' + can + '</td>' +
                        '<td><input  type="hidden" value="' + pre + '" name="prec'+id+'"/>$' + pre + '</td>' +
                        '<td><input type="hidden" value="' + iva + '" name="iva'+id+'"/>$' + totid2 + '</td>' +
                        '<td><a  title="Borrar" onclick="EliminarFila('+ totid2 + ',' + id + ');"><i class="fa text-danger fa-trash fa-2x fa-lg"></i></a></td>' +
                        '</tr>';
                        $("#tabla").append(tr_str);
                        total2 = total2+totid2;
                        $("#total2").html(total2);
                         $("#nada").remove();
                    } else if (array2.indexOf(id) > -1) {
                        $("#mesage").html('<div class="alert alert-success padding" role="alert">Este Articulo Ya se Encentra A&ntilde;adido!</div>').slideDown(400);
                        
                    }
                }}$('#anadir')[0].reset();   }
            });setTimeout(function(){$("#mesage").html(this).slideUp(700);
                         }, 3000);
    });
        $('#cliente').on('change', function() {
        var formData = $(this).serialize();
        $.ajax({
            type: "POST",
            url: r+"pedido/anterior.php",
            dataType: 'JSON',
            data: formData,
            success: function(response){
                var memori = response.length;
                for(var i=0; i<memori; i++){
                    var boton = response[i].rbtn;
                    var modal = response[i].rmodal;
                    var code = response[i].rcode;
                }
                $("#pedanterior").html(boton);
                $("#memoria").html(modal);
                $("#code").html(code);
                $("#btnagregar").removeClass( "d-none" ).addClass( "btn-block " );

            }
        });
    });
});