File size: 31,218 Bytes
fcc01f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
// 💬 JavaScript для БелУзор v0.1 — Беларусь

let conversationHistory = [];
let isProcessing = false;
let currentChatId = null;

// Ініцыялізацыя
document.addEventListener('DOMContentLoaded', () => {
    loadStats();
    loadLLMStatus();
    loadHistory();
    loadBooks();
    loadEncyclopedias();
    loadDictionaries();
    loadChatsList();
});

// Аўтаматычнае змяненне памеру textarea
function autoResize(textarea) {
    textarea.style.height = 'auto';
    textarea.style.height = Math.min(textarea.scrollHeight, 180) + 'px';
}

// Апрацоўка клавіш
function handleKeyDown(event) {
    if (event.key === 'Enter' && !event.shiftKey) {
        event.preventDefault();
        sendMessage();
    }
}

// Адпраўка паведамлення
async function sendMessage() {
    const input = document.getElementById('message-input');
    const message = input.value.trim();
    
    if (!message || isProcessing) return;
    
    // Схаваць прывітанне, паказаць кантэйнер паведамленняў
    document.getElementById('welcome-message').style.display = 'none';
    document.getElementById('messages-container').classList.add('active');
    
    // Даданне паведамлення карыстальніка
    addMessage('user', message);
    
    // Ачыстка поля
    input.value = '';
    input.style.height = 'auto';
    
    // Адпраўка на сервер
    isProcessing = true;
    document.getElementById('btn-send').disabled = true;
    
    // Паказ індыкатара загрузкі
    const loadingId = addLoading();
    
    try {
        const useLLM = document.getElementById('use-llm').checked;
        const showFacts = document.getElementById('show-facts').checked;
        
        const response = await fetch('/api/chat', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                message: message,
                use_llm: useLLM
            })
        });
        
        if (!response.ok) {
            throw new Error('Памылка сервера');
        }
        
        const data = await response.json();
        
        // Выдаленне індыкатара загрузкі
        removeLoading(loadingId);
        
        // Даданне адказу з крыніцамі
        addMessage('assistant', data.llm_answer, data.facts, showFacts);
        
    } catch (error) {
        removeLoading(loadingId);
        addMessage('assistant', `❌ Памылка: ${error.message}`);
    } finally {
        isProcessing = false;
        document.getElementById('btn-send').disabled = false;
        input.focus();
    }
}

// Даданне паведамлення
function addMessage(role, text, facts = null, showFacts = true) {
    const container = document.getElementById('messages-container');
    const messageDiv = document.createElement('div');
    messageDiv.className = `message message-${role}`;
    
    const contentDiv = document.createElement('div');
    contentDiv.className = 'message-content';
    
    let html = `<h4>${role === 'user' ? '👤 Вы' : '🤖 БелУзор'}</h4>`;
    html += `<div class="message-text">${escapeHtml(text)}</div>`;
    
    // Калі ёсць факты і яны павінны адлюстроўвацца
    if (facts && facts.length > 0 && showFacts && role === 'assistant') {
        html += `<div class="facts-container">`;
        html += `<h5>📚 Крыніцы (${facts.length}): <button class="btn-search-all" onclick="searchAllInGoogle()">🔍 Шукаць усе ў Google</button></h5>`;
        facts.forEach((fact, i) => {
            const googleQuery = encodeURIComponent(fact.substring(0, 100));
            
            // Ачыстка крыніцы ад імёнаў файлаў
            let cleanFact = fact
                .replace(/^\[tom_\d+_clean\]\s*/gi, '')  // Прыбраць [tom_1_clean]
                .replace(/^\[encyclopedia_volume_\d+\]\s*/gi, '')  // Прыбраць [encyclopedia_volume_1]
                .replace(/^\[\d+\]\s*/gi, '')  // Прыбраць [1]
                .replace(/^\d+\.\s*/gi, '')  // Прыбраць 1.
                .trim();
            
            // Вызначэнне крыніцы
            let sourceBadge = '';
            if (fact.match(/tom_\d+_clean/i)) {
                sourceBadge = '<span class="source-badge encyclopedia">📚 Энцыклапедыя</span>';
            } else if (fact.match(/^\[\d\]/) || fact.match(/^\d+\./)) {
                sourceBadge = '<span class="source-badge textbook">📖 Падручнік</span>';
            }
            
            html += `
                <div class="fact-item">
                    <span class="fact-number">[${i+1}]</span>
                    ${sourceBadge}
                    <span class="fact-text">${escapeHtml(cleanFact)}</span>
                    <div class="fact-actions">
                        <button class="btn-search-google" onclick="searchInGoogle('${googleQuery}')">
                            🔍 Google
                        </button>
                    </div>
                </div>
            `;
        });
        html += `</div>`;
    }
    
    contentDiv.innerHTML = html;
    messageDiv.appendChild(contentDiv);
    container.appendChild(messageDiv);
    
    // Пракрутка ўніз
    container.scrollTop = container.scrollHeight;
}

