File size: 9,044 Bytes
8472119
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65c4ece
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8472119
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
document.addEventListener('DOMContentLoaded', function() {
    // File upload handling
    const dropArea = document.getElementById('drop-area');
    const fileInput = document.getElementById('file-input');
    const filePreview = document.getElementById('file-preview');
    const uploadForm = document.getElementById('upload-form');

    // Only initialize if elements exist
    if (!dropArea || !fileInput || !filePreview || !uploadForm) {
        console.warn('Some upload elements not found, skipping initialization');
        return;
    }

    // Prevent default drag behaviors
    ['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
        dropArea.addEventListener(eventName, preventDefaults, false);
        document.body.addEventListener(eventName, preventDefaults, false);
    });

    // Highlight drop area when item is dragged over it
    ['dragenter', 'dragover'].forEach(eventName => {
        dropArea.addEventListener(eventName, highlight, false);
    });

    ['dragleave', 'drop'].forEach(eventName => {
        dropArea.addEventListener(eventName, unhighlight, false);
    });

    // Handle dropped files
    dropArea.addEventListener('drop', handleDrop, false);

    // Click to upload functionality
    dropArea.addEventListener('click', function(e) {
        // Prevent triggering if clicking the button itself
        if (e.target.tagName !== 'BUTTON' && e.target.tagName !== 'I') {
            fileInput.click();
        }
    });

    // Handle file input change
    fileInput.addEventListener('change', function(e) {
        handleFiles(e.target.files);
    });

    function preventDefaults(e) {
        e.preventDefault();
        e.stopPropagation();
    }

    function highlight() {
        dropArea.style.borderColor = 'var(--primary-color)';
        dropArea.style.backgroundColor = 'rgba(139, 92, 246, 0.05)';
        dropArea.classList.add('dragover');
    }

    function unhighlight() {
        dropArea.style.borderColor = 'var(--border-color)';
        dropArea.style.backgroundColor = 'var(--background-light)';
        dropArea.classList.remove('dragover');
    }

    function handleDrop(e) {
        const dt = e.dataTransfer;
        const files = dt.files;
        handleFiles(files);
    }

    function handleFiles(files) {
        if (files.length > 0) {
            const file = files[0];
            
            // Validate file type
            const allowedTypes = ['application/pdf', 'application/msword', 
                'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
                'application/vnd.ms-powerpoint',
                'application/vnd.openxmlformats-officedocument.presentationml.presentation',
                'text/plain'];
            
            const allowedExtensions = ['.pdf', '.doc', '.docx', '.ppt', '.pptx', '.txt'];
            const fileExtension = '.' + file.name.split('.').pop().toLowerCase();
            
            if (!allowedTypes.includes(file.type) && !allowedExtensions.includes(fileExtension)) {
                alert('Please select a valid file type (PDF, Word, PowerPoint, or Text)');
                return;
            }
            
            // Check file size (50MB limit)
            if (file.size > 50 * 1024 * 1024) {
                alert('File size must be less than 50MB');
                return;
            }
            
            showFilePreview(file);
            
            // Auto-fill title if empty
            const titleInput = document.getElementById('title');
            if (titleInput && !titleInput.value) {
                titleInput.value = file.name.replace(/\.[^/.]+$/, "");
            }
        }
    }

    function showFilePreview(file) {
        const fileIcon = document.getElementById('file-icon');
        const fileName = document.getElementById('file-name');
        const fileSize = document.getElementById('file-size');
        
        if (!fileIcon || !fileName || !fileSize) {
            console.warn('File preview elements not found');
            return;
        }
        
        // Get file extension
        const extension = file.name.split('.').pop().toLowerCase();
        
        // Set icon and color based on file type
        let iconClass = 'fas fa-file';
        let iconColor = '#6b7280';
        
        switch(extension) {
            case 'pdf':
                iconClass = 'fas fa-file-pdf';
                iconColor = '#ef4444';
                break;
            case 'doc':
            case 'docx':
                iconClass = 'fas fa-file-word';
                iconColor = '#3b82f6';
                break;
            case 'ppt':
            case 'pptx':
                iconClass = 'fas fa-file-powerpoint';
                iconColor = '#f59e0b';
                break;
            case 'txt':
                iconClass = 'fas fa-file-alt';
                iconColor = 'var(--primary-color)';
                break;
        }
        
        fileIcon.innerHTML = `<i class="${iconClass}"></i>`;
        fileIcon.style.background = iconColor;
        fileName.textContent = file.name;
        fileSize.textContent = formatFileSize(file.size);
        
        filePreview.style.display = 'block';
    }

    // Make removeFile global so it can be called from onclick
    window.removeFile = function() {
        fileInput.value = '';
        filePreview.style.display = 'none';
        
        // Clear the title if it was auto-filled
        const titleInput = document.getElementById('title');
        if (titleInput) {
            titleInput.value = '';
        }
    };

    function formatFileSize(bytes) {
        if (bytes === 0) return '0 Bytes';
        const k = 1024;
        const sizes = ['Bytes', 'KB', 'MB', 'GB'];
        const i = Math.floor(Math.log(bytes) / Math.log(k));
        return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
    }

    // Form submission handling
    uploadForm.addEventListener('submit', function(e) {
        const uploadBtn = document.getElementById('upload-btn');
        const fileInputValue = fileInput.value;
        
        // Check if file is selected
        if (!fileInputValue) {
            e.preventDefault();
            alert('Please select a file to upload');
            return false;
        }
        
        // Show loading state
        const originalText = uploadBtn.innerHTML;
        uploadBtn.innerHTML = '<i class="fas fa-spinner fa-spin me-2"></i>Processing...';
        uploadBtn.disabled = true;
        
        // Re-enable after timeout (failsafe)
        setTimeout(() => {
            uploadBtn.innerHTML = originalText;
            uploadBtn.disabled = false;
        }, 30000);
        
        // Form will submit normally (not via AJAX)
    });

    // Initialize any existing file in the input
    if (fileInput.files && fileInput.files.length > 0) {
        handleFiles(fileInput.files);
    }
});

