/**
 * 文件详情查看页面交互脚本
 */

(function() {
    'use strict';

    let previewVisible = false;

    window.togglePreview = function() {
        const previewPanel = document.getElementById('previewPanel');
        const content = document.querySelector('.file-view-content');
        const previewBtn = document.getElementById('previewBtn');
        
        if (!previewPanel) return;
        
        previewVisible = !previewVisible;
        
        if (previewVisible) {
            previewPanel.style.display = 'flex';
            content.classList.add('has-preview');
            if (previewBtn) {
                previewBtn.innerHTML = '<i class="fa fa-eye-slash"></i> 关闭预览';
                previewBtn.classList.remove('btn-primary');
                previewBtn.classList.add('btn-secondary');
            }
        } else {
            previewPanel.style.display = 'none';
            content.classList.remove('has-preview');
            if (previewBtn) {
                previewBtn.innerHTML = '<i class="fa fa-eye"></i> 在线预览';
                previewBtn.classList.remove('btn-secondary');
                previewBtn.classList.add('btn-primary');
            }
        }
    };

    window.copyLink = function(inputId) {
        const input = document.getElementById(inputId);
        if (!input) return;
        
        const value = input.value;
        
        if (navigator.clipboard && window.isSecureContext) {
            navigator.clipboard.writeText(value).then(function() {
                showToast('链接已复制到剪贴板');
            }).catch(function() {
                fallbackCopy(input);
            });
        } else {
            fallbackCopy(input);
        }
    };

    function fallbackCopy(input) {
        input.select();
        input.setSelectionRange(0, 99999);
        
        try {
            document.execCommand('copy');
            showToast('链接已复制到剪贴板');
        } catch (err) {
            showToast('复制失败，请手动复制');
        }
        
        window.getSelection().removeAllRanges();
    }

    function showToast(message) {
        const toast = document.getElementById('toast');
        const toastMsg = document.getElementById('toastMsg');
        
        if (!toast || !toastMsg) return;
        
        toastMsg.textContent = message;
        toast.classList.add('show');
        
        setTimeout(function() {
            toast.classList.remove('show');
        }, 2500);
    }

    document.addEventListener('DOMContentLoaded', function() {
        const previewPanel = document.getElementById('previewPanel');
        const content = document.querySelector('.file-view-content');
        
        if (previewPanel && content) {
            if (previewPanel.style.display !== 'none') {
                content.classList.add('has-preview');
                previewVisible = true;
            }
        }
        
        const copyInputs = document.querySelectorAll('.link-input-group input');
        copyInputs.forEach(function(input) {
            input.addEventListener('click', function() {
                this.select();
            });
        });
    });

})();