// Пошук у Google
function searchInGoogle(query) {
    window.open(`https://www.google.com/search?q=${query}`, '_blank');
}

// Пошук усіх крыніц у Google
function searchAllInGoogle() {
    const factTexts = document.querySelectorAll('.fact-item .fact-text');
    if (factTexts.length === 0) return;
    
    const queries = Array.from(factTexts).map(el => el.textContent.trim());
    const combinedQuery = encodeURIComponent(queries.slice(0, 3).join(' OR '));
    
    window.open(`https://www.google.com/search?q=${combinedQuery}`, '_blank');
}

// Індыкатар загрузкі
function addLoading() {
    const container = document.getElementById('messages-container');
    const id = 'loading-' + Date.now();
    
    const loadingDiv = document.createElement('div');
    loadingDiv.className = 'message message-assistant';
    loadingDiv.id = id;
    
    loadingDiv.innerHTML = `
        <div class="message-content">
            <div style="display: flex; align-items: center; gap: 10px;">
                <div class="loading"></div>
                <span>Пошук адказу па ўзоры кніг...</span>
            </div>
        </div>
    `;
    
    container.appendChild(loadingDiv);
    container.scrollTop = container.scrollHeight;
    
    return id;
}

function removeLoading(id) {
    const element = document.getElementById(id);
    if (element) {
        element.remove();
    }
}

// Прыклад пытання
function sendExample(question) {
    document.getElementById('message-input').value = question;
    sendMessage();
}

// Новы дыялог
function newChat() {
    // Аўтаматычнае захаванне бягучага дыялогу
    if (currentChatId || document.querySelectorAll('.message').length > 0) {
        saveChat();
    }
    
    document.getElementById('messages-container').innerHTML = '';
    document.getElementById('welcome-message').style.display = 'block';
    conversationHistory = [];
    currentChatId = null;
    
    // Абнаўленне спісу дыялогаў
    loadChatsList();
    
    showMessage('Новы дыялог пачаты');
}

// Ачыстка гісторыі
async function clearHistory() {
    if (!confirm('Сапраўды ачысціць гісторыю дыялогаў?')) return;
    
    try {
        await fetch('/api/history/clear', { method: 'POST' });
        conversationHistory = [];
        document.getElementById('messages-container').innerHTML = '';
        document.getElementById('welcome-message').style.display = 'block';
        currentChatId = null;
        localStorage.removeItem('belUzor_chats');
        showMessage('Гісторыя ачышчана');
    } catch (error) {
        showMessage('Памылка: ' + error.message);
    }
}

