@page
@using DXApp.TemplateKitProject.Services
@model DXApp.TemplateKitProject.Pages.Invoices.ViewAttachmentModel
@{
    ViewData["Title"] = $"Attachment: {Model.FileName}";
}

<h2>?? Attachment: @Model.FileName</h2>

<div class="mb-3">
    <a href="/Invoices/Details" class="btn btn-secondary">? Zurück</a>
    <a href="?handler=Download&filePath=@Request.Query["filePath"]" 
       class="btn btn-primary">
        ?? Download
    </a>
</div>

@switch (Model.ViewerType)
{
    case AttachmentViewerType.Pdf:
        <div class="alert alert-info">
            PDF-Dateien können Sie über die Result-PDF-Funktion anzeigen.
        </div>
        break;

    case AttachmentViewerType.Xml:
        <div class="card">
            <div class="card-header bg-primary text-white">
                <strong>XML-Inhalt</strong> (ZUGFeRD/Factur-X)
            </div>
            <div class="card-body p-0">
                <textarea id="xml-viewer" style="display:none;">@Model.TextContent</textarea>
                <div id="xml-rendered"></div>
            </div>
        </div>

        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.16/codemirror.min.css">
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.16/theme/monokai.min.css">
        <script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.16/codemirror.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.16/mode/xml/xml.min.js"></script>
        <script>
            var editor = CodeMirror(document.getElementById('xml-rendered'), {
                value: document.getElementById('xml-viewer').value,
                mode: 'xml',
                lineNumbers: true,
                readOnly: true,
                theme: 'monokai',
                lineWrapping: true
            });
        </script>
        break;

    case AttachmentViewerType.Text:
        <div class="card">
            <div class="card-header bg-secondary text-white">
                <strong>Text-Inhalt</strong>
            </div>
            <div class="card-body">
                <pre style="background-color: #f5f5f5; padding: 15px; border-radius: 5px; max-height: 600px; overflow: auto;">@Model.TextContent</pre>
            </div>
        </div>
        break;

    case AttachmentViewerType.Image:
        <div class="card">
            <div class="card-body text-center">
                <img src="?handler=Download&filePath=@Request.Query["filePath"]" 
                     alt="@Model.FileName" 
                     class="img-fluid" 
                     style="max-height: 800px;">
            </div>
        </div>
        break;

    case AttachmentViewerType.Word:
        <div class="alert alert-warning">
            <strong>Word-Dokumente können nicht direkt angezeigt werden.</strong>
            <p>Bitte laden Sie die Datei herunter oder implementieren Sie eine Konvertierung zu PDF.</p>
        </div>
        break;

    default:
        <div class="alert alert-warning">
            <strong>Dieser Dateityp kann nicht angezeigt werden.</strong>
            <p>Bitte laden Sie die Datei herunter.</p>
        </div>
        break;
}
