File size: 25,912 Bytes
a1be761
 
 
 
 
74828dc
a1be761
 
74828dc
a1be761
 
 
74828dc
 
 
 
 
 
 
 
 
a1be761
 
 
 
 
 
 
 
74828dc
 
a1be761
 
 
 
 
 
 
74828dc
a1be761
 
 
74828dc
 
a1be761
74828dc
 
a1be761
74828dc
 
a1be761
74828dc
 
 
a1be761
 
 
 
 
 
 
74828dc
 
a1be761
74828dc
a1be761
 
74828dc
a1be761
 
 
 
74828dc
a1be761
74828dc
 
 
 
a1be761
74828dc
a1be761
 
 
74828dc
a1be761
 
 
 
 
 
 
 
 
74828dc
a1be761
 
 
 
74828dc
a1be761
74828dc
a1be761
 
 
 
 
 
 
74828dc
a1be761
74828dc
a1be761
 
 
 
 
 
74828dc
a1be761
74828dc
 
 
a1be761
74828dc
 
 
a1be761
74828dc
 
 
 
 
 
 
a1be761
 
 
74828dc
 
a1be761
 
 
 
74828dc
 
a1be761
 
74828dc
 
a1be761
 
 
74828dc
 
 
 
 
 
 
 
 
 
 
 
 
a1be761
 
74828dc
 
 
 
a1be761
 
 
74828dc
a1be761
74828dc
a1be761
 
 
 
 
 
74828dc
a1be761
74828dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a1be761
 
 
74828dc
 
a1be761
 
 
 
74828dc
 
a1be761
 
74828dc
 
a1be761
 
 
 
74828dc
 
a1be761
74828dc
 
 
 
 
a1be761
 
 
 
 
74828dc
a1be761
74828dc
 
 
a1be761
 
74828dc
 
 
 
 
 
a1be761
74828dc
 
a1be761
74828dc
 
 
 
 
 
a1be761
 
 
 
 
 
 
 
 
74828dc
a1be761
 
 
 
74828dc
 
 
 
 
 
 
 
 
 
 
a1be761
 
 
74828dc
 
 
 
 
a1be761
 
 
74828dc
 
a1be761
 
74828dc
a1be761
 
74828dc
 
a1be761
 
 
74828dc
 
 
 
 
 
 
 
 
 
 
a1be761
74828dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a1be761
 
 
 
 
 
 
 
 
74828dc
 
 
 
 
a1be761
74828dc
a1be761
 
 
74828dc
a1be761
 
74828dc
a1be761
 
 
 
74828dc
 
 
a1be761
74828dc
 
 
a1be761
 
74828dc
a1be761
74828dc
a1be761
 
74828dc
a1be761
 
 
74828dc
 
 
 
 
 
 
 
a1be761
 
 
 
 
74828dc
 
a1be761
74828dc
 
 
 
a1be761
74828dc
a1be761
 
74828dc
 
 
 
a1be761
 
74828dc
 
 
 
 
 
a1be761
 
74828dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a1be761
74828dc
a1be761
 
74828dc
a1be761
74828dc
a1be761
 
 
 
 
 
 
 
74828dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a1be761
 
 
74828dc
 
 
 
a1be761
74828dc
 
 
 
 
 
 
 
 
 
 
a1be761
74828dc
 
 
 
a1be761
74828dc
 
a1be761
74828dc
a1be761
74828dc
 
 
a1be761
 
 
74828dc
a1be761
74828dc
 
 
 
a1be761
 
 
 
74828dc
a1be761
 
74828dc
a1be761
74828dc
a1be761
74828dc
 
 
 
 
 
 
 
 
 
 
a1be761
 
 
 
 
74828dc
a1be761
74828dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7b84ca6
74828dc
a1be761
74828dc
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
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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
// Admin JavaScript for the backend management interface

document.addEventListener('DOMContentLoaded', function() {
    // Initialize theme
    initTheme();
    
    // Setup dashboard functionality
    setupDashboardCards();
    
    // Setup admin forms
    setupMatiereForm();
    setupSousCategorieForm();
    setupTexteForm();
    
    // Setup content block editor
    setupContentBlockEditor();
    
    // Setup image management
    setupImageUploader();
    setupImageGallery();
    
    // Setup theme toggle
    setupThemeToggle();
});

