File size: 23,819 Bytes
23f58c1
d1e876b
91025a4
 
 
 
 
 
 
 
23f58c1
91025a4
 
 
 
 
 
 
 
1eee5fa
 
 
 
 
 
 
 
 
 
 
d1e876b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1eee5fa
d1e876b
 
 
23f58c1
1eee5fa
d1e876b
 
1eee5fa
23f58c1
d1e876b
 
 
 
 
 
 
 
 
 
 
1eee5fa
d1e876b
23f58c1
 
d1e876b
 
 
 
23f58c1
d1e876b
 
 
 
23f58c1
 
d1e876b
 
 
 
 
23f58c1
 
d1e876b
 
 
23f58c1
d1e876b
23f58c1
d1e876b
23f58c1
d1e876b
23f58c1
d1e876b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1eee5fa
d1e876b
 
91025a4
d1e876b
 
23f58c1
d1e876b
 
23f58c1
d1e876b
 
 
 
 
 
23f58c1
d1e876b
23f58c1
 
d1e876b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23f58c1
d1e876b
 
23f58c1
d1e876b
 
 
 
23f58c1
8f199d9
d1e876b
 
 
 
 
 
 
 
 
 
 
 
8f199d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d1e876b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8f199d9
 
 
 
 
 
 
 
 
d1e876b
8f199d9
d1e876b
 
 
8f199d9
 
d1e876b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
149db23
 
d1e876b
 
 
 
 
 
 
 
1eee5fa
d1e876b
 
 
91025a4
d1e876b
 
 
 
 
 
 
 
 
 
 
 
 
1eee5fa
d1e876b
 
 
91025a4
d1e876b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23f58c1
 
 
d1e876b
 
 
 
 
 
 
 
 
 
8781c4d
ae3a709
 
 
8781c4d
23f58c1
 
 
 
d1e876b
 
 
 
 
23f58c1
 
d1e876b
 
23f58c1
d1e876b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
da8cca5
 
 
 
 
 
 
 
 
 
 
299df5d
 
23f58c1
299df5d
23f58c1
 
 
 
 
 
 
 
 
 
d1e876b
 
 
 
1eee5fa
d1e876b
 
 
 
 
 
 
 
 
 
 
f7fe3bc
d1e876b
f7fe3bc
 
c72cb42
23f58c1
f7fe3bc
 
 
 
 
 
 
 
 
 
 
 
 
305719c
23f58c1
 
 
 
 
 
 
c72cb42
23f58c1
 
c72cb42
305719c
 
f7fe3bc
 
c72cb42
d1e876b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1eee5fa
d1e876b
 
91025a4
d1e876b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1eee5fa
d1e876b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23f58c1
1eee5fa
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
import { Client } from "https://cdn.jsdelivr.net/npm/@gradio/client@1.9.0/dist/index.min.js";

// Read/write tokens from sessionStorage
function getReadToken() {
    return sessionStorage.getItem('hf_read_token') || '';
}
function getWriteToken() {
    return sessionStorage.getItem('hf_write_token') || '';
}

// Helper to add tokens to every API call
function withTokens(obj = {}) {
    return {
        ...obj,
        read_token: getReadToken(),
        write_token: getWriteToken(),
    };
}

// Helper to call secure server-side proxy
async function callSecureApi(fn, args) {
    const resp = await fetch('/api/proxy', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ fn, args })
    });
    if (!resp.ok) throw new Error('Proxy error: ' + resp.status);
    return await resp.json();
}

let client;
let currentUser = null;
let currentPassword = null;
let files = [];
let selected = new Set();
let currentView = 'grid';
let previewFile = null;
let isLoadingFiles = false;

const iconMap = {
    'pdf': '📄', 'doc': '📄', 'docx': '📄',
    'png': '🖼️', 'jpg': '🖼️', 'jpeg': '🖼️', 'gif': '🖼️',
    'mp3': '🎵', 'wav': '🎵', 'flac': '🎵',
    'mp4': '📹', 'avi': '📹', 'mov': '📹',
    'zip': '🗜️', 'rar': '🗜️', '7z': '🗜️',
    'txt': '📝', 'md': '📝',
    'py': '🐍', 'js': '📜', 'html': '🌐', 'css': '🌐',
    'psd': '🎨', 'ai': '🎨',
    'sql': '🗃️', 'xlsx': '📊', 'csv': '📊',
    'pptx': '📑', 'ppt': '📑',
};

