$(function () {

    $.tzPOST('getComprobante', {'numero_documento': documento, 'nombre_comprobante': comprobante, 'ruc_empresa': RUC}, function(resolve){

        if(resolve.error){
            swal({title: "Error Interno!", text: "Por favor verifique que todo este correcto e intente de nuevo", icon: "error", button: "OK"});
        }
        else{
            var APP_URL = "http://"+resolve.dominio+"."+resolve.servidor+"/public";
            var datos = [];
            for( var i=0; i< resolve.comprobantes.length; i++ ){
                resolve.comprobantes[i]['i'] = i+1;
                if(resolve.comprobantes[i]){
                    datos.push(render(resolve.comprobantes[i], APP_URL));
                }
            }
            $('#tbody').html(datos.join(''));
          
        }
    });

});


/*********************************************************************************************************/
/* Otras funciones
/*********************************************************************************************************/

$.tzPOST = function(action,data,callback){
  $.post('php/ajax.php?action='+action,data,callback,'json');
}


function render(resolve, APP_URL){
  var arr = [];
    arr = [
        '<tr>',
            '<td>',resolve.documento,'</td>',
            '<td>',resolve.cliente,'</td>',
            '<td>',resolve.tipo,'</td>',
            '<td>',resolve.nombre_comprobante,'</td>',
            '<td>',resolve.fecha_venta,'</td>',
            '<td>',resolve.total_venta,'</td>',
            '<td><a class="ui mini vertical animated green button" title="Descargar comprobante en XML" target="_blank" href="',APP_URL,'/../sunat/xml/comprobantes/',RUC,'-',resolve.codigo,'-',resolve.nombre_comprobante,'.xml" download><div class="hidden content">XML</div><div class="visible content"><i class="download icon"></i></div></a>',
                '<a class="ui mini vertical animated red button" title="Descargar comprobante en PDF" target="_blank" href="',APP_URL,'/../pdf/imprimeA4.php?id_venta=',resolve.id_venta,'" download><div class="hidden content">PDF</div><div class="visible content"><i class="download icon"></i></div></a>',
           '</td>',
        '</tr>',
    ];
    return arr.join(''); 
}