// Initialize theme based on user preference
function initTheme() {
    const userPreference = localStorage.getItem('theme') || 'light';
    document.documentElement.setAttribute('data-theme', userPreference);
    
    // Update theme icon
    updateThemeIcon(userPreference);
}

// Setup theme toggle functionality
function setupThemeToggle() {
    const themeToggle = document.getElementById('theme-toggle');
    if (!themeToggle) return;
    
    themeToggle.addEventListener('click', function() {
        const currentTheme = document.documentElement.getAttribute('data-theme');
        const newTheme = currentTheme === 'dark' ? 'light' : 'dark';
        
        // Update theme attribute
        document.documentElement.setAttribute('data-theme', newTheme);
        
        // Save preference to localStorage
        localStorage.setItem('theme', newTheme);
        
        // Update icon
        updateThemeIcon(newTheme);
        
        // Send theme preference to server
        saveThemePreference(newTheme);
    });
}

// Update the theme toggle icon based on current theme
function updateThemeIcon(theme) {
    const themeToggle = document.getElementById('theme-toggle');
    if (!themeToggle) return;
    
    // Update icon based on theme
    if (theme === 'dark') {
        themeToggle.innerHTML = '<i class="fas fa-sun"></i>';
        themeToggle.setAttribute('title', 'Activer le mode clair');
    } else {
        themeToggle.innerHTML = '<i class="fas fa-moon"></i>';
        themeToggle.setAttribute('title', 'Activer le mode sombre');
    }
}

// Save theme preference to server
function saveThemePreference(theme) {
    const formData = new FormData();
    formData.append('theme', theme);
    
    fetch('/set_theme', {
        method: 'POST',
        body: formData
    })
    .then(response => response.json())
    .then(data => {
        console.log('Theme preference saved:', data);
    })
    .catch(error => {
        console.error('Error saving theme preference:', error);
    });
}

// Setup dashboard cards with hover effects
function setupDashboardCards() {
    const dashboardCards = document.querySelectorAll('.admin-card');
    
    dashboardCards.forEach(card => {
        card.addEventListener('mouseenter', function() {
            this.style.transform = 'translateY(-5px)';
            this.style.boxShadow = 'var(--hover-shadow)';
            this.style.transition = 'transform 0.3s ease, box-shadow 0.3s ease';
        });
        
        card.addEventListener('mouseleave', function() {
            this.style.transform = 'translateY(0)';
            this.style.boxShadow = 'var(--shadow)';
        });
    });
}

// Setup matiere form functionality
function setupMatiereForm() {
    // Show edit form when edit button is clicked
    const editButtons = document.querySelectorAll('.edit-matiere-btn');
    editButtons.forEach(button => {
        button.addEventListener('click', function() {
            const matiereId = this.getAttribute('data-id');
            const matiereName = this.getAttribute('data-name');
            const matiereColor = this.getAttribute('data-color');
            
            const editForm = document.getElementById('edit-matiere-form');
            if (editForm) {
                const idInput = editForm.querySelector('input[name="matiere_id"]');
                const nameInput = editForm.querySelector('input[name="nom"]');
                const colorInput = editForm.querySelector('input[name="color_code"]');
                
                idInput.value = matiereId;
                nameInput.value = matiereName;
                colorInput.value = matiereColor;
                
                // Show the edit form
                document.getElementById('add-matiere-section').classList.add('d-none');
                document.getElementById('edit-matiere-section').classList.remove('d-none');
                
                // Scroll to edit form
                editForm.scrollIntoView({ behavior: 'smooth' });
            }
        });
    });
    
    // Cancel edit button
    const cancelEditButton = document.getElementById('cancel-edit-matiere');
    if (cancelEditButton) {
        cancelEditButton.addEventListener('click', function(e) {
            e.preventDefault();
            document.getElementById('add-matiere-section').classList.remove('d-none');
            document.getElementById('edit-matiere-section').classList.add('d-none');
        });
    }
    
    // Color picker preview
    const colorPickers = document.querySelectorAll('input[type="color"]');
    colorPickers.forEach(picker => {
        picker.addEventListener('input', function() {
            // Find adjacent preview element or create one
            let preview = this.nextElementSibling;
            if (!preview || !preview.classList.contains('color-preview')) {
                preview = document.createElement('span');
                preview.className = 'color-preview';
                preview.style.display = 'inline-block';
                preview.style.width = '24px';
                preview.style.height = '24px';
                preview.style.borderRadius = '50%';
                preview.style.marginLeft = '10px';
                this.parentNode.insertBefore(preview, this.nextSibling);
            }
            
            preview.style.backgroundColor = this.value;
        });
        
        // Trigger once to initialize
        const event = new Event('input');
        picker.dispatchEvent(event);
    });
}