// Processing mode selection functionality
function selectProcessingMode(mode) {
    // Remove active class from all options
    document.querySelectorAll('.processing-option').forEach(option => {
        option.style.border = '2px solid var(--border-color)';
        option.style.transform = 'none';
        option.style.boxShadow = 'none';
    });
    
    // Select the radio button
    const radioButton = document.getElementById(mode + '_mode');
    if (radioButton) {
        radioButton.checked = true;
        
        // Add active styling to selected option
        const selectedOption = radioButton.closest('.processing-option');
        if (mode === 'fast') {
            selectedOption.style.border = '2px solid #28a745';
            selectedOption.style.boxShadow = '0 4px 15px rgba(40, 167, 69, 0.2)';
        } else {
            selectedOption.style.border = '2px solid #6f42c1';
            selectedOption.style.boxShadow = '0 4px 15px rgba(111, 66, 193, 0.2)';
        }
        selectedOption.style.transform = 'translateY(-2px)';
        
        // Update submit button text
        updateSubmitButtonText(mode);
    }
}

function updateSubmitButtonText(mode) {
    const uploadBtn = document.getElementById('upload-btn');
    if (uploadBtn) {
        if (mode === 'fast') {
            uploadBtn.innerHTML = '<i class="fas fa-bolt me-2"></i>Upload & Process (Fast)';
        } else {
            uploadBtn.innerHTML = '<i class="fas fa-eye me-2"></i>Upload & Process (Advanced OCR)';
        }
    }
}

// Initialize processing mode styling on page load
document.addEventListener('DOMContentLoaded', function() {
    // Set initial styling for fast mode (default)
    selectProcessingMode('fast');
    
    // Add click handlers for processing options
    document.querySelectorAll('.processing-option').forEach(option => {
        option.addEventListener('click', function() {
            const radioButton = this.querySelector('input[type="radio"]');
            if (radioButton) {
                selectProcessingMode(radioButton.value);
            }
        });
    });
});