// Initialize dashboard on page load
async function initDashboard() {
    console.log('=== Dashboard Init Started ===');
    currentUser = sessionStorage.getItem('pockit_user');
    currentPassword = sessionStorage.getItem('pockit_pass');
    
    if (!currentUser || !currentPassword) {
        console.warn('No session found, redirecting to login');
        window.location.href = 'index.html';
        return;
    }
    
    try {
        // Update account info first
        const initials = currentUser.substring(0, 2).toUpperCase();
        document.querySelector('.avatar').textContent = initials;
        document.querySelector('.account-name').textContent = currentUser;
        
        // Load data using proxy
        await loadUserData();
        await loadFiles();
        console.log('✓ Dashboard loaded');
        
    } catch (err) {
        console.error('Init error:', err);
        showToast('Connection failed');
        setTimeout(() => window.location.href = 'index.html', 2000);
    }
}

// Load user info and update cards
async function loadUserData() {
    try {
        console.log('Loading user data...');
        const result = await callSecureApi("/search_user", withTokens({ user_id: currentUser }));
        const [log, isDev, isPro, isTester, capacityUsed, usedTotal] = result.data;
        
        // Determine role
        let role = 'free';
        if (isDev) role = 'dev';
        else if (isPro) role = 'pro';
        else if (isTester) role = 'tester';
        
        const tag = document.getElementById('role-tag');
        tag.className = 'role-tag ' + role;
        tag.textContent = role.charAt(0).toUpperCase() + role.slice(1);
        document.getElementById('tier-val').textContent = tag.textContent;
        
        // Parse capacity
        const capacityPercent = Math.round(capacityUsed);
        let usedDisplay = usedTotal || '0 / 5 GB';
        let usedParts = usedDisplay.split('/');
        let usedAmount = usedParts[0].trim();
        let maxAmount = usedParts[1] ? usedParts[1].trim() : '5 GB';
        
        // Update storage displays
        document.querySelector('.card-bar-fill').style.width = capacityPercent + '%';
        document.querySelector('.stor-label span:last-child').textContent = usedDisplay;
        document.querySelector('.sbar-fill').style.width = capacityPercent + '%';
        
        const storageCard = document.querySelectorAll('.card')[0];
        storageCard.querySelector('.card-val').innerHTML = usedAmount + ' <span style="font-size:.38em;font-weight:400;color:rgba(0,0,0,.38)">(' + capacityPercent + '%)</span>';
        storageCard.querySelector('.card-sub').textContent = usedDisplay + ' used';
        
        document.querySelectorAll('.card')[3].querySelector('.card-sub').textContent = maxAmount + ' max capacity';
        
        console.log('✓ User data loaded');
    } catch (err) {
        console.error('User data error:', err);
    }
}

// Load files from API
async function loadFiles() {
    // Prevent concurrent loads
    if (isLoadingFiles) return;
    isLoadingFiles = true;
    
    try {
        console.log('Loading files...');
        loading(true);
        const result = await callSecureApi("/get_files_secure", withTokens({
            user_id: currentUser,
            password: currentPassword,
        }));
        // API returns [dropdown, status]
        const dropdownData = result.data[0];
        console.log('Raw dropdownData:', dropdownData);
        let fileNames = [];
        if (dropdownData && typeof dropdownData === 'object' && dropdownData.choices) {
            // Flatten all sub-arrays in choices
            fileNames = dropdownData.choices.flat ? dropdownData.choices.flat() : [].concat(...dropdownData.choices);
        } else if (Array.isArray(dropdownData)) {
            fileNames = dropdownData;
        } else if (typeof dropdownData === 'string') {
            fileNames = [dropdownData];
        }
        // Convert all to string, trim, and filter empty
        fileNames = fileNames.map(f => String(f).trim()).filter(Boolean);
        console.log('Extracted fileNames from dropdown:', fileNames);
        // Deduplicate file names (case-insensitive, trim)
        const seen = new Set();
        const deduped = [];
        for (const name of fileNames) {
            const key = name.toLowerCase();
            if (!key || seen.has(key)) continue;
            seen.add(key);
            deduped.push(name);
        }
        files = deduped.map((name, idx) => {
            const ext = name.includes('.') ? name.split('.').pop().toLowerCase() : '';
            return {
                id: idx,
                name: name,
                icon: iconMap[ext] || '📁',
                size: '—',
                date: 'Recently',
                type: ext ? ext.toUpperCase() : '',
                preview: null // for preview content
            };
        });
        console.log('Final files array before render:', files);
        selected.clear();
        updateStatsCards();
        render(files);
        loading(false);
        console.log('✓ Files loaded and rendered');
        preloadTextPreviews();
    } catch (err) {
        console.error('Files load error:', err);
        loading(false);
        files = [];
        selected.clear();
        render([]);
        showToast('Failed to load files');
    } finally {
        isLoadingFiles = false;
    }
}

