{% trans_default_domain 'core' %}



{% if entity.autorizado == true %}
    <a type="submit" class="btn btn-app disabled" name="Form-edit"><span
                class="glyphicon glyphicon-pencil"></span> {% trans %}abstract_invoice.show.edit{% endtrans %}
    </a>
{% else %}
    <button type="submit" class="btn btn-app" name="Form-edit"><span
                class="glyphicon glyphicon-pencil"></span> {% trans %}abstract_invoice.show.edit{% endtrans %}
    </button>
{% endif %}

{% if entity.autorizado == true and entity.ambiente==2 %}
    <a type="submit" class="btn btn-app disabled" name="Form-delete"
       data-confirm="{% trans %}abstract_invoice.show.delete_confirm{% endtrans %}"><span
                class="glyphicon glyphicon-trash"></span> {% trans %}abstract_invoice.show.delete{% endtrans %}
    </a>
{% else %}
    <button type="submit" class="btn btn-app" name="Form-delete"
            data-confirm="{% trans %}abstract_invoice.show.delete_confirm{% endtrans %}"><span
                class="glyphicon glyphicon-trash"></span> {% trans %}abstract_invoice.show.delete{% endtrans %}
    </button>
{% endif %}


{#
<button type="submit" name="Form-pdf" class="btn btn-app">
    <i class="fa fa-file-pdf-o"></i>Pdf
</button>
#}

<button type="button" class="btn btn-app" data-toggle="modal"
        data-target="#modal-default">
    <i class="glyphicon glyphicon-envelope"></i>Mail
</button>

{% if entity.autorizado == false %}

<button type="submit" name="Form-enviar" id="btn-enviar-sri" class="btn btn-app">
    <i class="glyphicon glyphicon-cloud-upload"></i> Enviar SRI
</button>
{#
    <button type="submit" name="Form-enviar" class="btn btn-app">
        <i class="glyphicon glyphicon-cloud-upload"></i>Enviar SRI
    </button>
    #}
    
    <style>

@keyframes sriBlink {
    0% { transform: scale(1); background-color:#f39c12; }
    50% { transform: scale(1.1); background-color:#e74c3c; }
    100% { transform: scale(1); background-color:#f39c12; }
}

.sri-alerta {
    animation: sriBlink 1s infinite;
    color: white !important;
}

</style>
   <button type="submit" name="Form-auto" id="btn-autorizar-sri" class="btn btn-app">
    <i class="glyphicon glyphicon-cloud-download"></i>Autorizar SRI
</button>

{% endif %}

<button type="submit" name="Form-anular" class="btn btn-app" data-confirm="Esta seguro de anular el comprobante">
    <i class="glyphicon glyphicon-ban-circle"></i>Anular
</button>


<div id="myModal" class="modal fade" tabindex="-1">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            {#<div class="modal-header">
                <button type = "button" class="close" data-dismiss = "modal">×</button>
            </div>#}
            <div class="modal-body" style="height: 500px;">
                <iframe id="pdfFrame" style="width:100%;height:100%;"></iframe>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" data-dismiss="modal">Cerrar</button>
            </div>
        </div>
    </div>
</div>

<script>

    /**

     *

     *  Base64 encode / decode

     *  http://www.webtoolkit.info/

     **/

    function base64ToUint8Array(base64) {
        var raw = atob(base64);
        var uint8Array = new Uint8Array(raw.length);
        for (var i = 0; i < raw.length; i++) {
            uint8Array[i] = raw.charCodeAt(i);
        }
        return uint8Array;
    }


    //$('#pdf').click(function (e) {
    function verPdf(pdf_url) {
        //e.preventDefault(); // if you have a URL in the link

        $("#pdfFrame").contents().find("body").html("");

        $.ajax({
            type: "GET",
            url: pdf_url,
            success: function (data) {

                /*var winparams = 'dependent=yes,locationbar=no,scrollbars=yes,menubar=no,' +
                    'resizable,screenX=50,screenY=50,width=800,height=600';

                var htmlPop = '<embed width=100% height=100%'
                    + ' type="application/pdf"'
                    + ' src="data:application/pdf;base64,'
                    + escape(data)
                    + '"></embed>';


                var printWindow = window.open("", "PDF", winparams);
                printWindow.document.write(htmlPop);
                printWindow.print();
                */

                var pdfData = base64ToUint8Array(data);
                //var pdfData =  Base64.decode(escape(data));

                var pdfViewerFrame = document.getElementById("pdfFrame");

                pdfViewerFrame.onload = function () {
                    pdfViewerFrame.contentWindow.PDFViewerApplication.open(pdfData);
                    pdfViewerFrame.contentWindow.PDFViewerApplication.setTitleUsingUrl('{{ entity.labelPdf }}');
                };

                pdfViewerFrame.setAttribute("src", "/web/viewer.html?file=");

                //$('#frame').append(htmlPop);

            }
        });

    };

    $('#btn-generate-pdf').click(function (e) {
        e.preventDefault(); // if you have a URL in the link

        /*const xhr = new XMLHttpRequest();
        xhr.open('GET', pdf_url, true);
        xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
        xhr.responseType = 'blob';
        xhr.onreadystatechange = function () {
            if (this.readyState == 4 && this.status == 200) {
                if (window.navigator.msSaveOrOpenBlob) {
                    window.navigator.msSaveBlob(this.response, "fileName.pdf");
                } else {
                    const downloadLink = window.document.createElement('a');
                    const contentTypeHeader = xhr.getResponseHeader("Content-Type");
                    downloadLink.href = window.URL.createObjectURL(new Blob([this.response], { type: contentTypeHeader }));
                    downloadLink.download = "fileName.pdf";
                    document.body.appendChild(downloadLink);
                    downloadLink.click();
                    document.body.removeChild(downloadLink);
                }
            }
        };
        hr.send(null);
        *
         */

        /*let xhr = new XMLHttpRequest();
        let iframe = document.getElementById('my-frame');

        xhr.addEventListener("load", function(e) {
            if (this.status === 200) {
                let contentType = xhr.getResponseHeader('Content-Type');
                let blob = new Blob([this.response], { type: contentType });
                let blobUrl = window.URL.createObjectURL(blob);
                iframe.setAttribute('src', blobUrl);
            }
        });

        xhr.open('GET', pdf_url, true);
        xhr.responseType = 'arraybuffer';
        xhr.send();
*/

    });
</script>
