File size: 37,009 Bytes
7c5da52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"use client";

import React, { useState, useEffect } from "react";
import {
  UploadCloud,
  X,
  Edit3,
  Shield,
  Download,
  CheckCircle2,
  AlertTriangle,
  Sparkles,
  Share2,
  Copy,
  Check,
  Lock,
  Unlock,
  Power,
  RefreshCw,
  ExternalLink,
  UserCheck,
  UserX,
  Clock,
} from "lucide-react";
import { useCloud } from "@/context/CloudContext";
import { formatBytes, buildFrontendShareUrl } from "@/lib/api";
import { ToastMsg } from "@/types/cloud";

export function GlobalModals() {
  const {
    uploadModalOpen,
    setUploadModalOpen,
    uploadTargetFolder,
    setUploadTargetFolder,
    selectedFiles,
    setSelectedFiles,
    startBatchUpload,
    renameTarget,
    setRenameTarget,
    newPathInput,
    setNewPathInput,
    renaming,
    handleRenameSubmit,
    editingUser,
    setEditingUser,
    handleSaveUser,
    previewFile,
    setPreviewFile,
    getDownloadUrl,
    triggerDownload,
    sharesMap,
    shareTarget,
    setShareTarget,
    upsertFileShare,
    respondShareRequest,
    notify,
    toasts,
  } = useCloud();

  const [draftRequireApproval, setDraftRequireApproval] = useState<boolean>(true);
  const [savingShare, setSavingShare] = useState<boolean>(false);
  const [copiedShareLink, setCopiedShareLink] = useState<boolean>(false);

  const activeShare = shareTarget ? sharesMap[shareTarget.path] : undefined;

  useEffect(() => {
    if (shareTarget) {
      const sh = sharesMap[shareTarget.path];
      setDraftRequireApproval(sh ? sh.require_approval : true);
      setCopiedShareLink(false);
    }
  }, [shareTarget, sharesMap]);

  async function handleEnableShare() {
    if (!shareTarget) return;
    setSavingShare(true);
    await upsertFileShare(shareTarget.path, shareTarget.size, true, draftRequireApproval, false);
    setSavingShare(false);
  }

  async function handleDisableShare() {
    if (!shareTarget || !activeShare) return;
    setSavingShare(true);
    await upsertFileShare(shareTarget.path, shareTarget.size, false, activeShare.require_approval, false);
    setSavingShare(false);
  }

  async function handleToggleRequireApproval(nextVal: boolean) {
    if (!shareTarget || !activeShare) return;
    setDraftRequireApproval(nextVal);
    setSavingShare(true);
    await upsertFileShare(shareTarget.path, shareTarget.size, activeShare.enabled, nextVal, false);
    setSavingShare(false);
  }

  async function handleRegenerateShareLink() {
    if (!shareTarget || !activeShare) return;
    if (!confirm("Đổi mã liên kết mới sẽ vô hiệu hóa liên kết cũ và xóa danh sách đã cấp quyền trước đó. Tiếp tục?")) {
      return;
    }
    setSavingShare(true);
    await upsertFileShare(shareTarget.path, shareTarget.size, true, activeShare.require_approval, true);
    setSavingShare(false);
  }

  function handleCopyShareUrl(shareId: string) {
    const url = buildFrontendShareUrl(shareId);
    navigator.clipboard.writeText(url);
    setCopiedShareLink(true);
    notify("Đã sao chép liên kết chia sẻ!", "success", url);
    setTimeout(() => setCopiedShareLink(false), 2500);
  }

  return (
    <>
      {/* 1. UPLOAD MODAL */}
      {uploadModalOpen && (
        <div className="fixed inset-0 z-50 flex items-center justify-center bg-black/75 backdrop-blur-sm p-4">
          <div className="glass-panel w-full max-w-lg rounded-3xl p-6 border border-white/15">
            <div className="flex items-center justify-between mb-4">
              <h3 className="text-base font-bold text-white flex items-center gap-2">
                <UploadCloud className="w-5 h-5 text-indigo-400" />
                <span>Tải tệp lên Upload ThTuanIT</span>
              </h3>
              <button onClick={() => setUploadModalOpen(false)} className="p-1.5 rounded-xl bg-white/5 text-slate-400">
                <X className="w-4 h-4" />
              </button>
            </div>

            <div className="space-y-4">
              <div>
                <label className="block text-xs font-medium text-slate-300 mb-1">Thư mục đích (tùy chọn)</label>
                <input
                  type="text"
                  value={uploadTargetFolder}
                  onChange={(e) => setUploadTargetFolder(e.target.value)}
                  placeholder="Ví dụ: videos/2026 hoặc để trống lưu ở gốc"
                  className="w-full bg-slate-900 border border-white/10 rounded-xl px-3.5 py-2.5 text-sm text-white font-mono focus:outline-none"
                />
              </div>

              <label className="flex flex-col items-center justify-center p-8 border-2 border-dashed border-white/15 hover:border-indigo-500 rounded-2xl bg-slate-900/50 cursor-pointer transition">
                <input
                  type="file"
                  multiple
                  className="hidden"
                  onChange={(e) => setSelectedFiles(Array.from(e.target.files || []))}
                />
                <UploadCloud className="w-10 h-10 text-indigo-400 mb-2" />
                <span className="text-sm font-medium text-slate-200">Nhấn để chọn nhiều tệp hoặc kéo thả</span>
                <span className="text-xs text-slate-400 mt-1">Tối đa 10 GiB mỗi tệp qua Upload ThTuanIT Turbo Engine</span>
              </label>

              {selectedFiles.length > 0 && (
                <div className="max-h-36 overflow-y-auto space-y-1.5">
                  {selectedFiles.map((f, i) => (
                    <div
                      key={i}
                      className="flex justify-between text-xs px-3 py-2 rounded-xl bg-slate-900 border border-white/10"
                    >
                      <span className="truncate text-slate-200">{f.name}</span>
                      <span className="font-mono text-slate-400 ml-2">{formatBytes(f.size)}</span>
                    </div>
                  ))}
                </div>
              )}

              <div className="flex justify-end gap-2 pt-2">
                <button
                  onClick={() => setUploadModalOpen(false)}
                  className="px-4 py-2 rounded-xl bg-white/5 text-xs text-slate-300"
                >
                  Hủy
                </button>
                <button
                  onClick={startBatchUpload}
                  className="px-5 py-2.5 rounded-xl bg-gradient-to-r from-indigo-600 to-violet-600 text-xs font-bold text-white"
                >
                  Bắt đầu tải lên ({selectedFiles.length})
                </button>
              </div>
            </div>
          </div>
        </div>
      )}

      {/* 2. RENAME / MOVE FILE MODAL */}
      {renameTarget && (
        <div className="fixed inset-0 z-50 flex items-center justify-center bg-black/75 backdrop-blur-sm p-4">
          <div className="glass-panel w-full max-w-md rounded-3xl p-6 border border-white/15">
            <div className="flex items-center justify-between mb-4">
              <h3 className="text-base font-bold text-white flex items-center gap-2">
                <Edit3 className="w-5 h-5 text-amber-400" />
                <span>Đổi tên / Di chuyển Tệp tin</span>
              </h3>
              <button onClick={() => setRenameTarget(null)} className="p-1.5 rounded-xl bg-white/5 text-slate-400">
                <X className="w-4 h-4" />
              </button>
            </div>

            <form onSubmit={handleRenameSubmit} className="space-y-4">
              <div>
                <label className="block text-xs text-slate-400 mb-1">Đường dẫn hiện tại</label>
                <p className="text-xs font-mono text-slate-300 bg-slate-900/70 px-3 py-2 rounded-xl border border-white/5">
                  {renameTarget.path}
                </p>
              </div>
              <div>
                <label className="block text-xs font-medium text-slate-200 mb-1">Đường dẫn / Tên mới</label>
                <input
                  type="text"
                  required
                  value={newPathInput}
                  onChange={(e) => setNewPathInput(e.target.value)}
                  className="w-full bg-slate-900 border border-white/15 focus:border-amber-400 rounded-xl px-3.5 py-2.5 text-sm text-white font-mono focus:outline-none"
                />
              </div>
              <div className="flex justify-end gap-2">
                <button
                  type="button"
                  onClick={() => setRenameTarget(null)}
                  className="px-4 py-2 rounded-xl bg-white/5 text-xs text-slate-300"
                >
                  Hủy
                </button>
                <button
                  type="submit"
                  disabled={renaming}
                  className="px-5 py-2 rounded-xl bg-amber-500 hover:bg-amber-400 text-slate-950 font-bold text-xs"
                >
                  {renaming ? "Đang đồng bộ..." : "Lưu thay đổi"}
                </button>
              </div>
            </form>
          </div>
        </div>
      )}

      {/* 3. QUICK SHARE MODAL (CHIA SẺ NHANH & PHÊ DUYỆT QUYỀN XEM) */}
      {shareTarget && (
        <div className="fixed inset-0 z-50 flex items-center justify-center bg-black/80 backdrop-blur-md p-4">
          <div className="glass-panel w-full max-w-xl rounded-3xl p-6 border border-indigo-500/30 space-y-5 max-h-[90vh] overflow-y-auto">
            <div className="flex items-center justify-between border-b border-white/10 pb-4">
              <div className="flex items-center gap-3 min-w-0">
                <div className="w-10 h-10 rounded-2xl bg-indigo-500/20 border border-indigo-500/40 flex items-center justify-center text-indigo-300 shrink-0">
                  <Share2 className="w-5 h-5" />
                </div>
                <div className="min-w-0">
                  <h3 className="text-base font-bold text-white truncate">Chia sẻ nhanh: {shareTarget.name}</h3>
                  <p className="text-xs text-slate-400 font-mono truncate">
                    {formatBytes(shareTarget.size)} • Chủ sở hữu: @{shareTarget.owner}
                  </p>
                </div>
              </div>
              <button
                onClick={() => setShareTarget(null)}
                className="p-1.5 rounded-xl bg-white/5 hover:bg-white/10 text-slate-400 hover:text-white"
              >
                <X className="w-4 h-4" />
              </button>
            </div>

            {/* IF NOT YET ENABLED -> CONFIRMATION TO ALLOW SHARING */}
            {!activeShare || !activeShare.enabled ? (
              <div className="space-y-4">
                <div className="p-4 rounded-2xl bg-amber-500/10 border border-amber-500/30 space-y-2">
                  <div className="flex items-center gap-2 text-amber-300 font-bold text-xs uppercase tracking-wider">
                    <Lock className="w-4 h-4" />
                    <span>Xác nhận cho phép chia sẻ tệp tin</span>
                  </div>
                  <p className="text-xs text-slate-300 leading-relaxed">
                    Tệp <b>{shareTarget.name}</b> hiện đang ở chế độ <b>Riêng tư</b>. Bạn có muốn tạo liên kết chia sẻ
                    để gửi cho mọi người xem được tệp này không? Bạn có thể <b>tắt chia sẻ bất cứ lúc nào</b>.
                  </p>
                </div>

                <div className="space-y-2.5">
                  <p className="text-xs font-bold text-slate-200 uppercase tracking-wider">
                    Chọn chế độ kiểm soát người xem:
                  </p>

                  <label
                    onClick={() => setDraftRequireApproval(true)}
                    className={`flex items-start gap-3 p-3.5 rounded-2xl border cursor-pointer transition ${
                      draftRequireApproval
                        ? "bg-indigo-500/15 border-indigo-500/50 text-white"
                        : "bg-slate-900/70 border-white/10 text-slate-400 hover:border-white/20"
                    }`}
                  >
                    <input
                      type="radio"
                      checked={draftRequireApproval}
                      onChange={() => setDraftRequireApproval(true)}
                      className="mt-1 accent-indigo-500"
                    />
                    <div className="text-xs space-y-1">
                      <p className="font-bold text-slate-100 flex items-center gap-1.5">
                        <Lock className="w-3.5 h-3.5 text-indigo-400" />
                        <span>Phải hỏi xin phép khi truy cập (Khuyên dùng)</span>
                      </p>
                      <p className="text-slate-400 leading-relaxed">
                        Người có link khi mở phải gửi yêu cầu và <b>chờ bạn bấm &quot;Cho phép&quot;</b> mới được xem
                        trước hoặc tải tệp xuống.
                      </p>
                    </div>
                  </label>

                  <label
                    onClick={() => setDraftRequireApproval(false)}
                    className={`flex items-start gap-3 p-3.5 rounded-2xl border cursor-pointer transition ${
                      !draftRequireApproval
                        ? "bg-emerald-500/15 border-emerald-500/50 text-white"
                        : "bg-slate-900/70 border-white/10 text-slate-400 hover:border-white/20"
                    }`}
                  >
                    <input
                      type="radio"
                      checked={!draftRequireApproval}
                      onChange={() => setDraftRequireApproval(false)}
                      className="mt-1 accent-emerald-500"
                    />
                    <div className="text-xs space-y-1">
                      <p className="font-bold text-slate-100 flex items-center gap-1.5">
                        <Unlock className="w-3.5 h-3.5 text-emerald-400" />
                        <span>Chia sẻ nhanh trực tiếp qua Link</span>
                      </p>
                      <p className="text-slate-400 leading-relaxed">
                        Bất kỳ ai nhận được liên kết chia sẻ này đều có thể xem và tải tệp ngay lập tức cho đến khi bạn
                        tắt liên kết.
                      </p>
                    </div>
                  </label>
                </div>

                <div className="flex items-center justify-end gap-2.5 pt-2">
                  <button
                    type="button"
                    onClick={() => setShareTarget(null)}
                    className="px-4 py-2.5 rounded-xl bg-white/5 hover:bg-white/10 text-xs font-semibold text-slate-300"
                  >
                    Không chia sẻ
                  </button>
                  <button
                    type="button"
                    disabled={savingShare}
                    onClick={handleEnableShare}
                    className="px-5 py-2.5 rounded-xl bg-gradient-to-r from-indigo-600 to-violet-600 hover:from-indigo-500 hover:to-violet-500 text-xs font-bold text-white shadow-lg shadow-indigo-600/30 flex items-center gap-2"
                  >
                    <Share2 className="w-4 h-4" />
                    <span>{savingShare ? "Đang tạo liên kết..." : "Cho phép & Bật Link Chia sẻ"}</span>
                  </button>
                </div>
              </div>
            ) : (
              /* WHEN SHARE IS ACTIVE (ENABLED) */
              <div className="space-y-5">
                {/* Status Banner & Quick OFF Button */}
                <div className="p-4 rounded-2xl bg-emerald-500/10 border border-emerald-500/30 flex flex-wrap items-center justify-between gap-3">
                  <div className="flex items-center gap-2.5">
                    <span className="w-2.5 h-2.5 rounded-full bg-emerald-400 animate-pulse" />
                    <div>
                      <p className="text-xs font-bold text-emerald-300">Liên kết chia sẻ đang BẬT</p>
                      <p className="text-[11px] text-slate-300">
                        {activeShare.require_approval
                          ? "Chế độ: Người xem phải hỏi xin phép và được bạn duyệt"
                          : "Chế độ: Ai có link đều xem & tải được ngay"}
                      </p>
                    </div>
                  </div>
                  <button
                    type="button"
                    disabled={savingShare}
                    onClick={handleDisableShare}
                    className="px-3.5 py-2 rounded-xl bg-rose-500/20 hover:bg-rose-500/30 border border-rose-500/40 text-xs font-bold text-rose-300 flex items-center gap-1.5 transition"
                  >
                    <Power className="w-3.5 h-3.5" />
                    <span>Tắt chia sẻ ngay</span>
                  </button>
                </div>

                {/* Share URL Box */}
                <div className="space-y-2">
                  <div className="flex items-center justify-between">
                    <label className="text-xs font-bold text-slate-300">
                      Liên kết gửi cho mọi người:
                    </label>
                    <button
                      type="button"
                      onClick={handleRegenerateShareLink}
                      className="text-[11px] text-indigo-300 hover:text-indigo-200 flex items-center gap-1 font-mono"
                    >
                      <RefreshCw className="w-3 h-3" /> Đổi mã link mới
                    </button>
                  </div>
                  <div className="flex items-center gap-2">
                    <input
                      type="text"
                      readOnly
                      value={buildFrontendShareUrl(activeShare.id)}
                      className="flex-1 bg-slate-950 border border-white/15 rounded-xl px-3.5 py-2.5 text-xs font-mono text-indigo-200 focus:outline-none"
                    />
                    <button
                      type="button"
                      onClick={() => handleCopyShareUrl(activeShare.id)}
                      className="px-4 py-2.5 rounded-xl bg-indigo-600 hover:bg-indigo-500 text-xs font-bold text-white flex items-center gap-1.5 shrink-0 transition"
                    >
                      {copiedShareLink ? <Check className="w-4 h-4 text-emerald-300" /> : <Copy className="w-4 h-4" />}
                      <span>{copiedShareLink ? "Đã chép" : "Sao chép"}</span>
                    </button>
                    <a
                      href={buildFrontendShareUrl(activeShare.id)}
                      target="_blank"
                      rel="noopener noreferrer"
                      className="p-2.5 rounded-xl bg-white/5 hover:bg-white/10 border border-white/10 text-slate-300 hover:text-white"
                      title="Mở trang chia sẻ trong tab mới"
                    >
                      <ExternalLink className="w-4 h-4" />
                    </a>
                  </div>
                </div>

                {/* Toggle Require Approval */}
                <div className="p-3.5 rounded-2xl bg-slate-900/90 border border-white/10 flex items-center justify-between gap-4">
                  <div className="space-y-0.5">
                    <p className="text-xs font-bold text-white flex items-center gap-1.5">
                      {activeShare.require_approval ? (
                        <Lock className="w-3.5 h-3.5 text-amber-400" />
                      ) : (
                        <Unlock className="w-3.5 h-3.5 text-emerald-400" />
                      )}
                      <span>Bắt buộc hỏi xin phép trước khi xem tệp</span>
                    </p>
                    <p className="text-[11px] text-slate-400">
                      Khi bật, người mở link phải gửi yêu cầu và chờ bạn nhấn &quot;Cho phép&quot; bên dưới.
                    </p>
                  </div>
                  <button
                    type="button"
                    disabled={savingShare}
                    onClick={() => handleToggleRequireApproval(!activeShare.require_approval)}
                    className={`px-3.5 py-1.5 rounded-xl text-xs font-bold border transition shrink-0 ${
                      activeShare.require_approval
                        ? "bg-amber-500/20 text-amber-300 border-amber-500/40"
                        : "bg-white/5 text-slate-400 border-white/10 hover:text-white"
                    }`}
                  >
                    {activeShare.require_approval ? "Đang BẬT hỏi phép" : "Đang TẮT hỏi phép"}
                  </button>
                </div>

                {/* Access Requests List */}
                <div className="space-y-2.5 pt-1 border-t border-white/10">
                  <div className="flex items-center justify-between">
                    <h4 className="text-xs font-bold uppercase tracking-wider text-slate-300">
                      Danh sách người xin quyền xem ({activeShare.requests?.length || 0})
                    </h4>
                    <span className="text-[11px] font-mono text-slate-400">Tự động cập nhật</span>
                  </div>

                  {!activeShare.requests || activeShare.requests.length === 0 ? (
                    <div className="p-5 rounded-2xl bg-slate-950/60 border border-white/5 text-center text-xs text-slate-400">
                      Chưa có yêu cầu xin quyền xem nào cho liên kết này.
                    </div>
                  ) : (
                    <div className="space-y-2 max-h-56 overflow-y-auto pr-1">
                      {activeShare.requests.map((req) => (
                        <div
                          key={req.request_id}
                          className="p-3 rounded-2xl bg-slate-900/90 border border-white/10 flex flex-wrap items-center justify-between gap-3"
                        >
                          <div className="min-w-0 flex-1">
                            <div className="flex items-center gap-2">
                              <span className="text-xs font-bold text-white truncate">{req.visitor_name}</span>
                              {req.status === "pending" && (
                                <span className="px-2 py-0.5 rounded-full text-[10px] font-mono bg-amber-500/20 text-amber-300 border border-amber-500/30 flex items-center gap-1">
                                  <Clock className="w-3 h-3" /> Chờ cho phép
                                </span>
                              )}
                              {req.status === "approved" && (
                                <span className="px-2 py-0.5 rounded-full text-[10px] font-mono bg-emerald-500/20 text-emerald-300 border border-emerald-500/30">
                                  ✓ Đã cho phép
                                </span>
                              )}
                              {req.status === "rejected" && (
                                <span className="px-2 py-0.5 rounded-full text-[10px] font-mono bg-rose-500/20 text-rose-300 border border-rose-500/30">
                                  ✕ Đã từ chối
                                </span>
                              )}
                            </div>
                            {req.note && <p className="text-xs text-slate-300 mt-0.5 italic">&ldquo;{req.note}&rdquo;</p>}
                            <p className="text-[10px] font-mono text-slate-500 mt-0.5">{req.requested_at}</p>
                          </div>

                          <div className="flex items-center gap-1.5">
                            {req.status !== "approved" && (
                              <button
                                type="button"
                                onClick={() => respondShareRequest(activeShare.id, req.request_id, "approved")}
                                className="px-3 py-1.5 rounded-xl bg-emerald-600 hover:bg-emerald-500 text-xs font-bold text-white flex items-center gap-1 transition"
                              >
                                <UserCheck className="w-3.5 h-3.5" />
                                <span>Cho phép</span>
                              </button>
                            )}
                            {req.status !== "rejected" && (
                              <button
                                type="button"
                                onClick={() => respondShareRequest(activeShare.id, req.request_id, "rejected")}
                                className="px-3 py-1.5 rounded-xl bg-rose-500/20 hover:bg-rose-500/30 border border-rose-500/30 text-xs font-semibold text-rose-300 flex items-center gap-1 transition"
                              >
                                <UserX className="w-3.5 h-3.5" />
                                <span>{req.status === "approved" ? "Thu hồi" : "Từ chối"}</span>
                              </button>
                            )}
                          </div>
                        </div>
                      ))}
                    </div>
                  )}
                </div>
              </div>
            )}
          </div>
        </div>
      )}

      {/* 4. ADMIN EDIT / CREATE USER MODAL */}
      {editingUser && (
        <div className="fixed inset-0 z-50 flex items-center justify-center bg-black/75 backdrop-blur-sm p-4">
          <div className="glass-panel w-full max-w-md rounded-3xl p-6 border border-amber-500/30">
            <div className="flex items-center justify-between mb-4">
              <h3 className="text-base font-bold text-white flex items-center gap-2">
                <Shield className="w-5 h-5 text-amber-400" />
                <span>{editingUser.isNew ? "Thêm người dùng mới" : `Chỉnh sửa @${editingUser.username}`}</span>
              </h3>
              <button onClick={() => setEditingUser(null)} className="p-1.5 rounded-xl bg-white/5 text-slate-400">
                <X className="w-4 h-4" />
              </button>
            </div>

            <form onSubmit={handleSaveUser} className="space-y-4">
              <div>
                <label className="block text-xs font-medium text-slate-300 mb-1">Tên đăng nhập (Username)</label>
                <input
                  type="text"
                  required
                  disabled={!editingUser.isNew}
                  value={editingUser.username}
                  onChange={(e) => setEditingUser({ ...editingUser, username: e.target.value })}
                  className="w-full bg-slate-900 border border-white/15 rounded-xl px-3.5 py-2 text-sm text-white font-mono disabled:opacity-60"
                />
              </div>

              <div>
                <label className="block text-xs font-medium text-slate-300 mb-1">Tên hiển thị</label>
                <input
                  type="text"
                  required
                  value={editingUser.display_name}
                  onChange={(e) => setEditingUser({ ...editingUser, display_name: e.target.value })}
                  className="w-full bg-slate-900 border border-white/15 rounded-xl px-3.5 py-2 text-sm text-white"
                />
              </div>

              <div className="grid grid-cols-2 gap-3">
                <div>
                  <label className="block text-xs font-medium text-slate-300 mb-1">Vai trò (Role)</label>
                  <select
                    value={editingUser.role}
                    disabled={editingUser.username === "admin"}
                    onChange={(e) => setEditingUser({ ...editingUser, role: e.target.value as "admin" | "user" })}
                    className="w-full bg-slate-900 border border-white/15 rounded-xl px-3 py-2 text-sm text-white"
                  >
                    <option value="user">User (Thành viên)</option>
                    <option value="admin">Admin (Quản trị viên)</option>
                  </select>
                </div>

                <div>
                  <label className="block text-xs font-medium text-slate-300 mb-1">
                    {editingUser.isNew ? "Mật khẩu *" : "Đặt mật khẩu mới"}
                  </label>
                  <input
                    type="password"
                    required={editingUser.isNew}
                    value={editingUser.password}
                    onChange={(e) => setEditingUser({ ...editingUser, password: e.target.value })}
                    placeholder="••••••••"
                    className="w-full bg-slate-900 border border-white/15 rounded-xl px-3.5 py-2 text-sm text-white font-mono"
                  />
                </div>
              </div>

              {editingUser.role !== "admin" && (
                <div className="p-3.5 rounded-2xl bg-slate-900/90 border border-white/10 space-y-2.5">
                  <p className="text-xs font-semibold text-slate-300">Quyền hạn chi tiết:</p>
                  <label className="flex items-center justify-between text-xs text-slate-300 cursor-pointer">
                    <span>Cho phép tải tệp lên (Upload)</span>
                    <input
                      type="checkbox"
                      checked={editingUser.permissions.can_upload}
                      onChange={(e) =>
                        setEditingUser({
                          ...editingUser,
                          permissions: { ...editingUser.permissions, can_upload: e.target.checked },
                        })
                      }
                      className="rounded accent-indigo-500"
                    />
                  </label>
                  <label className="flex items-center justify-between text-xs text-slate-300 cursor-pointer">
                    <span>Cho phép đổi tên / di chuyển tệp (Rename)</span>
                    <input
                      type="checkbox"
                      checked={editingUser.permissions.can_rename}
                      onChange={(e) =>
                        setEditingUser({
                          ...editingUser,
                          permissions: { ...editingUser.permissions, can_rename: e.target.checked },
                        })
                      }
                      className="rounded accent-indigo-500"
                    />
                  </label>
                  <label className="flex items-center justify-between text-xs text-slate-300 cursor-pointer">
                    <span>Cho phép xóa tệp tin khỏi Bucket (Delete)</span>
                    <input
                      type="checkbox"
                      checked={editingUser.permissions.can_delete}
                      onChange={(e) =>
                        setEditingUser({
                          ...editingUser,
                          permissions: { ...editingUser.permissions, can_delete: e.target.checked },
                        })
                      }
                      className="rounded accent-indigo-500"
                    />
                  </label>
                </div>
              )}

              <div className="flex justify-end gap-2 pt-2">
                <button
                  type="button"
                  onClick={() => setEditingUser(null)}
                  className="px-4 py-2 rounded-xl bg-white/5 text-xs text-slate-300"
                >
                  Hủy
                </button>
                <button
                  type="submit"
                  className="px-5 py-2 rounded-xl bg-amber-500 hover:bg-amber-400 text-slate-950 font-bold text-xs"
                >
                  Lưu cấu hình
                </button>
              </div>
            </form>
          </div>
        </div>
      )}

      {/* 5. FULLSCREEN MEDIA PREVIEW MODAL */}
      {previewFile && (
        <div className="fixed inset-0 z-50 flex items-center justify-center bg-black/85 backdrop-blur-md p-4">
          <div className="glass-panel w-full max-w-4xl rounded-3xl overflow-hidden border border-white/15 flex flex-col max-h-[90vh]">
            <div className="px-6 py-4 border-b border-white/10 flex items-center justify-between gap-4">
              <div className="min-w-0">
                <div className="flex items-center gap-2">
                  <h3 className="text-base font-bold text-white truncate">{previewFile.name}</h3>
                  <span className="px-2 py-0.5 rounded text-[10px] font-mono bg-indigo-500/20 text-indigo-300 border border-indigo-500/30">
                    @{previewFile.owner}
                  </span>
                </div>
                <p className="text-xs text-slate-400 font-mono truncate">
                  {formatBytes(previewFile.size)} • cloud://thtuanit/{previewFile.path}
                </p>
              </div>
              <div className="flex items-center gap-2">
                <button
                  onClick={() => {
                    const target = previewFile;
                    setPreviewFile(null);
                    setShareTarget(target);
                  }}
                  className="px-3.5 py-1.5 rounded-xl bg-emerald-600/20 hover:bg-emerald-600/30 border border-emerald-500/40 text-xs font-semibold text-emerald-300 flex items-center gap-1.5"
                >
                  <Share2 className="w-3.5 h-3.5" /> Chia sẻ nhanh
                </button>
                <button
                  onClick={() => triggerDownload(previewFile)}
                  className="px-3.5 py-1.5 rounded-xl bg-indigo-600 hover:bg-indigo-500 text-xs font-semibold text-white flex items-center gap-1.5"
                >
                  <Download className="w-3.5 h-3.5" /> Tải xuống
                </button>
                <button
                  onClick={() => setPreviewFile(null)}
                  className="p-1.5 rounded-xl bg-white/5 hover:bg-white/10 text-slate-300"
                >
                  <X className="w-4 h-4" />
                </button>
              </div>
            </div>
            <div className="p-6 flex-1 overflow-auto flex items-center justify-center bg-slate-950/80 min-h-[340px]">
              {previewFile.category === "video" ? (
                <video
                  controls
                  autoPlay
                  src={getDownloadUrl(previewFile.path, true)}
                  className="max-h-[68vh] w-full rounded-2xl bg-black shadow-2xl"
                />
              ) : previewFile.category === "image" ? (
                <img
                  src={getDownloadUrl(previewFile.path, true)}
                  alt={previewFile.name}
                  className="max-h-[68vh] rounded-2xl object-contain"
                />
              ) : (
                <div className="text-center py-10">
                  <p className="text-sm text-slate-300 mb-4">
                    Tệp <b>{previewFile.name}</b> ({formatBytes(previewFile.size)}) sẵn sàng để tải về máy.
                  </p>
                  <button
                    onClick={() => triggerDownload(previewFile)}
                    className="px-6 py-3 rounded-xl bg-indigo-600 hover:bg-indigo-500 text-xs font-bold text-white"
                  >
                    Tải tệp tin về máy ngay
                  </button>
                </div>
              )}
            </div>
          </div>
        </div>
      )}

      <ToastStack toasts={toasts} />
    </>
  );
}