// Preload short text previews for text files so grid view
// can show the first few words instead of just an icon.
async function preloadTextPreviews() {
    // Only attempt after files are loaded and user is known
    if (!currentUser || !currentPassword || !Array.isArray(files)) return;
    for (const file of files) {
        // Simple heuristic: only fetch preview for obvious text formats
        const name = (file && file.name) ? String(file.name).toLowerCase() : '';
        if (!name.endsWith('.txt') && !name.endsWith('.md')) continue;
        try {
            const result = await callSecureApi("/get_preview_text_action", withTokens({
                user_id: currentUser,
                password: currentPassword,
                filename: file.name,
            }));
            const text = Array.isArray(result.data) ? result.data[0] : result.data;
            if (typeof text === 'string' && text.trim()) {
                file.preview = text;
            }
        } catch (err) {
            console.error('Text preview preload error for', file.name, err);
        }
    }
    render(files);
}

// Update stats cards
function updateStatsCards() {
    const cards = document.querySelectorAll('.card');
    if (cards.length < 3) return;
    
    // Files count
    cards[1].querySelector('.card-val').textContent = files.length;
    
    // File types
    const fileTypes = new Set(files.map(f => f.type));
    cards[1].querySelector('.card-sub').textContent = `Across ${fileTypes.size} file type${fileTypes.size !== 1 ? 's' : ''}`;
    
    // Upload card
    cards[2].querySelector('.card-val').textContent = files.length > 0 ? '1' : '0';
    cards[2].querySelector('.card-sub').textContent = files.length > 0 ? 'Last upload: Recently' : 'No uploads yet';
}

// Render grid and table
function render(list) {
    console.log('Rendering', list.length, 'files:', list.map(f => f.name));
    
    const grid = document.getElementById('file-grid');
    const tbody = document.getElementById('file-tbody');
    
    if (!grid) {
        console.error('file-grid missing');
        return;
    }
    if (!tbody) {
        console.error('file-tbody missing');
        return;
    }
    
    // Clear everything first
    grid.innerHTML = '';
    tbody.innerHTML = '';
    
    // Only render if there are files
    if (!list || list.length === 0) {
        console.log('No files to render');
        return;
    }
    
    // Grid HTML
    const gridHTML = list.map((f, i) => {
        let iconOrPreview = f.icon;
        if (f.preview && typeof f.preview === 'string' && f.preview.trim()) {
            const words = f.preview.trim().split(/\s+/).slice(0, 5).join(' ');
            const snippet = words + (f.preview.trim().length > words.length ? '...' : '');
            iconOrPreview = escapeHtml(snippet);
        }
        return `
        <div class="file-card ${selected.has(i) ? 'selected' : ''}" onclick="toggleSelect(${i})" ondblclick="openPreview(${i})">
            <span class="fc-check"><i class="fa-solid fa-check"></i></span>
            <span class="fc-icon">${iconOrPreview}</span>
            <span class="fc-name">${f.name}</span>
            <span class="fc-meta">${f.size}</span>
        </div>
    `;
    }).join('');
    
    grid.innerHTML = gridHTML;
    console.log('Grid rendered with', list.length, 'items');
    
    // Table HTML
    const tableHTML = list.map((f, i) => `
        <tr onclick="openPreview(${i})">
            <td class="td-name"><span>${f.icon}</span> ${f.name}</td>
            <td class="td-muted">${f.size}</td>
            <td class="td-muted">${f.date}</td>
            <td class="td-muted">${f.type}</td>
            <td class="td-actions">
                <button class="tbl-btn" onclick="event.stopPropagation();openPreview(${i})"><i class="fa-solid fa-eye"></i></button>
                <button class="tbl-btn" onclick="event.stopPropagation();downloadFile(${i})"><i class="fa-solid fa-download"></i></button>
                <button class="tbl-btn del" onclick="event.stopPropagation();deleteFile(${i})"><i class="fa-solid fa-trash"></i></button>
            </td>
        </tr>
    `).join('');
    
    tbody.innerHTML = tableHTML;
    console.log('Table rendered with', list.length, 'items');
}