// Загрузка статыстыкі
async function loadStats() {
    try {
        const response = await fetch('/api/stats');
        const stats = await response.json();
        
        document.getElementById('stat-facts').textContent = stats.facts.toLocaleString();
        document.getElementById('stat-words').textContent = stats.indexed_words.toLocaleString();
        document.getElementById('stat-books').textContent = stats.books_count;
        
    } catch (error) {
        console.error('Памылка загрузкі статыстыкі:', error);
    }
}

// Загрузка статусу LLM
async function loadLLMStatus() {
    try {
        const response = await fetch('/api/llm/status');
        const status = await response.json();
        
        const indicator = document.getElementById('llm-indicator');
        const text = document.getElementById('llm-text');
        const btnEnable = document.getElementById('btn-llm-enable');
        const btnDisable = document.getElementById('btn-llm-disable');
        
        if (status.enabled) {
            indicator.classList.add('active');
            text.textContent = `${status.provider} / ${status.model}`;
            text.style.color = 'var(--success-color)';
            btnEnable.style.display = 'none';
            btnDisable.style.display = 'block';
        } else {
            indicator.classList.remove('active');
            text.textContent = 'Адключаны';
            text.style.color = 'var(--text-light)';
            btnEnable.style.display = 'block';
            btnDisable.style.display = 'none';
        }
        
    } catch (error) {
        console.error('Памылка загрузкі LLM статусу:', error);
    }
}

// Уключыць LLM
async function enableLLM() {
    openModal();
}

// Адключыць LLM
async function disableLLM() {
    if (!confirm('Сапраўды адключыць LLM?')) return;
    
    try {
        await fetch('/api/llm/disable', { method: 'POST' });
        document.getElementById('use-llm').checked = false;
        showMessage('LLM адключаны');
        loadLLMStatus();
    } catch (error) {
        showMessage('Памылка: ' + error.message);
    }
}

// Модальнае акно
function openModal() {
    document.getElementById('llm-modal').classList.add('active');
}

function closeModal() {
    document.getElementById('llm-modal').classList.remove('active');
}

// Захаванне налад LLM
async function saveLLMSettings() {
    const provider = document.getElementById('llm-provider').value;
    const model = document.getElementById('llm-model').value;
    const customUrl = document.getElementById('llm-custom-url').value;
    
    try {
        const response = await fetch('/api/llm/enable', {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({
                provider: provider,
                model: model,
                custom_url: customUrl
            })
        });
        
        const result = await response.json();
        
        if (response.ok) {
            showMessage(result.message);
            document.getElementById('use-llm').checked = true;
            closeModal();
            loadLLMStatus();
        } else {
            showMessage('Памылка: ' + result.error);
        }
        
    } catch (error) {
        showMessage('Памылка: ' + error.message);
    }
}

// Загрузка гісторыі
async function loadHistory() {
    try {
        const response = await fetch('/api/history');
        const data = await response.json();
        conversationHistory = data.history || [];
        
        // Калі ёсць гісторыя, паказаць яе
        if (conversationHistory.length > 0) {
            document.getElementById('welcome-message').style.display = 'none';
            document.getElementById('messages-container').classList.add('active');
            
            conversationHistory.forEach(msg => {
                if (msg.role === 'assistant') {
                    addMessage(msg.role, msg.content, msg.facts, true);
                }
            });
        }
        
    } catch (error) {
        console.error('Памылка загрузкі гісторыі:', error);
    }
}

// Загрузка кніг
async function loadBooks() {
    try {
        const response = await fetch('/api/books');
        const data = await response.json();
        
        const booksList = document.getElementById('books-list');
        
        if (data.books && data.books.length > 0) {
            booksList.innerHTML = data.books.map(book => `
                <div class="material-item book-item">
                    <span class="material-icon">📕</span>
                    <span class="material-name">${book.name}</span>
                    <span class="material-size">${book.size_mb} MB</span>
                </div>
            `).join('');
        } else {
            booksList.innerHTML = '<p class="no-items">Кніг няма</p>';
        }
        
    } catch (error) {
        console.error('Памылка загрузкі кніг:', error);
        document.getElementById('books-list').innerHTML = '<p class="error">Памылка загрузкі</p>';
    }
}