// Setup sous categorie form functionality
function setupSousCategorieForm() {
    // Show edit form when edit button is clicked
    const editButtons = document.querySelectorAll('.edit-sous-categorie-btn');
    editButtons.forEach(button => {
        button.addEventListener('click', function() {
            const sousCategorieId = this.getAttribute('data-id');
            const sousCategorieName = this.getAttribute('data-name');
            const matiereId = this.getAttribute('data-matiere-id');
            
            const editForm = document.getElementById('edit-sous-categorie-form');
            if (editForm) {
                const idInput = editForm.querySelector('input[name="sous_categorie_id"]');
                const nameInput = editForm.querySelector('input[name="nom"]');
                const matiereSelect = editForm.querySelector('select[name="matiere_id"]');
                
                idInput.value = sousCategorieId;
                nameInput.value = sousCategorieName;
                matiereSelect.value = matiereId;
                
                // Show the edit form
                document.getElementById('add-sous-categorie-section').classList.add('d-none');
                document.getElementById('edit-sous-categorie-section').classList.remove('d-none');
                
                // Scroll to edit form
                editForm.scrollIntoView({ behavior: 'smooth' });
            }
        });
    });
    
    // Cancel edit button
    const cancelEditButton = document.getElementById('cancel-edit-sous-categorie');
    if (cancelEditButton) {
        cancelEditButton.addEventListener('click', function(e) {
            e.preventDefault();
            document.getElementById('add-sous-categorie-section').classList.remove('d-none');
            document.getElementById('edit-sous-categorie-section').classList.add('d-none');
        });
    }
    
    // Matiere select filter
    const matiereFilterSelect = document.getElementById('matiere-filter');
    if (matiereFilterSelect) {
        matiereFilterSelect.addEventListener('change', function() {
            const selectedMatiereId = this.value;
            const sousCategorieRows = document.querySelectorAll('.sous-categorie-row');
            
            sousCategorieRows.forEach(row => {
                if (selectedMatiereId === '' || row.getAttribute('data-matiere-id') === selectedMatiereId) {
                    row.style.display = '';
                } else {
                    row.style.display = 'none';
                }
            });
        });
    }
}

// Setup texte form functionality
function setupTexteForm() {
    // Matiere select change - populate sous-categories
    const matiereSelect = document.getElementById('matiere-select');
    if (matiereSelect) {
        matiereSelect.addEventListener('change', function() {
            const matiereId = this.value;
            const sousCategorieSelect = document.getElementById('sous-categorie-select');
            
            if (matiereId && sousCategorieSelect) {
                // Clear current options
                sousCategorieSelect.innerHTML = '<option value="">Sélectionnez une sous-catégorie</option>';
                
                // Fetch sous-categories for the selected matiere
                fetch(`/get_sous_categories/${matiereId}`)
                    .then(response => response.json())
                    .then(data => {
                        data.forEach(sousCategorie => {
                            const option = document.createElement('option');
                            option.value = sousCategorie.id;
                            option.textContent = sousCategorie.nom;
                            sousCategorieSelect.appendChild(option);
                        });
                    })
                    .catch(error => {
                        console.error('Error loading sous-categories:', error);
                    });
            }
        });
    }
}

