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
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Mariam AI - Analyse d'Image</title>
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/tailwind.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/marked.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/sweetalert2.all.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/moment.min.js"></script>
    <style>
        @layer utilities {
            .markdown-content {
                @apply prose prose-sm md:prose-base lg:prose-lg mx-auto;
            }

            .markdown-content table {
                @apply w-full table-auto border-collapse my-4;
            }

            .markdown-content th,
            .markdown-content td {
                @apply border border-gray-300 p-2 text-left align-top break-words;
                min-width: auto;
                white-space: normal;
            }

            .markdown-content pre {
                @apply overflow-x-auto p-4 rounded-lg bg-gray-100;
            }
        }

        .loader {
            width: 48px;
            height: 48px;
            border: 5px solid #FFF;
            border-bottom-color: #6366F1;
            border-radius: 50%;
            display: inline-block;
            box-sizing: border-box;
            animation: rotation 1s linear infinite;
        }

        @keyframes rotation {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }

        .fade-in {
            animation: fadeIn 0.3s ease-in;
        }

        @keyframes fadeIn {
            0% { opacity: 0; }
            100% { opacity: 1; }
        }

        .upload-zone {
            @apply relative border-2 border-dashed border-indigo-500 rounded-lg p-8 transition-all duration-300 ease-in-out;
        }

        .upload-zone.drag-over {
            @apply border-indigo-600 bg-indigo-50;
        }

        .tab-button {
            @apply px-4 py-2 rounded-lg transition-all duration-200 text-sm md:text-base;
        }

        .tab-button:not(.active) {
            @apply bg-white text-indigo-600 hover:bg-indigo-50;
        }

        .tab-button.active {
            @apply bg-indigo-600 text-white;
        }

        .backup-item {
            @apply flex flex-col md:flex-row justify-between items-start md:items-center gap-2 p-4 rounded-lg border border-gray-200 hover:bg-gray-50 transition-colors duration-200;
        }

        .backup-actions {
            @apply flex gap-2 w-full md:w-auto justify-end;
        }
    </style>
</head>