// Загрузка энцыклапедый
async function loadEncyclopedias() {
    try {
        const response = await fetch('/api/encyclopedias');
        const data = await response.json();
        
        const encList = document.getElementById('encyclopedia-list');
        
        if (data.encyclopedias && data.encyclopedias.length > 0) {
            encList.innerHTML = data.encyclopedias.map((enc, i) => `
                <div class="material-item encyclopedia-item">
                    <span class="material-icon">📖</span>
                    <span class="material-name">Том ${i+1}</span>
                    <span class="material-size">${enc.size_mb} MB</span>
                </div>
            `).join('');
        } else {
            encList.innerHTML = '<p class="no-items">Энцыклапедый няма</p>';
        }
        
    } catch (error) {
        console.error('Памылка загрузкі энцыклапедый:', error);
        document.getElementById('encyclopedia-list').innerHTML = '<p class="error">Памылка загрузкі</p>';
    }
}

// Загрузка слоўнікаў
async function loadDictionaries() {
    try {
        const response = await fetch('/api/dictionaries');
        const data = await response.json();
        
        const dictList = document.getElementById('dictionaries-list');
        
        if (data.dictionaries && data.dictionaries.length > 0) {
            dictList.innerHTML = data.dictionaries.map(dict => `
                <div class="material-item dictionary-item" onclick="viewDictionary('${dict.name}')" style="cursor: pointer;">
                    <span class="material-icon">📗</span>
                    <span class="material-name">${dict.name}</span>
                    <span class="material-size">${dict.count || ''}</span>
                </div>
            `).join('');
        } else {
            dictList.innerHTML = '<p class="no-items">Слоўнікаў няма</p>';
        }
        
    } catch (error) {
        console.error('Памылка загрузкі слоўнікаў:', error);
        document.getElementById('dictionaries-list').innerHTML = '<p class="error">Памылка загрузкі</p>';
    }
}

// Прагляд слоўніка
async function viewDictionary(dictName) {
    try {
        const modal = document.getElementById('dictionary-modal');
        const title = document.getElementById('dict-title');
        const content = document.getElementById('dictionary-content');
        const countLabel = document.getElementById('dict-count');
        
        title.textContent = dictName;
        content.innerHTML = '<p class="loading">Загрузка...</p>';
        modal.classList.add('active');
        
        const response = await fetch(`/api/dictionaries/${encodeURIComponent(dictName)}`);
        const data = await response.json();
        
        if (data.entries && data.entries.length > 0) {
            window.currentDictionary = data.entries;
            countLabel.textContent = `${data.entries.length} запісаў`;
            renderDictionary(data.entries);
        } else {
            content.innerHTML = '<p class="no-items">Запісаў не знойдзена</p>';
        }
        
    } catch (error) {
        showMessage('Памылка загрузкі слоўніка: ' + error.message);
        closeDictionaryModal();
    }
}

// Рэндэрынг слоўніка
function renderDictionary(entries) {
    const content = document.getElementById('dictionary-content');
    
    if (!entries || entries.length === 0) {
        content.innerHTML = '<p class="no-items">Запісаў не знойдзена</p>';
        return;
    }
    
    content.innerHTML = entries.map(entry => {
        const word = entry.word || '';
        const definition = entry.definition || '';
        
        // Калі ёсць і слова і азначэнне
        if (word && definition) {
            return `
                <div class="dictionary-entry">
                    <div class="entry-word">${escapeHtml(word)}</div>
                    <div class="entry-definition">${escapeHtml(definition)}</div>
                </div>
            `;
        }
        // Калі толькі слова (назва артыкула)
        else if (word) {
            return `
                <div class="dictionary-entry">
                    <div class="entry-word">${escapeHtml(word)}</div>
                </div>
            `;
        }
        // Пусты запіс - прапускаем
        return '';
    }).filter(html => html.trim() !== '').join('');
    
    if (content.innerHTML.trim() === '') {
        content.innerHTML = '<p class="no-items">Запісаў не знойдзена</p>';
    }
}