// Setup content block editor
function setupContentBlockEditor() {
    const blocksContainer = document.getElementById('blocks-container');
    const addBlockButton = document.getElementById('add-block-button');
    const saveBlocksButton = document.getElementById('save-blocks-button');
    
    if (!blocksContainer) return;
    
    // Add new block
    if (addBlockButton) {
        addBlockButton.addEventListener('click', function() {
            addContentBlock();
        });
    }
    
    // Make blocks sortable
    if (window.Sortable) {
        new Sortable(blocksContainer, {
            animation: 150,
            handle: '.block-handle',
            ghostClass: 'block-ghost',
            onEnd: function() {
                // Update order numbers
                updateBlockOrder();
            }
        });
    }
    
    // Save blocks
    if (saveBlocksButton) {
        saveBlocksButton.addEventListener('click', function() {
            saveContentBlocks();
        });
    }
    
    // Add event listeners for existing blocks
    setupExistingBlockControls();
}

// Setup controls for existing blocks
function setupExistingBlockControls() {
    // Setup delete buttons
    const deleteButtons = document.querySelectorAll('.delete-block-btn');
    deleteButtons.forEach(button => {
        button.addEventListener('click', function() {
            if (confirm('Êtes-vous sûr de vouloir supprimer ce bloc ?')) {
                const blockEditor = this.closest('.block-editor');
                blockEditor.remove();
                updateBlockOrder();
            }
        });
    });
    
    // Setup image position selects
    const positionSelects = document.querySelectorAll('.image-position-select');
    positionSelects.forEach(select => {
        select.addEventListener('change', function() {
            updateBlockImagePreview(this.closest('.block-editor'));
        });
    });
    
    // Setup image selection buttons
    const imageSelectButtons = document.querySelectorAll('.select-image-btn');
    imageSelectButtons.forEach(button => {
        button.addEventListener('click', function() {
            const blockEditor = this.closest('.block-editor');
            const galleryModal = document.getElementById('image-gallery-modal');
            
            if (galleryModal) {
                // Set current block ID as data attribute for the modal
                galleryModal.setAttribute('data-target-block', blockEditor.getAttribute('data-block-id'));
                
                // Show the modal
                const modal = new bootstrap.Modal(galleryModal);
                modal.show();
            }
        });
    });
    
    // Setup image remove buttons
    const removeImageButtons = document.querySelectorAll('.remove-image-btn');
    removeImageButtons.forEach(button => {
        button.addEventListener('click', function() {
            const blockEditor = this.closest('.block-editor');
            const imageIdInput = blockEditor.querySelector('.block-image-id');
            const imagePreview = blockEditor.querySelector('.image-preview');
            
            if (imageIdInput) {
                imageIdInput.value = '';
            }
            
            if (imagePreview) {
                imagePreview.src = '';
                imagePreview.style.display = 'none';
            }
            
            // Hide remove button
            this.style.display = 'none';
            
            // Show select button
            const selectButton = blockEditor.querySelector('.select-image-btn');
            if (selectButton) {
                selectButton.style.display = 'inline-block';
            }
        });
    });
}