<body class="min-h-screen bg-gray-50">
    <header class="bg-white shadow-sm sticky top-0 z-50">
        <div class="max-w-7xl mx-auto px-4 py-4 sm:px-6 lg:px-8">
            <h1 class="text-2xl md:text-3xl font-bold text-indigo-600">Mariam AI</h1>
            <p class="mt-1 text-sm text-gray-500">Assistant pour commentaire composé</p>
        </div>
    </header>

    <main class="max-w-7xl mx-auto px-4 py-8 sm:px-6 lg:px-8 space-y-8">
        <form id="uploadForm" class="bg-white rounded-xl shadow-sm p-6">
            <div class="upload-zone" id="dropZone">
                <input type="file" id="imageInput" accept="image/*" class="hidden" />
                <label for="imageInput" class="block text-center cursor-pointer">
                    <svg class="mx-auto h-12 w-12 text-indigo-500" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 16l4.586-4.586a2 2 0 012.828 0L16 16m-2-2l1.586-1.586a2 2 0 012.828 0L20 14m-6-6h.01M6 20h12a2 2 0 002-2V6a2 2 0 00-2-2H6a2 2 0 00-2 2v12a2 2 0 002 2z" />
                    </svg>
                    <p id="fileName" class="mt-2 text-sm text-gray-500">
                        Cliquez ou glissez une image ici
                    </p>
                </label>
            </div>

            <div id="imagePreview" class="hidden mt-6">
                <div class="relative max-w-md mx-auto">
                    <img id="preview" src="#" alt="Prévisualisation" class="rounded-lg shadow-md max-h-[400px] w-full object-contain" />
                    <button type="button" onclick="removeImage()" class="absolute top-2 right-2 p-2 rounded-full bg-white/90 hover:bg-red-500 hover:text-white transition-colors duration-200">
                        <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12" />
                        </svg>
                    </button>
                </div>
            </div>

            <div class="mt-6 flex justify-center">
                <button type="submit" class="inline-flex items-center px-6 py-3 rounded-lg bg-indigo-600 text-white font-medium hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 transition-colors duration-200">
                    Analyser l'image
                </button>
            </div>
        </form>

        <div id="loading" class="hidden">
            <div class="flex flex-col items-center justify-center gap-4">
                <span class="loader"></span>
                <p class="text-gray-500">Analyse en cours, veuillez patienter...</p>
            </div>
        </div>

        <div id="results" class="hidden space-y-6">
            <div class="flex justify-center gap-2">
                <button id="showDissertation" class="tab-button active">Dissertation</button>
                <button id="showTableau" class="tab-button">Tableau</button>
                <button id="showBackups" class="tab-button">Sauvegardes</button>
            </div>

            <div id="dissertationTab" class="tab-content active">
                <div class="bg-white rounded-xl shadow-sm p-6">
                    <h2 class="text-xl md:text-2xl font-bold text-gray-900 mb-4">Dissertation</h2>
                    <div id="dissertationResult" class="markdown-content"></div>
                    <button id="saveDissertation" class="mt-6 px-4 py-2 rounded-lg bg-green-600 text-white hover:bg-green-700 transition-colors duration-200">
                        Sauvegarder la dissertation
                    </button>
                </div>
            </div>

            <div id="tableauTab" class="tab-content hidden">
                <div class="bg-white rounded-xl shadow-sm p-6">
                    <h2 class="text-xl md:text-2xl font-bold text-gray-900 mb-4">Tableau d'analyse</h2>
                    <div id="tableauResult" class="markdown-content"></div>
                    <button id="saveTableau" class="mt-6 px-4 py-2 rounded-lg bg-green-600 text-white hover:bg-green-700 transition-colors duration-200">
                        Sauvegarder le tableau
                    </button>
                </div>
            </div>

            <div id="backupsTab" class="tab-content hidden">
                <div class="bg-white rounded-xl shadow-sm p-6">
                    <h2 class="text-xl md:text-2xl font-bold text-gray-900 mb-4">Sauvegardes locales</h2>
                    <div id="backupList" class="space-y-3"></div>
                </div>
            </div>
        </div>
    </main>

    <script>
        let tableauContent = '';
        let dissertationContent = '';
        let backups = loadBackups();

        // Gestion du drag & drop
        const dropZone = document.getElementById('dropZone');
        ['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
            dropZone.addEventListener(eventName, preventDefaults, false);
        });

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

        ['dragenter', 'dragover'].forEach(eventName => {
            dropZone.addEventListener(eventName, highlight, false);
        });

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

        function highlight(e) {
            dropZone.classList.add('drag-over');
        }

        function unhighlight(e) {
            dropZone.classList.remove('drag-over');
        }

        dropZone.addEventListener('drop', handleDrop, false);

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

            if (files && files[0]) {
                handleFiles(files);
            }
        }

        function handleFiles(files) {
            const imageInput = document.getElementById('imageInput');
            imageInput.files = files;
            handleImageSelect();
        }

        function handleImageSelect() {
            const input = document.getElementById('imageInput');
            const preview = document.getElementById('preview');
            const previewContainer = document.getElementById('imagePreview');
            const fileName = document.getElementById('fileName');

            if (input.files && input.files[0]) {
                const reader = new FileReader();

                reader.onload = function(e) {
                    preview.src = e.target.result;
                    previewContainer.classList.remove('hidden');
                    previewContainer.classList.add('fade-in');
                    fileName.textContent = input.files[0].name;
                };

                reader.readAsDataURL(input.files[0]);
            }
        }

        function removeImage() {
            const input = document.getElementById('imageInput');
            const previewContainer = document.getElementById('imagePreview');
            const fileName = document.getElementById('fileName');

            input.value = '';
            previewContainer.classList.add('hidden');
            fileName.textContent = 'Cliquez ou glissez une image ici';
        }

        // Gestion des onglets
        document.querySelectorAll('.tab-button').forEach(button => {
            button.addEventListener('click', () => {
                const tabName = button.id.replace('show', '').toLowerCase();
                showTab(tabName);
            });
        });

        function showTab(tabName) {
            document.querySelectorAll('.tab-content').forEach(tab => tab.classList.add('hidden'));
            document.querySelectorAll('.tab-button').forEach(btn => btn.classList.remove('active'));

            const targetTab = document.getElementById(`${tabName}Tab`);
            const targetButton = document.getElementById(`show${tabName.charAt(0).toUpperCase() + tabName.slice(1)}`);

            targetTab.classList.remove('hidden');
            targetButton.classList.add('active');

            if (tabName === 'tableau') {
                renderTableauContent();
            } else if (tabName === 'backups') {
                updateBackupList();
            }
        }

        // Gestion des sauvegardes
        function loadBackups() {
            try {
                return JSON.parse(localStorage.getItem('mariamAIBackups')) || [];
            } catch {
                return [];
            }
        }

        function saveBackup(type, content) {
            const timestamp = moment().format('YYYY-MM-DD HH:mm:ss');
            backups.unshift({ timestamp, type, content });
            localStorage.setItem('mariamAIBackups', JSON.stringify(backups));
            updateBackupList();
            Swal.fire({
                icon: 'success',
                title: 'Sauvegarde réussie!',
                toast: true,
                position: 'top-end',
                showConfirmButton: false,
                timer: 3000
            });
        }

        function updateBackupList() {
            const backupList = document.getElementById('backupList');
            backupList.innerHTML = backups.length ? '' : '<p class="text-gray-500 text-center py-4">Aucune sauvegarde disponible</p>';

            backups.forEach((backup, index) => {
                const div = document.createElement('div');
                div.className = 'backup-item';
                div.innerHTML = `
                    <div class="flex-1">
                        <h3 class="font-medium">${backup.type === 'dissertation' ? 'Dissertation' : 'Tableau'}</h3>
                        <p class="text-sm text-gray-500">${backup.timestamp}</p>
                    </div>
                    <div class="backup-actions">
                        <button data-index="${index}" class="view-backup px-3 py-1 rounded-lg bg-blue-500 text-white text-sm hover:bg-blue-600">
                            Voir
                        </button>
                        <button data-index="${index}" class="delete-backup px-3 py-1 rounded-lg bg-red-500 text-white text-sm hover:bg-red-600">
                            Supprimer
                        </button>
                    </div>
                `;
                backupList.appendChild(div);
            });

            document.querySelectorAll('.view-backup').forEach(btn => 
                btn.addEventListener('click', viewBackup));
            document.querySelectorAll('.delete-backup').forEach(btn => 
                btn.addEventListener('click', deleteBackup));
        }

        function viewBackup(event) {
            const backup = backups[event.target.dataset.index];
            Swal.fire({
                title: backup.type === 'dissertation' ? 'Dissertation sauvegardée' : 'Tableau sauvegardé',
                html: marked.parse(backup.content),
                width: '90%',
                customClass: {
                    popup: 'swal2-popup-large',
                    htmlContainer: 'markdown-content px-4'
                },
                showCloseButton: true,
                showConfirmButton: false
            });
        }

        function deleteBackup(event) {
            const index = event.target.dataset.index;
            const backup = backups[index];
            
            Swal.fire({
                title: 'Confirmer la suppression',
                text: `Voulez-vous vraiment supprimer cette sauvegarde du ${backup.timestamp} ?`,
                icon: 'warning',
                showCancelButton: true,
                confirmButtonColor: '#EF4444',
                cancelButtonColor: '#6B7280',
                confirmButtonText: 'Supprimer',
                cancelButtonText: 'Annuler'
            }).then((result) => {
                if (result.isConfirmed) {
                    backups.splice(index, 1);
                    localStorage.setItem('mariamAIBackups', JSON.stringify(backups));
                    updateBackupList();
                    
                    Swal.fire({
                        title: 'Supprimé!',
                        text: 'La sauvegarde a été supprimée.',
                        icon: 'success',
                        toast: true,
                        position: 'top-end',
                        showConfirmButton: false,
                        timer: 3000
                    });
                }
            });
        }

        // Gestion des boutons de sauvegarde
        document.getElementById('saveDissertation').addEventListener('click', () => {
            if (!dissertationContent.trim()) {
                Swal.fire({
                    icon: 'error',
                    title: 'Erreur',
                    text: 'Aucun contenu à sauvegarder'
                });
                return;
            }
            saveBackup('dissertation', dissertationContent);
        });

        document.getElementById('saveTableau').addEventListener('click', () => {
            if (!tableauContent.trim()) {
                Swal.fire({
                    icon: 'error',
                    title: 'Erreur',
                    text: 'Aucun contenu à sauvegarder'
                });
                return;
            }
            saveBackup('tableau', tableauContent);
        });

        // Gestion du formulaire d'upload
        document.getElementById('uploadForm').addEventListener('submit', async (e) => {
            e.preventDefault();

            const imageInput = document.getElementById('imageInput');
            if (!imageInput.files || !imageInput.files[0]) {
                Swal.fire({
                    icon: 'error',
                    title: 'Aucune image sélectionnée',
                    text: 'Veuillez sélectionner une image à analyser.'
                });
                return;
            }

            const loading = document.getElementById('loading');
            const results = document.getElementById('results');
            const dissertationResult = document.getElementById('dissertationResult');

            const formData = new FormData();
            formData.append('image', imageInput.files[0]);

            loading.classList.remove('hidden');
            results.classList.add('hidden');

            try {
                const response = await fetch('/analyze', {
                    method: 'POST',
                    body: formData
                });

                if (!response.ok) {
                    throw new Error(`Erreur serveur: ${response.status}`);
                }

                const data = await response.json();
                
                dissertationContent = data.dissertation;
                tableauContent = data.tableau;

                // Configuration de marked pour le rendu Markdown
                marked.setOptions({
                    breaks: true,
                    gfm: true,
                    tables: true,
                    smartLists: true,
                    smartypants: true
                });

                dissertationResult.innerHTML = marked.parse(data.dissertation);

                results.classList.remove('hidden');
                results.classList.add('fade-in');
                
                // Scroll vers les résultats
                results.scrollIntoView({ behavior: 'smooth', block: 'start' });

            } catch (error) {
                console.error("Erreur lors de l'analyse:", error);
                Swal.fire({
                    icon: 'error',
                    title: 'Erreur',
                    text: "Une erreur est survenue lors de l'analyse de l'image. Veuillez réessayer."
                });
            } finally {
                loading.classList.add('hidden');
            }
        });

        function renderTableauContent() {
            const tableauResult = document.getElementById('tableauResult');
            if (tableauContent) {
                tableauResult.innerHTML = marked.parse(tableauContent);
            }
        }

        // Initialisation
        document.getElementById('imageInput').addEventListener('change', handleImageSelect);
        updateBackupList();
    </script>
</body>
</html>