// Фільтр слоўніка
function filterDictionary() {
    const query = document.getElementById('dict-search').value.toLowerCase();
    
    if (!window.currentDictionary) return;
    
    const filtered = window.currentDictionary.filter(entry => {
        const word = (entry.word || '').toLowerCase();
        const def = (entry.definition || '').toLowerCase();
        return word.includes(query) || def.includes(query);
    });
    
    document.getElementById('dict-count').textContent = `${filtered.length} запісаў`;
    renderDictionary(filtered);
}

// Закрыццё модальнага акна слоўніка
function closeDictionaryModal() {
    const modal = document.getElementById('dictionary-modal');
    modal.classList.remove('active');
    window.currentDictionary = null;
    document.getElementById('dict-search').value = '';
}

// Пераключэнне матэрыялаў
function toggleMaterials(type) {
    const list = document.getElementById(`${type}-list`);
    const icon = document.getElementById(`${type}-icon`);
    
    if (list.style.display === 'none') {
        list.style.display = 'block';
        icon.textContent = '▼';
    } else {
        list.style.display = 'none';
        icon.textContent = '▶';
    }
}

// Загрузка спісу дыялогаў
function loadChatsList() {
    const chats = JSON.parse(localStorage.getItem('belUzor_chats') || '[]');
    const chatsList = document.getElementById('chats-list');
    const chatCount = document.getElementById('chat-count');
    
    chatCount.textContent = `(${chats.length})`;
    
    if (chats.length === 0) {
        chatsList.innerHTML = '<p class="no-chats">Дыялогаў няма</p>';
        return;
    }
    
    // Сартыроўка па часе (новыя зверху)
    const sortedChats = chats.sort((a, b) => new Date(b.timestamp) - new Date(a.timestamp));
    
    chatsList.innerHTML = sortedChats.map((chat, i) => {
        const date = new Date(chat.timestamp).toLocaleString('be-BY', {
            day: 'numeric',
            month: 'short',
            hour: '2-digit',
            minute: '2-digit'
        });
        const firstMessage = chat.messages[0]?.content.substring(0, 40) || 'Дыялог';
        
        return `
            <div class="chat-item-sidebar" id="sidebar-chat-${chat.id}">
                <div class="chat-item-sidebar-info" onclick="loadSpecificChat(${chat.id})">
                    <div class="chat-item-title">${firstMessage}${chat.messages[0]?.content.length > 40 ? '...' : ''}</div>
                    <div class="chat-item-meta">
                        <span class="chat-item-date">${date}</span>
                        <span class="chat-item-messages">${chat.messages.length} пав.</span>
                    </div>
                </div>
                <button class="btn-delete-chat-sidebar" onclick="deleteChat(${chat.id})" title="Выдаліць дыялог">
                    🗑️
                </button>
            </div>
        `;
    }).join('');
}

// Загрузка кніг
async function loadBooks() {
    try {
        const response = await fetch('/api/books');
        const data = await response.json();
        
        const booksList = document.getElementById('books-list');
        
        if (data.books && data.books.length > 0) {
            booksList.innerHTML = data.books.map(book => `
                <div class="book-item">
                    <span>📖 ${book.name}</span>
                    <span style="color: rgba(255,255,255,0.5); font-size: 12px;">${book.size_mb} MB</span>
                </div>
            `).join('');
        } else {
            booksList.innerHTML = '<p style="color: rgba(255,255,255,0.5); font-size: 13px;">Кніг няма</p>';
        }
        
    } catch (error) {
        console.error('Памылка загрузкі кніг:', error);
        document.getElementById('books-list').innerHTML = '<p style="color: rgba(255,255,255,0.5); font-size: 13px;">Памылка загрузкі</p>';
    }
}