// Add a new content block to the editor
function addContentBlock(data = null) {
    const blocksContainer = document.getElementById('blocks-container');
    if (!blocksContainer) return;
    
    // Generate a unique ID for the block
    const blockId = 'block-' + Date.now();
    
    // Create block HTML
    const blockHtml = `
        <div class="block-editor" data-block-id="${blockId}">
            <div class="block-editor-header">
                <div class="d-flex align-items-center">
                    <span class="block-handle"><i class="fas fa-grip-vertical"></i></span>
                    <h3 class="block-editor-title">Bloc #${blocksContainer.children.length + 1}</h3>
                </div>
                <div class="block-editor-actions">
                    <button type="button" class="btn btn-danger btn-sm delete-block-btn">
                        <i class="fas fa-trash"></i>
                    </button>
                </div>
            </div>
            <div class="form-group">
                <label for="${blockId}-title">Titre du bloc (optionnel)</label>
                <input type="text" class="form-control block-title" id="${blockId}-title" value="${data?.title || ''}">
            </div>
            <div class="form-group">
                <label for="${blockId}-content">Contenu du bloc</label>
                <textarea class="form-control block-content" id="${blockId}-content" rows="5">${data?.content || ''}</textarea>
            </div>
            <div class="form-group">
                <label>Image</label>
                <div class="d-flex align-items-center mb-2">
                    <button type="button" class="btn btn-primary btn-sm select-image-btn" style="${data?.image ? 'display:none;' : ''}">
                        <i class="fas fa-image"></i> Sélectionner une image
                    </button>
                    <button type="button" class="btn btn-warning btn-sm remove-image-btn ml-2" style="${data?.image ? '' : 'display:none;'}">
                        <i class="fas fa-times"></i> Retirer l'image
                    </button>
                </div>
                <input type="hidden" class="block-image-id" value="${data?.image?.id || ''}">
                <img src="${data?.image?.src || ''}" alt="Preview" class="image-preview mb-2" style="${data?.image ? '' : 'display:none;'}">
                <div class="form-group">
                    <label for="${blockId}-image-position">Position de l'image</label>
                    <select class="form-control image-position-select" id="${blockId}-image-position">
                        <option value="left" ${data?.image_position === 'left' ? 'selected' : ''}>Gauche</option>
                        <option value="right" ${data?.image_position === 'right' ? 'selected' : ''}>Droite</option>
                        <option value="top" ${data?.image_position === 'top' ? 'selected' : ''}>Haut</option>
                    </select>
                </div>
            </div>
        </div>
    `;
    
    // Add the block to the container
    blocksContainer.insertAdjacentHTML('beforeend', blockHtml);
    
    // Setup event listeners for the new block
    const newBlock = blocksContainer.lastElementChild;
    
    // Delete button
    const deleteButton = newBlock.querySelector('.delete-block-btn');
    if (deleteButton) {
        deleteButton.addEventListener('click', function() {
            if (confirm('Êtes-vous sûr de vouloir supprimer ce bloc ?')) {
                newBlock.remove();
                updateBlockOrder();
            }
        });
    }
    
    // Image position select
    const positionSelect = newBlock.querySelector('.image-position-select');
    if (positionSelect) {
        positionSelect.addEventListener('change', function() {
            updateBlockImagePreview(newBlock);
        });
    }
    
    // Image selection button
    const imageSelectButton = newBlock.querySelector('.select-image-btn');
    if (imageSelectButton) {
        imageSelectButton.addEventListener('click', function() {
            const galleryModal = document.getElementById('image-gallery-modal');
            
            if (galleryModal) {
                // Set current block ID as data attribute for the modal
                galleryModal.setAttribute('data-target-block', blockId);
                
                // Show the modal
                const modal = new bootstrap.Modal(galleryModal);
                modal.show();
            }
        });
    }
    
    // Image remove button
    const removeImageButton = newBlock.querySelector('.remove-image-btn');
    if (removeImageButton) {
        removeImageButton.addEventListener('click', function() {
            const imageIdInput = newBlock.querySelector('.block-image-id');
            const imagePreview = newBlock.querySelector('.image-preview');
            
            if (imageIdInput) {
                imageIdInput.value = '';
            }
            
            if (imagePreview) {
                imagePreview.src = '';
                imagePreview.style.display = 'none';
            }
            
            // Hide remove button
            removeImageButton.style.display = 'none';
            
            // Show select button
            if (imageSelectButton) {
                imageSelectButton.style.display = 'inline-block';
            }
        });
    }
    
    // Scroll to the new block
    newBlock.scrollIntoView({ behavior: 'smooth' });
}

// Update block order numbers in the UI
function updateBlockOrder() {
    const blocks = document.querySelectorAll('.block-editor');
    blocks.forEach((block, index) => {
        const titleEl = block.querySelector('.block-editor-title');
        if (titleEl) {
            titleEl.textContent = `Bloc #${index + 1}`;
        }
    });
}

// Update image preview based on position
function updateBlockImagePreview(blockEditor) {
    // This function would apply CSS classes to show how the image position 
    // will look in the frontend
    const positionSelect = blockEditor.querySelector('.image-position-select');
    const imagePreview = blockEditor.querySelector('.image-preview');
    
    if (!positionSelect || !imagePreview || imagePreview.style.display === 'none') {
        return;
    }
    
    const position = positionSelect.value;
    
    // Remove existing position classes
    imagePreview.classList.remove('position-left', 'position-right', 'position-top');
    
    // Add the selected position class
    imagePreview.classList.add(`position-${position}`);
    
    // Apply some simple styling to demonstrate the position
    switch (position) {
        case 'left':
            imagePreview.style.float = 'left';
            imagePreview.style.marginRight = '15px';
            imagePreview.style.marginBottom = '10px';
            imagePreview.style.width = '30%';
            break;
        case 'right':
            imagePreview.style.float = 'right';
            imagePreview.style.marginLeft = '15px';
            imagePreview.style.marginBottom = '10px';
            imagePreview.style.width = '30%';
            break;
        case 'top':
            imagePreview.style.float = 'none';
            imagePreview.style.marginRight = '0';
            imagePreview.style.marginLeft = '0';
            imagePreview.style.marginBottom = '15px';
            imagePreview.style.width = '100%';
            break;
    }
}

