var r='/admin/';
$(document).ready(function() {
    $("#msn-alert").css("display", "block");
    setTimeout(function(){$("#msn-alert").css("display", "none");}, 6000);
    var con=0;
    function numsn(){
        $.ajax({
            type: "POST",
            url: r+"pedido/alert-pedido.php",
            dataType: 'JSON',
            success: function(response){
            var len = response.length;
            for(var i=0; i<len; i++){
                var text = response[i].txtmsn;
                var num = response[i].numsn;
                $('#txtmsn').html(text);
                $('#numsn').html(num);
            }}
        });
    }

    setInterval(numsn, 30000);
    $('#departamento').on('change', function() {
        var id = $(this).val();
        $.ajax({
                url: r+'pais/municipios.php' ,
                type: "post",
                dataType: 'html',
                 data: { 'id':id},
	            success: function(data){}
        }).done(function(response) {
            $('#ciudad').html(response).slideDown(400);
            $('#localidad').html('').slideDown(400);
            $('#zona').html('').slideDown(400);
        });
    });
    $('#ciudad').on('change', function() {
        var id = $(this).val();
        $('#localidad').trigger("reset");
        $('#zona').trigger("reset");
       $.ajax({
                url: r+'pais/localidad.php' ,
                type: "post",
                dataType: 'html',
                 data: { 'id':id},
	            success: function(data){}
        }).done(function(response) {
            $('#localidad').html(response).slideDown(400);
            $('#zona').html('').slideDown(400);
        });
    });
    $('#localidad').on('change', function() {
        var id = $(this).val();
        $('#zona').trigger("reset");
       $.ajax({
                url: r+'pais/zonas.php' ,
                type: "post",
                dataType: 'html',
                 data: { 'id':id},
	            success: function(data){}
        }).done(function(response) {
            $('#zona').html(response).slideDown(400);
        });
    });
    
    $('#producto').on('change', function() {
        var id = $(this).val();
        $.ajax({
                url: r+'pedido/unidad.php' ,
                type: "post",
                dataType: 'JSON',
                 data: { 'id':id},
	            success: function(data){}
        }).done(function(response) {
                var und = response.unidad;
                var pre = response.precio;
            console.log( response );
            $('#unidad').html(und).show();
            $( "#precio" ).val(pre);
        });
    });
});
  
$(document).ready(function() {
     // Formulario Jovenes
    var formularios = $('#formulario');
    var mensage = $('#msn-alert');
    $(formularios).submit(function(f) {
        $(mensage).slideUp(750);
        $(mensage).hide();
        f.preventDefault();
        var action = $(this).attr('action');
        var type = $(this).attr('method');
        var form = $(this);
        var formdata = false;
        if (window.FormData){
            formdata = new FormData(form[0]);
        }
        $.ajax({
            url: action,
            type: type,
        data        : formdata ? formdata : form.serialize(),
        cache       : false,
        contentType : false,
        processData : false,
	    success     : function(data, textStatus, jqXHR){
                    if (data.match('.php') != null){window.location.replace(data);}
	        }
        }).done(function(response) {
            $(mensage).html(response).slideDown(400);
            setTimeout(function(){$(mensage).html(this).slideUp(700);}, 5000);
        });
    });
    
    
    
    
    var formularios = $('#gasto');
    var mensage = $('#msn-alert');
    $("#gasto").on("submit", function() {
     var action = $(this).attr('action');
     
        f.preventDefault();
    $.ajax({
        url: action, // point to server-side PHP script 
        dataType: 'text',  // what to expect back from the PHP script, if anything
        cache: false,
        contentType: false,
        processData: false,
        data:  new FormData(this),
        type: "POST",
        success: function(data){
                    if (data.match('.php') != null){window.location.replace(data);}
	        }
        }).done(function(response) {
            $(mensage).html(response).slideDown(400);
            setTimeout(function(){$(mensage).html(this).slideUp(700);}, 5000);
        });
     });
});

// Codigo ajax formulario 
var total = 0;
var totid = 0;
var array = ['0'];
var id = '';
var pro = '';
var pre = '';
var can = '';
var bod = '';
jQuery(document).ready(function() {
    $("#anadir").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/producto.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 totid = response[i].rtotid;
                    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 (array.indexOf(id) === -1) {
                        array.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+'"/>$' + totid + '</td>' +
                        '<td><a  title="Borrar" onclick="EliminarFila('+ totid + ',' + id + ');"><i class="fa text-danger fa-trash fa-2x fa-lg"></i></a></td>' +
                        '</tr>';
                        $("#tabla").append(tr_str);
                        total = total+totid;
                        $("#total").html(total);
                         $("#nada").remove();
                    } else if (array.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 " );

            }
        });
    });
});
// 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;
        var arc=document.getElementById("archivo").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 not = response[i].rnot;
                    var bod = parseInt(response[i].rbod);
                    var totid2 = response[i].rtotid;
                    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>' +
                        '<input type="file" value="' + arc + '" name="arc'+id+'"/>' +
                        '<input type="hidden" value="' + not + '" name="not'+id+'"/>' +
                        '<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();
                         document.getElementById("producto").value='';
                         document.getElementById("cantidad").value='';
                         document.getElementById("precio").value='';
                         document.getElementById("archivo").value='';
                         document.getElementById("nota").select='';
                    } else if (array2.indexOf(id) > -1) {
                        $("#mesage").html('<div class="alert alert-success padding" role="alert">Este Articulo Ya se Encentra en el Inventario!</div>').slideDown(400);
                        
                    }
                }}$('#anadir')[0].reset();   }
            });setTimeout(function(){$("#mesage").html(this).slideUp(700);
                         }, 3000);
    });
});
