File size: 7,041 Bytes
47a81c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
257
258
259
260
261
/**
 * Upload functionality for AI Document Analyzer
 */

document.addEventListener('DOMContentLoaded', () => {
  // Initialize document upload
  initDocumentUpload();
  
  // Initialize image upload
  initImageUpload();
});

/**
 * Initialize document upload functionality
 */
function initDocumentUpload() {
  const dropArea = document.getElementById('document-drop-area');
  const fileInput = document.getElementById('document-file-input');
  const analyzeBtn = document.getElementById('analyze-document-btn');
  
  let selectedFile = null;
  
  // Handle drag and drop events
  ['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
    dropArea.addEventListener(eventName, (e) => {
      e.preventDefault();
      e.stopPropagation();
    });
  });
  
  // Add dragover class
  dropArea.addEventListener('dragenter', () => {
    dropArea.classList.add('dragover');
  });
  
  dropArea.addEventListener('dragover', () => {
    dropArea.classList.add('dragover');
  });
  
  // Remove dragover class
  dropArea.addEventListener('dragleave', () => {
    dropArea.classList.remove('dragover');
  });
  
  // Handle file drop
  dropArea.addEventListener('drop', (e) => {
    dropArea.classList.remove('dragover');
    const file = e.dataTransfer.files[0];
    handleDocumentFile(file);
  });
  
  // Handle click to browse
  dropArea.addEventListener('click', () => {
    fileInput.click();
  });
  
  // Handle file selection
  fileInput.addEventListener('change', () => {
    const file = fileInput.files[0];
    handleDocumentFile(file);
  });
  
  // Handle analyze button click
  analyzeBtn.addEventListener('click', () => {
    if (selectedFile) {
      const analysisType = document.querySelector('input[name="document-analysis"]:checked').value;
      uploadDocumentForAnalysis(selectedFile, analysisType);
    }
  });
  
  /**
   * Handle document file selection
   * @param {File} file - The selected file
   */
  function handleDocumentFile(file) {
    if (!file) return;
    
    // Check if file type is allowed
    const allowedTypes = '.pdf,.docx,.pptx,.xlsx,.xls';
    if (!isFileTypeAllowed(file.name, allowedTypes)) {
      showError('File type not supported. Please upload a PDF, DOCX, PPTX, or Excel file.');
      return;
    }
    
    // Update UI to show selected file
    dropArea.classList.add('file-selected');
    dropArea.innerHTML = `
      <i class="fas fa-file-alt"></i>
      <h3>${file.name}</h3>
      <p>${formatFileSize(file.size)}</p>
      <p class="file-types">Click to change file</p>
    `;
    
    // Enable analyze button
    analyzeBtn.disabled = false;
    
    // Store selected file
    selectedFile = file;
  }
  
  /**
   * Upload document for analysis
   * @param {File} file - The document file to analyze
   * @param {string} analysisType - The type of analysis to perform
   */
  function uploadDocumentForAnalysis(file, analysisType) {
    showLoading();
    
    const formData = new FormData();
    formData.append('file', file);
    formData.append('analysis_type', analysisType);
    
    fetch('/api/analyze-document', {
      method: 'POST',
      body: formData
    })
      .then(response => {
        if (!response.ok) {
          return response.json().then(data => {
            throw new Error(data.detail || 'Error analyzing document');
          });
        }
        return response.json();
      })
      .then(data => {
        hideLoading();
        displayResults(data);
      })
      .catch(error => {
        hideLoading();
        showError(error.message);
      });
  }
}

/**
 * Initialize image upload functionality
 */
function initImageUpload() {
  const dropArea = document.getElementById('image-drop-area');
  const fileInput = document.getElementById('image-file-input');
  const analyzeBtn = document.getElementById('analyze-image-btn');
  
  let selectedFile = null;
  
  // Handle drag and drop events
  ['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
    dropArea.addEventListener(eventName, (e) => {
      e.preventDefault();
      e.stopPropagation();
    });
  });
  
  // Add dragover class
  dropArea.addEventListener('dragenter', () => {
    dropArea.classList.add('dragover');
  });
  
  dropArea.addEventListener('dragover', () => {
    dropArea.classList.add('dragover');
  });
  
  // Remove dragover class
  dropArea.addEventListener('dragleave', () => {
    dropArea.classList.remove('dragover');
  });
  
  // Handle file drop
  dropArea.addEventListener('drop', (e) => {
    dropArea.classList.remove('dragover');
    const file = e.dataTransfer.files[0];
    handleImageFile(file);
  });
  
  // Handle click to browse
  dropArea.addEventListener('click', () => {
    fileInput.click();
  });
  
  // Handle file selection
  fileInput.addEventListener('change', () => {
    const file = fileInput.files[0];
    handleImageFile(file);
  });
  
  // Handle analyze button click
  analyzeBtn.addEventListener('click', () => {
    if (selectedFile) {
      const analysisType = document.querySelector('input[name="image-analysis"]:checked').value;
      uploadImageForAnalysis(selectedFile, analysisType);
    }
  });
  
  /**
   * Handle image file selection
   * @param {File} file - The selected file
   */
  function handleImageFile(file) {
    if (!file) return;
    
    // Check if file type is allowed
    const allowedTypes = '.jpg,.jpeg,.png';
    if (!isFileTypeAllowed(file.name, allowedTypes)) {
      showError('File type not supported. Please upload a JPG, JPEG, or PNG file.');
      return;
    }
    
    // Create a preview of the image
    const reader = new FileReader();
    reader.onload = function(e) {
      dropArea.classList.add('file-selected');
      dropArea.innerHTML = `
        <img src="${e.target.result}" alt="Preview" style="max-width: 100%; max-height: 200px; margin-bottom: 16px;">
        <h3>${file.name}</h3>
        <p>${formatFileSize(file.size)}</p>
        <p class="file-types">Click to change image</p>
      `;
    };
    reader.readAsDataURL(file);
    
    // Enable analyze button
    analyzeBtn.disabled = false;
    
    // Store selected file
    selectedFile = file;
  }
  
  /**
   * Upload image for analysis
   * @param {File} file - The image file to analyze
   * @param {string} analysisType - The type of analysis to perform
   */
  function uploadImageForAnalysis(file, analysisType) {
    showLoading();
    
    const formData = new FormData();
    formData.append('file', file);
    formData.append('analysis_type', analysisType);
    
    fetch('/api/analyze-image', {
      method: 'POST',
      body: formData
    })
      .then(response => {
        if (!response.ok) {
          return response.json().then(data => {
            throw new Error(data.detail || 'Error analyzing image');
          });
        }
        return response.json();
      })
      .then(data => {
        hideLoading();
        displayResults(data);
      })
      .catch(error => {
        hideLoading();
        showError(error.message);
      });
  }
}