// Save content blocks
function saveContentBlocks() {
    const blocksContainer = document.getElementById('blocks-container');
    const blocksDataInput = document.getElementById('blocks-data');
    
    if (!blocksContainer || !blocksDataInput) return;
    
    const blocks = blocksContainer.querySelectorAll('.block-editor');
    const blocksData = [];
    
    blocks.forEach((block, index) => {
        const blockId = block.getAttribute('data-block-id');
        const title = block.querySelector('.block-title').value;
        const content = block.querySelector('.block-content').value;
        const imageId = block.querySelector('.block-image-id').value;
        const imagePosition = block.querySelector('.image-position-select').value;
        
        blocksData.push({
            id: blockId,
            title: title,
            content: content,
            image_id: imageId,
            image_position: imagePosition,
            order: index
        });
    });
    
    // Set the blocks data as JSON in the hidden input
    blocksDataInput.value = JSON.stringify(blocksData);
    
    // Submit the form
    const form = document.getElementById('blocks-form');
    if (form) {
        form.submit();
    }
}

// Setup image uploader
function setupImageUploader() {
    const imageUploadForm = document.getElementById('image-upload-form');
    const imageFileInput = document.getElementById('image-file');
    const imagePreview = document.getElementById('upload-image-preview');
    
    if (imageFileInput && imagePreview) {
        imageFileInput.addEventListener('change', function() {
            if (this.files && this.files[0]) {
                const reader = new FileReader();
                
                reader.onload = function(e) {
                    imagePreview.src = e.target.result;
                    imagePreview.style.display = 'block';
                };
                
                reader.readAsDataURL(this.files[0]);
            }
        });
    }
    
    if (imageUploadForm) {
        imageUploadForm.addEventListener('submit', function(e) {
            const fileInput = this.querySelector('#image-file');
            
            if (!fileInput.files || fileInput.files.length === 0) {
                e.preventDefault();
                alert('Veuillez sélectionner une image.');
            }
        });
    }
}

// Setup image gallery
function setupImageGallery() {
    // Handle image selection from gallery
    const galleryItems = document.querySelectorAll('.gallery-item');
    
    galleryItems.forEach(item => {
        item.addEventListener('click', function() {
            const imageId = this.getAttribute('data-image-id');
            const imageSrc = this.querySelector('img').src;
            const galleryModal = document.getElementById('image-gallery-modal');
            
            if (galleryModal) {
                const targetBlockId = galleryModal.getAttribute('data-target-block');
                const blockEditor = document.querySelector(`.block-editor[data-block-id="${targetBlockId}"]`);
                
                if (blockEditor) {
                    // Update the image ID input
                    const imageIdInput = blockEditor.querySelector('.block-image-id');
                    if (imageIdInput) {
                        imageIdInput.value = imageId;
                    }
                    
                    // Update the image preview
                    const imagePreview = blockEditor.querySelector('.image-preview');
                    if (imagePreview) {
                        imagePreview.src = imageSrc;
                        imagePreview.style.display = 'block';
                    }
                    
                    // Hide select button and show remove button
                    const selectButton = blockEditor.querySelector('.select-image-btn');
                    const removeButton = blockEditor.querySelector('.remove-image-btn');
                    
                    if (selectButton) {
                        selectButton.style.display = 'none';
                    }
                    
                    if (removeButton) {
                        removeButton.style.display = 'inline-block';
                    }
                    
                    // Update image preview position
                    updateBlockImagePreview(blockEditor);
                }
                
                // Close the modal
                const modal = bootstrap.Modal.getInstance(galleryModal);
                if (modal) {
                    modal.hide();
                }
            }
        });
    });
}