// Дадаванне кнігі
function addBook() {
    document.getElementById('file-input').click();
}

function handleFileSelect(event) {
    const file = event.target.files[0];
    if (!file) return;
    
    const reader = new FileReader();
    reader.onload = async function(e) {
        const bookText = e.target.result;
        const bookName = file.name.replace('.txt', '');
        
        try {
            showMessage(`Апрацоўка кнігі "${bookName}"...`);
            
            // Запампоўка на сервер
            const response = await fetch('/api/books/add', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify({
                    book_text: bookText,
                    book_name: bookName,
                    parse_mode: 'sentences'
                })
            });
            
            const result = await response.json();
            
            if (response.ok) {
                showMessage(`✅ Кніга дададзена: ${result.facts_count} фактаў`);
                loadBooks();
                loadStats();
            } else {
                showMessage('Памылка: ' + result.error);
            }
            
        } catch (error) {
            showMessage('Памылка: ' + error.message);
        }
    };
    reader.readAsText(file, 'UTF-8');
    
    // Ачыстка input для паўторнага выбару таго ж файла
    event.target.value = '';
}

// Захаванне дыялогу
function saveChat() {
    const messages = document.querySelectorAll('.message');
    if (messages.length === 0) {
        showMessage('Няма чаго захоўваць');
        return;
    }
    
    const chatData = {
        id: Date.now(),
        timestamp: new Date().toISOString(),
        messages: []
    };
    
    messages.forEach(msg => {
        const isUser = msg.classList.contains('message-user');
        const content = msg.querySelector('.message-text').textContent;
        chatData.messages.push({
            role: isUser ? 'user' : 'assistant',
            content: content,
            timestamp: new Date().toISOString()
        });
    });
    
    // Захаванне ў localStorage
    let chats = JSON.parse(localStorage.getItem('belUzor_chats') || '[]');
    chats.push(chatData);
    localStorage.setItem('belUzor_chats', JSON.stringify(chats));
    
    currentChatId = chatData.id;
    showMessage('Дыялог захаваны');
}

// Загрузка дыялогу
function loadChat() {
    const chats = JSON.parse(localStorage.getItem('belUzor_chats') || '[]');

    if (chats.length === 0) {
        showMessage('Захаваных дыялогаў няма');
        return;
    }

    // Стварэнне спісу дыялогаў
    const chatList = chats.map((chat, i) => {
        const date = new Date(chat.timestamp).toLocaleString('be-BY');
        return `
            <div class="chat-item" id="chat-${chat.id}">
                <div class="chat-item-info" onclick="loadSpecificChat(${chat.id})">
                    <strong>Дыялог #${i + 1}</strong><br>
                    <small class="chat-date">${date}</small><br>
                    <small class="chat-messages">${chat.messages.length} паведамленняў</small>
                </div>
                <button class="btn-delete-chat" onclick="deleteChat(${chat.id})" title="Выдаліць дыялог">
                    🗑️
                </button>
            </div>
        `;
    }).join('');

    // Паказ модальнага акна са спісам
    const modal = document.createElement('div');
    modal.className = 'modal active';
    modal.innerHTML = `
        <div class="modal-content">
            <div class="modal-header">
                <h3>📂 Загрузка дыялогу</h3>
                <button class="btn-close" onclick="this.closest('.modal').remove()">&times;</button>
            </div>
            <div class="modal-body modal-body-scroll">
                ${chatList}
            </div>
            <div class="modal-footer">
                <button class="btn-cancel" onclick="this.closest('.modal').remove()">Закрыць</button>
                <button class="btn-delete-all" onclick="deleteAllChats()">🗑️ Выдаліць усе</button>
            </div>
        </div>
    `;
    document.body.appendChild(modal);
}

