{% extends "base.html" %}

{% block title %}Binary file viewer{% endblock %}

{% block content %}
<div class="container mt-4">
    <div class="card shadow-sm">
        <div class="card-body">
            <h1 class="mb-3">Viewing File: {{ filename }}</h1>
            <p class="text-muted">Tipo MIME: {{ mime_type }}</p>

            <div class="row mb-4">
                <div class="col-md-6">
                    <h4>File Info</h4>
                    <ul class="list-group list-group-flush">
                        <li class="list-group-item"><strong>File Name:</strong> {{ filename }}</li>
                        <li class="list-group-item"><strong>Size:</strong> {{ size }} bytes</li>
                        <li class="list-group-item"><strong>Uploaded by:</strong> {{ owner.username }}</li>
                        <li class="list-group-item"><strong>Upload Date:</strong> {{ paste.created_at.strftime('%Y-%m-%d %H:%M:%S') }}</li>
                    </ul>
                </div>
                <div class="col-md-6">
                    <h4>Acciones</h4>
                    <div class="d-flex flex-column">
                        <a href="{{ url_for('download_paste', id=paste.id) }}" class="btn btn-success mb-2">
                            <i class="fas fa-download me-2"></i> Descargar Archivo
                        </a>
                         {% if 'admin' in session %}
                            <button type="button" class="btn btn-danger" data-bs-toggle="modal" data-bs-target="#deleteModal" aria-label="Delete Paste">
                                <i class="fas fa-trash-alt me-2"></i> Delete Paste
                            </button>
                        {% endif %}
                    </div>
                </div>
            </div>

            <div class="card">
                <div class="card-body text-center">
                    <h5 class="card-title">Download from smartphone</h5>
                    <p class="card-text">Scan this code from your mobile device:</p>
                    <div id="qrcode" class="my-3 mx-auto" style="width:200px; height:200px;"></div>
                    <button id="download-qr" class="btn btn-outline-primary mt-2">
                        <i class="fas fa-download me-2"></i> Download QR 
                    </button>
                </div>
            </div>
        </div>
    </div>
</div>

 {% if 'admin' in session %}
<div class="modal fade" id="deleteModal" tabindex="-1" aria-labelledby="deleteModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <form action="{{ url_for('admin_delete_paste', paste_id=paste.id) }}" method="POST">
        <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title" id="deleteModalLabel">Confirmar Eliminación</h5>
            <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
          </div>
          <div class="modal-body">
            ¿Are you sure? This action cannot be undone.
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
            <button type="submit" class="btn btn-danger">Delete</button>
          </div>
        </div>
    </form>
  </div>
</div>
{% endif %}
{% endblock %}

{% block scripts %}
<script>
    document.addEventListener('DOMContentLoaded', () => {
        const pasteId = "{{ paste.id }}";
        const downloadUrl = "{{ url_for('download_paste', id=paste.id, _external=True) }}";

        const qrCodeContainer = document.getElementById('qrcode');

        new QRCode(qrCodeContainer, {
            text: downloadUrl,
            width: 200,
            height: 200,
            colorDark : "#000000",
            colorLight : "#ffffff",
            correctLevel : QRCode.CorrectLevel.H
        });

        const downloadQrButton = document.getElementById('download-qr');
        downloadQrButton.addEventListener('click', () => {
            const canvas = qrCodeContainer.querySelector('canvas');
            if (canvas) {
                const pngUrl = canvas.toDataURL("image/png").replace("image/png", "image/octet-stream");
                const downloadLink = document.createElement('a');
                downloadLink.href = pngUrl;
                downloadLink.download = `qr_code_paste_${pasteId}.png`;
                document.body.appendChild(downloadLink);
                downloadLink.click();
                document.body.removeChild(downloadLink);
                alert('QR descargado exitosamente!');
            } else {
                alert('Error al generar el código QR.');
            }
        });
    });
</script>
{% endblock %}