export function ToastStack({ toasts }: { toasts: ToastMsg[] }) {
  return (
    <div className="fixed bottom-5 right-5 z-50 flex flex-col gap-2.5 max-w-sm w-full pointer-events-none">
      {toasts.map((t) => (
        <div
          key={t.id}
          className={`pointer-events-auto glass px-4 py-3 rounded-2xl border shadow-xl flex items-start gap-3 ${
            t.type === "success"
              ? "border-emerald-500/40 text-emerald-200"
              : t.type === "error"
              ? "border-rose-500/40 text-rose-200"
              : "border-indigo-500/40 text-slate-200"
          }`}
        >
          {t.type === "success" ? (
            <CheckCircle2 className="w-5 h-5 text-emerald-400 shrink-0 mt-0.5" />
          ) : t.type === "error" ? (
            <AlertTriangle className="w-5 h-5 text-rose-400 shrink-0 mt-0.5" />
          ) : (
            <Sparkles className="w-5 h-5 text-indigo-400 shrink-0 mt-0.5" />
          )}
          <div className="min-w-0 flex-1">
            <p className="text-xs font-semibold">{t.title}</p>
            {t.detail && <p className="text-[11px] opacity-80 font-mono truncate mt-0.5">{t.detail}</p>}
          </div>
        </div>
      ))}
    </div>
  );
}