// Выдаленне дыялогу
function deleteChat(chatId) {
    if (!confirm('Выдаліць гэты дыялог?')) return;
    
    let chats = JSON.parse(localStorage.getItem('belUzor_chats') || '[]');
    chats = chats.filter(c => c.id !== chatId);
    localStorage.setItem('belUzor_chats', JSON.stringify(chats));
    
    // Абнаўленне спісу ў бакавой панэлі
    loadChatsList();
    
    // Выдаленне з мадальнага акна
    const chatItem = document.getElementById(`chat-${chatId}`);
    if (chatItem) chatItem.remove();
    
    showMessage('Дыялог выдалены');
}

// Выдаленне ўсіх дыялогаў
function deleteAllChats() {
    if (!confirm('Сапраўды выдаліць УСЕ дыялогі?')) return;
    
    localStorage.removeItem('belUzor_chats');
    
    // Зачыненне модальнага акна
    document.querySelector('.modal').remove();
    
    showMessage('Усе дыялогі выдалены');
}

// Загрузка канкрэтнага дыялогу
function loadSpecificChat(chatId) {
    const chats = JSON.parse(localStorage.getItem('belUzor_chats') || '[]');
    const chat = chats.find(c => c.id === chatId);
    
    if (!chat) return;
    
    document.getElementById('messages-container').innerHTML = '';
    document.getElementById('welcome-message').style.display = 'none';
    document.getElementById('messages-container').classList.add('active');
    
    chat.messages.forEach(msg => {
        addMessage(msg.role, msg.content, null, false);
    });
    
    currentChatId = chatId;
    
    // Зачыненне модальнага акна
    document.querySelector('.modal').remove();
    showMessage('Дыялог загружаны');
}

// Захаванне ў localStorage
function saveChatToStorage() {
    if (currentChatId) {
        saveChat();
    }
}

// Экспарт дыялогу
function exportChat() {
    const messages = document.querySelectorAll('.message');
    if (messages.length === 0) {
        showMessage('Няма чаго экспартаваць');
        return;
    }
    
    let text = '🧠🇧🇾 БелУзор v0.1 — Дыялог\n';
    text += '=' .repeat(60) + '\n';
    text += `Дата: ${new Date().toLocaleString('be-BY')}\n`;
    text += '=' .repeat(60) + '\n\n';
    
    messages.forEach(msg => {
        const role = msg.classList.contains('message-user') ? 'Вы' : 'БелУзор';
        const content = msg.querySelector('.message-text').textContent;
        text += `${role}: ${content}\n\n`;
    });
    
    text += '\n' + '=' .repeat(60) + '\n';
    text += 'Працуем па ўзоры давераных кніг\n';
    
    const blob = new Blob([text], { type: 'text/plain;charset=utf-8' });
    const url = URL.createObjectURL(blob);
    const a = document.createElement('a');
    a.href = url;
    a.download = `БелУзор_дыялог_${new Date().toISOString().split('T')[0]}.txt`;
    a.click();
    URL.revokeObjectURL(url);
    
    showMessage('Дыялог экспартаваны');
}

// Паведамленне
function showMessage(text) {
    const existing = document.querySelector('.toast');
    if (existing) existing.remove();
    
    const div = document.createElement('div');
    div.className = 'toast';
    div.textContent = text;
    document.body.appendChild(div);
    
    setTimeout(() => {
        div.style.animation = 'fadeOut 0.3s';
        setTimeout(() => div.remove(), 300);
    }, 3000);
}

// Escape HTML
function escapeHtml(text) {
    const div = document.createElement('div');
    div.textContent = text;
    return div.innerHTML;
}

// Даданне анімацый
const style = document.createElement('style');
style.textContent = `
    @keyframes fadeOut {
        from { opacity: 1; transform: translateY(0); }
        to { opacity: 0; transform: translateY(20px); }
    }
`;
document.head.appendChild(style);