// Select/deselect file
function toggleSelect(id) {
    if (id < 0 || id >= files.length) return;
    selected.has(id) ? selected.delete(id) : selected.add(id);
    render(currentView === 'grid' ? files : files);
}

function clearSelection() {
    selected.clear();
    render(files);
}

function handleSelectAll(checked) {
    if (checked) {
        files.forEach((_, i) => selected.add(i));
    } else {
        selected.clear();
    }
    render(files);
}

// Open file preview
function openPreview(id) {
    if (id < 0 || id >= files.length) return;
    previewFile = files[id];
    document.getElementById('preview-thumb').textContent = previewFile.icon;
    document.getElementById('preview-name').textContent = previewFile.name;
    document.getElementById('preview-name').style.color = '#1a1a2e';
    document.getElementById('pv-size').textContent = previewFile.size;
    document.getElementById('pv-date').textContent = previewFile.date;
    document.getElementById('pv-type').textContent = previewFile.type;
    // Load preview content from API
    loadPreviewContent(previewFile);
    selected.clear();
    selected.add(id);
    render(files);
}

// Load preview content for a file using /handle_preview_secure and /get_preview_text_action
async function loadPreviewContent(file) {
    if (!file || !file.name) return;
    const previewContainer = document.getElementById('preview-content');
    if (!previewContainer) return;
    previewContainer.innerHTML = '<span style="color:#aaa">Loading preview...</span>';
    try {
        // Try /handle_preview_secure first
        const result = await callSecureApi("/handle_preview_secure", withTokens({
            user_id: currentUser,
            password: currentPassword,
            filename: file.name,
        }));
        // result.data: [file, localPath, html, image, code]
        let previewHtml = '';
        if (result.data[3] && typeof result.data[3] === 'string' && result.data[3].startsWith('data:image')) {
            // Image preview
            previewHtml = `<img src="${result.data[3]}" alt="Preview" style="max-width:100%;max-height:200px;">`;
        } else if (result.data[2] && typeof result.data[2] === 'string' && result.data[2].length > 0) {
            // HTML preview
            previewHtml = `<div class="pv-html">${result.data[2]}</div>`;
        } else if (result.data[4] && typeof result.data[4] === 'string' && result.data[4].length > 0) {
            // Code preview
            previewHtml = `<pre class="pv-code">${escapeHtml(result.data[4])}</pre>`;
        } else {
            // Try /get_preview_text_action for text preview
            const textResult = await callSecureApi("/get_preview_text_action", withTokens({
                user_id: currentUser,
                password: currentPassword,
                filename: file.name,
            }));
            if (textResult.data && typeof textResult.data[0] === 'string' && textResult.data[0].length > 0) {
                previewHtml = `<pre class="pv-text">${escapeHtml(textResult.data[0])}</pre>`;
            } else {
                previewHtml = '<span style="color:#aaa">No preview available.</span>';
            }
        }
        previewContainer.innerHTML = previewHtml;
    } catch (err) {
        console.error('Preview error:', err);
        previewContainer.innerHTML = '<span style="color:#f55">Failed to load preview.</span>';
    }
}

