{{define "view_text"}}
  {{template "base" .}}
{{end}}

{{define "view_text_content"}}
<div class="max-w-4xl mx-auto p-6">
    <!-- Header -->
    <div class="mb-6">
        <div class="flex items-center justify-between mb-4">
            <h1 class="text-3xl font-bold text-white">{{.file_name}}</h1>
            <div class="flex items-center space-x-3">
                {{if and .Authenticated .is_editable}}
                <a href="{{url (print "/editor/edit_text/" .file_path)}}" class="btn-primary">
                    <i class="fas fa-edit mr-2"></i>Edit
                </a>
                {{end}}
                <a href="{{url (print "/download/" .file_path)}}" class="btn-secondary">
                    <i class="fas fa-download mr-2"></i>Download
                </a>
                {{if .Authenticated}}
                <button class="btn-danger delete-file-btn" data-path="{{.file_path}}">
                    <i class="fas fa-trash mr-2"></i>Delete
                </button>
                {{end}}
            </div>
        </div>
        
        {{if .folder_path}}
        <p class="text-gray-400">
            <i class="fas fa-folder mr-2"></i>
            <a href="{{url (print "/folder/" .folder_path)}}" class="text-blue-400 hover:text-blue-300">{{.folder_path}}</a>
        </p>
        {{end}}
    </div>

    <!-- File Content -->
    <div class="bg-gray-800 rounded-lg p-6">
        <pre class="text-sm text-gray-300 whitespace-pre-wrap overflow-x-auto"><code>{{.content}}</code></pre>
    </div>
</div>

<!-- Delete Confirmation Modal -->
<div id="delete-modal" class="modal-overlay hidden">
    <div class="modal-content">
        <h3 class="text-lg font-medium text-white mb-4">Confirm Delete</h3>
        <p class="text-gray-300 mb-6">Are you sure you want to delete this file? This action cannot be undone.</p>
        <div class="flex justify-end space-x-3">
            <button id="cancel-delete" class="btn-secondary">Cancel</button>
            <button id="confirm-delete" class="btn-danger">Delete</button>
        </div>
    </div>
</div>
{{end}}

{{define "view_text_scripts"}}
<script>
    let deleteModal = document.getElementById('delete-modal');
    let deleteTarget = null;

    // Delete functionality
    document.addEventListener('click', function(e) {
        if (e.target.closest('.delete-file-btn')) {
            deleteTarget = e.target.closest('.delete-file-btn').dataset.path;
            deleteModal.classList.remove('hidden');
        }
    });

    document.getElementById('cancel-delete').addEventListener('click', function() {
        deleteModal.classList.add('hidden');
        deleteTarget = null;
    });

    document.getElementById('confirm-delete').addEventListener('click', function() {
        if (deleteTarget) {
            const m = document.cookie.match(/(?:^|; )csrf_token=([^;]+)/);
            const csrf = m && m[1] ? decodeURIComponent(m[1]) : '';
            fetch(window.prefix('/editor/delete/' + deleteTarget), {
                method: 'DELETE',
                headers: csrf ? { 'X-CSRF-Token': csrf } : {}
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    showNotification('File deleted successfully', 'success');
                    // Redirect to folder or root
                    const folderPath = '{{.folder_path}}';
                    if (folderPath) {
                        window.location.href = window.prefix('/folder/' + folderPath);
                    } else {
                        window.location.href = window.prefix('/');
                    }
                } else {
                    throw new Error(data.error || 'Delete failed');
                }
            })
            .catch(error => {
                showNotification('Delete failed: ' + error.message, 'error');
            });
        }
        deleteModal.classList.add('hidden');
        deleteTarget = null;
    });

    // Close modal when clicking outside
    deleteModal.addEventListener('click', function(e) {
        if (e.target === this) {
            this.classList.add('hidden');
            deleteTarget = null;
        }
    });

    // Highlight code if possible
    hljs.highlightAll();
</script>
{{end}}
