<!-- Document Preview Modal (iframe) -->
<div class="modal fade" id="docPreviewModal" tabindex="-1" aria-labelledby="docPreviewTitle" aria-hidden="true">
    <div class="modal-dialog modal-xl modal-dialog-scrollable">
        <div class="modal-content">
            <div class="modal-header py-2">
                <h6 class="modal-title" id="docPreviewTitle">Document Preview</h6>
                <div class="d-flex gap-2 ms-auto">
                    <button id="docPrintBtn" class="btn btn-sm btn-outline-secondary" title="Print"><i class="fas fa-print"></i></button>
                    <button id="docDownloadBtn" class="btn btn-sm btn-outline-primary" title="Download PDF"><i class="fas fa-download"></i> Download</button>
                    <button id="docNewTabBtn" class="btn btn-sm btn-outline-info" title="Open in new tab"><i class="fas fa-external-link-alt"></i></button>
                    <button class="btn btn-sm btn-outline-secondary" data-bs-dismiss="modal"><i class="fas fa-times"></i></button>
                </div>
            </div>
            <div class="modal-body p-0" style="min-height: 75vh;">
                <iframe id="docPreviewIframe" src="" style="width:100%;height:75vh;border:none;" title="Document Preview"></iframe>
            </div>
        </div>
    </div>
</div>

<script>
    // Open document in iframe modal
    function previewDocument(url, title) {
        document.getElementById('docPreviewIframe').src = url;
        document.getElementById('docPreviewTitle').textContent = title || 'Document Preview';
        document.getElementById('docDownloadBtn').onclick = function() { window.location.href = url; };
        document.getElementById('docPrintBtn').onclick = function() {
            var iframe = document.getElementById('docPreviewIframe');
            if (iframe.contentWindow) iframe.contentWindow.focus(); iframe.contentWindow.print();
        };
        document.getElementById('docNewTabBtn').onclick = function() { window.open(url, '_blank'); };
        new bootstrap.Modal(document.getElementById('docPreviewModal')).show();
    }
</script>