// Helper to escape HTML for code/text preview
function escapeHtml(str) {
    return String(str)
        .replace(/&/g, '&amp;')
        .replace(/</g, '<')
        .replace(/>/g, '>')
        .replace(/"/g, '"')
        .replace(/'/g, '&#39;');
}

// Delete single file
async function deleteFile(id) {
    if (id < 0 || id >= files.length) return;
    const file = files[id];
    
    try {
        loading(true);
        const result = await callSecureApi("/delete_file_secure", withTokens({
            user_id: currentUser,
            password: currentPassword,
            filename: file.name,
        }));
        
        console.log('Deleted:', file.name);
        
        // Update storage
        const [status, capacityUsed, usedTotal] = result.data;
        const percent = Math.round(capacityUsed);
        document.querySelector('.card-bar-fill').style.width = percent + '%';
        document.querySelector('.sbar-fill').style.width = percent + '%';
        document.querySelector('.stor-label span:last-child').textContent = usedTotal;
        
        // Remove from array
        files.splice(id, 1);
        selected.delete(id);
        
        updateStatsCards();
        render(files);
        loading(false);
        showToast(file.name + ' deleted');
    } catch (err) {
        console.error('Delete error:', err);
        loading(false);
        showToast('Delete failed');
    }
}

// Delete selected files
async function deleteSelected() {
    if (selected.size === 0) {
        showToast('No files selected');
        return;
    }
    const ids = Array.from(selected).sort((a, b) => b - a);
    for (const id of ids) {
        await deleteFile(id);
    }
}

// Delete preview file
async function deletePreviewFile() {
    if (!previewFile) return;
    const idx = files.indexOf(previewFile);
    if (idx >= 0) await deleteFile(idx);
    previewFile = null;
    document.getElementById('preview-name').textContent = 'Select a file';
    document.getElementById('preview-name').style.color = 'rgba(0,0,0,.3)';
}

// Download file
async function downloadFile(id) {
    if (id < 0 || id >= files.length) return;
    const file = files[id];
    try {
        loading(true);
        const resp = await fetch('/api/download-link', {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({
                user_id: currentUser,
                password: currentPassword,
                filename: file.name,
            }),
        });
        if (!resp.ok) throw new Error('Download-link HTTP ' + resp.status);
        const payload = await resp.json();
        const fileTarget = Array.isArray(payload.data) ? payload.data[0] : payload.data;
        if (typeof fileTarget === 'string' && fileTarget) {
            const a = document.createElement('a');
            a.href = '/api/download?file=' + encodeURIComponent(fileTarget);
            a.download = file.name;
            document.body.appendChild(a);
            a.click();
            setTimeout(() => {
                document.body.removeChild(a);
            }, 100);
            showToast('Download started: ' + file.name);
        } else {
            showToast('Download link error');
        }
        loading(false);
    } catch (err) {
        console.error('Download error:', err);
        loading(false);
        showToast('Download failed');
    }
}

async function downloadPreviewFile() {
    if (!previewFile) return;
    const idx = files.indexOf(previewFile);
    if (idx >= 0) {
        await downloadFile(idx);
    }
}

// Upload file (single file) via server-side endpoint so private HF token stays on the server
async function handleUpload(input) {
    if (!input.files || !input.files[0]) return;
    const file = input.files[0];
    try {
        loading(true);
        const formData = new FormData();
        formData.append('file', file);
        formData.append('user_id', currentUser);
        formData.append('password', currentPassword);
        formData.append('custom_name', file.name);

        const resp = await fetch('/api/upload', {
            method: 'POST',
            body: formData,
        });
        if (!resp.ok) throw new Error('Upload HTTP ' + resp.status);
        const result = await resp.json();

        console.log('Uploaded:', file.name);
        // Update storage
        const [status, capacityUsed, usedTotal] = result.data;
        const percent = Math.round(capacityUsed);
        document.querySelector('.card-bar-fill').style.width = percent + '%';
        document.querySelector('.sbar-fill').style.width = percent + '%';
        document.querySelector('.stor-label span:last-child').textContent = usedTotal;
        // Reload files
        await loadFiles();
        showToast(file.name + ' uploaded');
        input.value = '';
    } catch (err) {
        console.error('Upload error:', err);
        showToast('Upload failed');
    } finally {
        loading(false);
    }
}

function handleDrop(e) {
    e.preventDefault();
    document.getElementById('drop-zone').classList.remove('drag');
    if (e.dataTransfer.files) {
        handleUpload({ files: e.dataTransfer.files });
    }
}

// Filter files by search
function filterFiles(q) {
    const query = q.toLowerCase();
    const filtered = files.filter(f => f.name.toLowerCase().includes(query));
    render(filtered);
}

// Toggle view
function setView(v) {
    currentView = v;
    document.getElementById('file-grid').style.display = v === 'grid' ? 'grid' : 'none';
    document.getElementById('file-table-wrap').style.display = v === 'list' ? 'block' : 'none';
    document.getElementById('vt-grid').classList.toggle('active', v === 'grid');
    document.getElementById('vt-list').classList.toggle('active', v === 'list');
}

// Navigation sections
function setNav(el, section) {
    document.querySelectorAll('.nav-item').forEach(n => n.classList.remove('active'));
    el.classList.add('active');
    
    if (section === 'recent') {
        document.getElementById('page-title').textContent = 'Recent Files';
        render(files.slice(0, 5));
    } else {
        document.getElementById('page-title').textContent = 'My Files';
        render(files);
    }
}

// Change password
async function changePassword() {
    const p1 = document.getElementById('new-pw').value;
    const p2 = document.getElementById('conf-pw').value;
    
    if (!p1 || p1 !== p2) {
        showToast('Passwords do not match');
        return;
    }
    
    try {
        loading(true);
        const result = await callSecureApi("/update_password", withTokens({
            user_id: currentUser,
            new_password: p1,
        }));
        
        if (result.data[0].toLowerCase().includes('success') || result.data[0].toLowerCase().includes('updated')) {
            currentPassword = p1;
            sessionStorage.setItem('pockit_pass', p1);
            closeModal('pw-modal');
            showToast('Password updated');
            document.getElementById('new-pw').value = '';
            document.getElementById('conf-pw').value = '';
        } else {
            showToast('Password update failed');
        }
        loading(false);
    } catch (err) {
        console.error('Password error:', err);
        loading(false);
        showToast('Password update failed');
    }
}

// Logout
function logoutUser() {
    sessionStorage.clear();
    showToast('Logged out');
    setTimeout(() => window.location.href = 'index.html', 1500);
}

// Modal helpers
function openModal(id) {
    document.getElementById(id).classList.add('open');
}

function closeModal(id) {
    document.getElementById(id).classList.remove('open');
}

// UI helpers
function loading(on) {
    document.getElementById('loader').classList.toggle('on', on);
}

let toastTimer;
function showToast(msg) {
    const toast = document.getElementById('toast');
    document.getElementById('toast-msg').textContent = msg;
    toast.classList.add('show');
    clearTimeout(toastTimer);
    toastTimer = setTimeout(() => toast.classList.remove('show'), 3000);
}

// Initialize on page load
document.addEventListener('DOMContentLoaded', initDashboard);

// Expose to global scope
window.initDashboard = initDashboard;
window.loadUserData = loadUserData;
window.loadFiles = loadFiles;
window.updateStatsCards = updateStatsCards;
window.render = render;
window.toggleSelect = toggleSelect;
window.clearSelection = clearSelection;
window.handleSelectAll = handleSelectAll;
window.openPreview = openPreview;
window.loadPreviewContent = loadPreviewContent;
window.deleteFile = deleteFile;
window.deleteSelected = deleteSelected;
window.deletePreviewFile = deletePreviewFile;
window.downloadFile = downloadFile;
window.downloadPreviewFile = downloadPreviewFile;
window.handleUpload = handleUpload;
window.handleDrop = handleDrop;
window.filterFiles = filterFiles;
window.setView = setView;
window.setNav = setNav;
window.changePassword = changePassword;
window.logoutUser = logoutUser;
window.openModal = openModal;
window.closeModal = closeModal;
window.loading = loading;
window.showToast = showToast;