<?php
/**
 * THUAN THIEN CORP - DATA ARCHIVE SYSTEM
 * Phiên bản: Blue Modern - Nâng cấp Duyệt Thư Mục & Chống truy cập chéo
 */

session_start();
date_default_timezone_set('Asia/Ho_Chi_Minh');

// ==========================================
// 1. CẤU HÌNH HỆ THỐNG (Người quản trị khai báo tại đây)
// ==========================================
$config = [
    'project_id'     => 'PHUONG_LINH_GROUP_2024', // ID riêng biệt để chống đăng nhập chéo với dự án khác
    'customer_name'  => 'CÔNG TY CP TẬP ĐOÀN CƠ ĐIỆN PHƯƠNG LINH',
    'customer_tax_id'=> '0101033805',
    'customer_addr'  => 'Lô A221 Đường số 3, Khu CN Thái Hoà, Xã Đức Lập, Tỉnh Tây Ninh',
    'customer_bank'  => '', // (Thông tin chưa cung cấp)
    'customer_buyer' => 'Hân (P. Mua hàng) - 0934 138 511',
    'passwords'      => [
        '0101033805',  // Loại 1: Mật khẩu theo MST công ty
        '212781166'    // Loại 2: Mật khẩu mặc định hệ thống
    ],
    'support_info'   => [
        'name'  => 'Mr. TÂM',
        'phone' => '0935 125 637'
    ],
    // Cấu hình Cloudflare Turnstile
    'turnstile_site_key'   => '0x4AAAAAAC1l7RrGHDF-ross',
    'turnstile_secret_key' => '0x4AAAAAAC1l7apRayNNf5X2jSK-QGZEWgs',
];

// ==========================================
// 2. XỬ LÝ BẢO MẬT & ĐĂNG NHẬP
// ==========================================
$error = "";
$session_key = 'auth_data_' . $config['project_id'];

// Xử lý đăng xuất
if (isset($_GET['logout'])) {
    unset($_SESSION[$session_key]);
    session_destroy();
    header("Location: index.php");
    exit;
}

// Kiểm tra đăng nhập
if (isset($_POST['access_password'])) {
    $user_pass = $_POST['access_password'];
    $turnstile_response = $_POST['cf-turnstile-response'] ?? '';

    // Xác thực Turnstile
    $verify = file_get_contents("https://challenges.cloudflare.com/turnstile/v0/siteverify", false, stream_context_create([
        'http' => [
            'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
            'method'  => 'POST',
            'content' => http_build_query([
                'secret'   => $config['turnstile_secret_key'],
                'response' => $turnstile_response,
            ]),
        ],
    ]));
    $verify_data = json_decode($verify);

    if (in_array($user_pass, $config['passwords']) && $verify_data->success) {
        $_SESSION[$session_key] = true;
        $_SESSION['user_agent_' . $config['project_id']] = $_SERVER['HTTP_USER_AGENT'];
    } else {
        $error = (!$verify_data->success) ? "Vui lòng hoàn thành xác minh bảo mật." : "Mật khẩu không chính xác cho dự án này.";
    }
}

$is_logged_in = isset($_SESSION[$session_key]) && $_SESSION[$session_key] === true;

// ==========================================
// 3. LOGIC DUYỆT THƯ MỤC & TỆP TIN
// ==========================================
$exclude_files = ['index.php', '.htaccess', '.htpasswd', 'error_log', 'cgi-bin'];

// Xử lý đường dẫn thư mục hiện tại
$base_dir = ".";
$rel_path = "";
if (isset($_GET['dir'])) {
    // Bảo mật: Loại bỏ các ký tự điều hướng thư mục nguy hiểm
    $rel_path = str_replace(['..', './', '\\'], '', $_GET['dir']);
    $rel_path = trim($rel_path, '/');
}
$current_scan_dir = $rel_path ? $base_dir . '/' . $rel_path : $base_dir;

function get_file_info($filename, $is_dir = false) {
    if ($is_dir) {
        return ['icon' => 'fa-folder', 'color' => 'text-amber-400', 'bg' => 'bg-amber-50', 'viewable' => false];
    }
    $ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
    switch ($ext) {
        case 'pdf': return ['icon' => 'fa-file-pdf', 'color' => 'text-red-500', 'bg' => 'bg-red-50', 'viewable' => true];
        case 'doc':
        case 'docx': return ['icon' => 'fa-file-word', 'color' => 'text-blue-500', 'bg' => 'bg-blue-50', 'viewable' => false];
        case 'xls':
        case 'xlsx': return ['icon' => 'fa-file-excel', 'color' => 'text-emerald-500', 'bg' => 'bg-emerald-50', 'viewable' => false];
        case 'zip':
        case 'rar': return ['icon' => 'fa-file-archive', 'color' => 'text-amber-500', 'bg' => 'bg-amber-50', 'viewable' => false];
        case 'jpg':
        case 'png':
        case 'jpeg':
        case 'svg': return ['icon' => 'fa-file-image', 'color' => 'text-indigo-500', 'bg' => 'bg-indigo-50', 'viewable' => true];
        default: return ['icon' => 'fa-file', 'color' => 'text-slate-400', 'bg' => 'bg-slate-50', 'viewable' => false];
    }
}

function get_file_category($filename, $is_dir = false) {
    if ($is_dir) return ['text' => 'Thư mục', 'class' => 'bg-amber-50 text-amber-600 border-amber-100'];
    $fn = strtolower($filename);
    if (stripos($fn, 'baogia') === 0) return ['text' => 'Báo Giá', 'class' => 'bg-blue-50 text-blue-600 border-blue-100'];
    if (stripos($fn, 'hd') === 0 || stripos($fn, 'hopdong') === 0) return ['text' => 'Hợp Đồng', 'class' => 'bg-cyan-50 text-cyan-700 border-cyan-100'];
    if (stripos($fn, 'hoadon') === 0) return ['text' => 'Hóa Đơn', 'class' => 'bg-sky-50 text-sky-600 border-sky-100'];
    if (stripos($fn, 'po') === 0) return ['text' => 'Đơn Hàng (PO)', 'class' => 'bg-indigo-50 text-indigo-600 border-indigo-100'];
    if (stripos($fn, 'xuatkho') === 0) return ['text' => 'Xuất Kho', 'class' => 'bg-slate-50 text-slate-600 border-slate-100'];
    if (stripos($fn, 'sanpham') === 0) return ['text' => 'Sản Phẩm', 'class' => 'bg-blue-50 text-blue-500 border-blue-100'];
    
    return ['text' => 'Tài Liệu', 'class' => 'bg-gray-50 text-gray-400 border-gray-100'];
}

function format_size($bytes) {
    if ($bytes >= 1073741824) return number_format($bytes / 1073741824, 2) . ' GB';
    if ($bytes >= 1048576) return number_format($bytes / 1048576, 2) . ' MB';
    if ($bytes >= 1024) return number_format($bytes / 1024, 2) . ' KB';
    return $bytes . ' bytes';
}

$items = [];
if ($is_logged_in && is_dir($current_scan_dir)) {
    if ($dh = opendir($current_scan_dir)) {
        while (($file = readdir($dh)) !== false) {
            if ($file != "." && $file != ".." && !in_array($file, $exclude_files)) {
                $full_path = $current_scan_dir . '/' . $file;
                $is_directory = is_dir($full_path);
                $timestamp = filemtime($full_path);
                
                $items[] = [
                    'name'      => $file,
                    'is_dir'    => $is_directory,
                    'rel_url'   => ($rel_path ? $rel_path . '/' : '') . $file,
                    'size'      => $is_directory ? '--' : format_size(filesize($full_path)),
                    'date'      => date("d/m/Y H:i", $timestamp),
                    'timestamp' => $timestamp,
                    'info'      => get_file_info($file, $is_directory),
                    'category'  => get_file_category($file, $is_directory)
                ];
            }
        }
        closedir($dh);
    }
    usort($items, function($a, $b) {
        if ($a['is_dir'] && !$b['is_dir']) return -1;
        if (!$a['is_dir'] && $b['is_dir']) return 1;
        return $b['timestamp'] - $a['timestamp'];
    });
}
?>

<!DOCTYPE html>
<html lang="vi">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>HỆ THỐNG LƯU TRỮ DATA | Thuan Thien Corp</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@300;400;500;600;700;800&display=swap" rel="stylesheet">
    <script src="https://challenges.cloudflare.com/turnstile/v0/api.js" async defer></script>
    <style>
        body { font-family: 'Plus Jakarta Sans', sans-serif; background-color: #f0f7ff; color: #1e293b; }
        .bg-main-blue { background-color: #0056b3; }
        .text-main-blue { color: #0056b3; }
        .glass-effect { background: rgba(255, 255, 255, 0.9); backdrop-filter: blur(10px); }
        .file-card { transition: all 0.3s ease; border: 1px solid #e2e8f0; }
        .file-card:hover { transform: translateY(-4px); border-color: #bfdbfe; box-shadow: 0 10px 25px -5px rgba(0, 86, 179, 0.1); }
        .quick-view-modal { display: none; }
        .quick-view-active { display: flex; }
        input[type="password"] { font-family: 'Inter', sans-serif; }
        .btn-blue { background-color: #006ce6; transition: background 0.2s; }
        .btn-blue:hover { background-color: #0056b3; }
    </style>
</head>
<body class="min-h-screen flex flex-col">

    <?php if (!$is_logged_in): ?>
    <!-- GIAO DIỆN ĐĂNG NHẬP -->
    <div class="flex-1 flex items-center justify-center p-6 bg-gradient-to-br from-blue-50 to-indigo-50">
        <div class="max-w-md w-full bg-white rounded-[2rem] shadow-xl border border-blue-100 p-10 text-center relative overflow-hidden">
            <div class="absolute top-0 left-0 w-full h-1.5 bg-main-blue"></div>
            
            <div class="inline-flex items-center justify-center w-20 h-20 bg-blue-50 text-main-blue rounded-3xl mb-6 ring-4 ring-blue-50">
                <i class="fas fa-lock-open text-3xl"></i>
            </div>
            
            <h2 class="text-2xl font-extrabold text-slate-800 tracking-tight uppercase">Xác minh truy cập</h2>
            <p class="text-slate-400 mt-2 mb-8 font-medium text-sm italic">Hệ thống lưu trữ bảo mật Thuan Thien Corp</p>

            <form method="POST" class="space-y-6 text-left">
                <div class="space-y-2">
                    <label class="text-[10px] font-bold text-slate-400 uppercase tracking-widest ml-1">Mật khẩu hệ thống</label>
                    <div class="relative">
                        <span class="absolute inset-y-0 left-0 pl-4 flex items-center text-slate-300">
                            <i class="fas fa-fingerprint text-sm"></i>
                        </span>
                        <input type="password" name="access_password" autofocus required
                               class="w-full pl-11 pr-5 py-4 rounded-2xl border border-slate-200 focus:border-blue-400 focus:ring-4 focus:ring-blue-50 outline-none transition-all text-center font-bold tracking-[0.2em]"
                               placeholder="********">
                    </div>
                </div>

                <div class="flex justify-center py-1">
                    <div class="cf-turnstile" data-sitekey="<?= $config['turnstile_site_key'] ?>" data-theme="light"></div>
                </div>

                <?php if ($error): ?>
                    <div class="bg-red-50 text-red-500 p-3 rounded-xl text-xs font-semibold flex items-center gap-2 border border-red-100">
                        <i class="fas fa-circle-exclamation"></i> <?= $error ?>
                    </div>
                <?php endif; ?>

                <button type="submit" class="w-full btn-blue text-white font-bold py-4 rounded-2xl shadow-lg shadow-blue-100 transition-all active:scale-95 flex items-center justify-center gap-3">
                    Đăng nhập hệ thống <i class="fas fa-chevron-right text-xs"></i>
                </button>
            </form>
            
            <div class="mt-8 pt-6 border-t border-slate-50 text-[9px] text-slate-300 uppercase font-bold tracking-widest">© Thuan Thien Corporation</div>
        </div>
    </div>
    <?php else: ?>

    <!-- GIAO DIỆN CHÍNH -->
    <header class="glass-effect border-b border-blue-100 sticky top-0 z-50">
        <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
            <div class="flex justify-between items-center h-20">
                <div class="flex items-center gap-4">
                    <div class="bg-main-blue p-2.5 rounded-2xl shadow-md transition-transform hover:scale-105">
                        <i class="fas fa-server text-white text-lg"></i>
                    </div>
                    <div>
                        <h1 class="text-lg font-extrabold text-slate-800 leading-none uppercase">THUẬN THIÊN CORP</h1>
                        <p class="text-[10px] text-main-blue font-bold tracking-widest uppercase mt-1 italic">Hệ thống lưu trữ DATA</p>
                    </div>
                </div>
                
                <div class="flex-1 max-w-lg mx-8 hidden lg:block">
                    <div class="relative group">
                        <span class="absolute inset-y-0 left-0 pl-5 flex items-center text-slate-400 group-focus-within:text-main-blue transition-colors">
                            <i class="fas fa-search text-xs"></i>
                        </span>
                        <input type="text" id="searchInput" onkeyup="filterFiles()" 
                               class="block w-full pl-11 pr-4 py-3 border border-slate-100 rounded-2xl bg-slate-50/50 focus:bg-white focus:ring-4 focus:ring-blue-50 sm:text-sm transition-all" 
                               placeholder="Tìm kiếm tài liệu nội bộ...">
                    </div>
                </div>

                <div class="flex items-center gap-4">
                    <div class="text-right hidden sm:block">
                        <p class="text-[9px] font-bold text-slate-300 uppercase leading-none mb-1">Support</p>
                        <p class="text-xs font-bold text-slate-700"><?= $config['support_info']['name'] ?></p>
                    </div>
                    <a href="?logout=1" class="w-10 h-10 flex items-center justify-center bg-blue-50 text-main-blue rounded-xl hover:bg-red-500 hover:text-white transition-all shadow-sm" title="Thoát">
                        <i class="fas fa-power-off text-sm"></i>
                    </a>
                </div>
            </div>
        </div>
    </header>

    <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-10 w-full flex-1">
        <!-- Project Information Card -->
        <div class="bg-white rounded-[2.5rem] p-8 shadow-sm border border-white mb-8 relative overflow-hidden flex flex-col md:flex-row gap-8 items-center">
            <div class="absolute top-0 right-0 p-8 opacity-5">
                <i class="fas fa-briefcase text-9xl"></i>
            </div>
            
            <div class="w-24 h-24 bg-blue-50 text-main-blue rounded-[2rem] flex items-center justify-center text-4xl shrink-0">
                <i class="fas fa-user-tie"></i>
            </div>

            <div class="flex-1 text-center md:text-left z-10">
                <p class="text-main-blue text-[10px] font-bold uppercase tracking-[0.3em] mb-2">Thông tin đối tác</p>
                <h2 class="text-2xl font-black text-slate-800 mb-3 leading-tight"><?= $config['customer_name'] ?></h2>
                
                <div class="grid grid-cols-1 md:grid-cols-2 gap-y-2 gap-x-6 text-sm text-slate-500">
                    <div class="flex items-center gap-2"><i class="fas fa-barcode text-main-blue opacity-50 w-4"></i><span class="font-semibold">MST:</span> <?= $config['customer_tax_id'] ?></div>
                    <div class="flex items-center gap-2"><i class="fas fa-location-dot text-main-blue opacity-50 w-4"></i><span class="font-semibold">Địa chỉ:</span> <?= $config['customer_addr'] ?></div>
                    <div class="flex items-center gap-2"><i class="fas fa-credit-card text-main-blue opacity-50 w-4"></i><span class="font-semibold">Ngân hàng:</span> <?= $config['customer_bank'] ?: 'Đang cập nhật' ?></div>
                    <div class="flex items-center gap-2"><i class="fas fa-user-check text-main-blue opacity-50 w-4"></i><span class="font-semibold">Mua hàng:</span> <?= $config['customer_buyer'] ?></div>
                </div>
            </div>

            <div class="shrink-0 flex flex-col gap-2 w-full md:w-auto min-w-[200px]">
                <div class="bg-blue-50/50 p-4 rounded-2xl border border-blue-50 text-center">
                    <p class="text-[9px] font-bold text-main-blue uppercase tracking-widest mb-1 leading-none italic">Tư vấn bán hàng</p>
                    <a href="tel:<?= str_replace(' ', '', $config['support_info']['phone']) ?>" class="text-sm font-black text-slate-700 flex items-center justify-center gap-2">
                         <i class="fas fa-phone-volume text-main-blue animate-pulse"></i> <?= $config['support_info']['phone'] ?>
                    </a>
                </div>
            </div>
        </div>

        <!-- Breadcrumbs Navigation -->
        <div class="flex items-center justify-between mb-8 px-2 overflow-x-auto">
            <nav class="flex items-center gap-2 text-xs font-bold text-slate-400">
                <a href="index.php" class="hover:text-main-blue flex items-center gap-1">
                    <i class="fas fa-house"></i> Gốc
                </a>
                <?php 
                if ($rel_path) {
                    $parts = explode('/', $rel_path);
                    $path_acc = "";
                    foreach ($parts as $part) {
                        $path_acc .= ($path_acc ? '/' : '') . $part;
                        echo '<i class="fas fa-chevron-right text-[10px] opacity-30"></i>';
                        echo '<a href="?dir=' . urlencode($path_acc) . '" class="hover:text-main-blue">' . htmlspecialchars($part) . '</a>';
                    }
                }
                ?>
            </nav>
            <div class="flex items-center gap-4 shrink-0 ml-4">
                <button onclick="location.reload()" class="text-xs font-bold text-slate-400 hover:text-main-blue transition-colors flex items-center gap-2 uppercase tracking-widest">
                    <i class="fas fa-rotate text-[10px]"></i> Làm mới
                </button>
            </div>
        </div>

        <!-- Items Grid -->
        <div id="fileGrid" class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-6">
            <?php if ($rel_path): ?>
                <?php 
                    $parent_dir = dirname($rel_path);
                    $parent_link = ($parent_dir == "." || $parent_dir == "") ? "index.php" : "?dir=" . urlencode($parent_dir);
                ?>
                <a href="<?= $parent_link ?>" class="file-item bg-blue-50/30 rounded-3xl p-6 border-2 border-dashed border-blue-100 flex items-center gap-4 transition-all hover:bg-blue-100/50 group">
                    <div class="w-12 h-12 bg-white rounded-xl flex items-center justify-center text-blue-400 shadow-sm group-hover:scale-110 transition-transform">
                        <i class="fas fa-arrow-left"></i>
                    </div>
                    <div>
                        <p class="text-sm font-black text-blue-700">Quay lại</p>
                        <p class="text-[10px] text-blue-400 font-bold uppercase tracking-widest">Thư mục cha</p>
                    </div>
                </a>
            <?php endif; ?>

            <?php foreach ($items as $item): ?>
            <div class="file-item bg-white rounded-3xl p-6 file-card flex flex-col group relative" data-name="<?= strtolower($item['name']) ?>">
                <div class="flex justify-between items-start mb-5">
                    <div class="<?= $item['info']['bg'] ?> <?= $item['info']['color'] ?> w-14 h-14 rounded-2xl flex items-center justify-center text-2xl transition-transform group-hover:scale-110">
                        <i class="fas <?= $item['info']['icon'] ?>"></i>
                    </div>
                    <span class="px-3 py-1 rounded-lg text-[9px] font-black border uppercase tracking-wider <?= $item['category']['class'] ?>">
                        <?= $item['category']['text'] ?>
                    </span>
                </div>
                <div class="mb-6">
                    <h3 class="text-slate-800 font-bold text-sm mb-1 truncate pr-2 group-hover:text-main-blue transition-colors" title="<?= $item['name'] ?>">
                        <?= $item['name'] ?>
                    </h3>
                    <p class="text-[10px] text-slate-400 font-medium italic">
                        <?= $item['is_dir'] ? 'Thư mục hệ thống' : 'Cập nhật: ' . $item['date'] ?>
                    </p>
                </div>
                <div class="mt-auto pt-4 border-t border-slate-50 flex items-center justify-between">
                    <div>
                        <p class="text-[9px] text-slate-300 uppercase font-black leading-none mb-1 tracking-tighter">Dung lượng</p>
                        <p class="text-xs font-bold text-slate-500"><?= $item['size'] ?></p>
                    </div>
                    <div class="flex gap-1.5">
                        <?php if ($item['is_dir']): ?>
                            <a href="?dir=<?= urlencode($item['rel_url']) ?>" class="w-9 h-9 bg-main-blue text-white rounded-xl hover:bg-slate-800 transition-all flex items-center justify-center shadow-md active:scale-90" title="Mở thư mục">
                                <i class="fas fa-folder-open text-xs"></i>
                            </a>
                        <?php else: ?>
                            <?php if ($item['info']['viewable']): ?>
                            <button onclick="openQuickView('<?= $item['rel_url'] ?>', '<?= pathinfo($item['name'], PATHINFO_EXTENSION) ?>')" class="w-9 h-9 bg-blue-50 text-main-blue rounded-xl hover:bg-main-blue hover:text-white transition-all flex items-center justify-center shadow-sm" title="Xem nhanh">
                                <i class="fas fa-eye text-xs"></i>
                            </button>
                            <?php endif; ?>
                            <a href="<?= $item['rel_url'] ?>" download class="w-9 h-9 bg-slate-800 text-white rounded-xl hover:bg-main-blue transition-all flex items-center justify-center shadow-md active:scale-90" title="Tải về">
                                <i class="fas fa-download text-xs"></i>
                            </a>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
            <?php endforeach; ?>
        </div>
    </div>

    <!-- Quick View Modal -->
    <div id="quickViewModal" class="quick-view-modal fixed inset-0 z-[100] items-center justify-center p-4 md:p-10 bg-slate-900/80 backdrop-blur-sm">
        <div class="bg-white w-full max-w-6xl h-full rounded-[2.5rem] overflow-hidden flex flex-col relative shadow-2xl animate-in fade-in zoom-in duration-300">
            <div class="px-8 py-5 border-b flex justify-between items-center bg-slate-50/50">
                <div class="flex items-center gap-3">
                    <i class="fas fa-magnifying-glass-plus text-main-blue"></i>
                    <h3 id="modalTitle" class="font-bold truncate max-w-xs md:max-w-xl text-slate-800">Tên tệp</h3>
                </div>
                <button onclick="closeQuickView()" class="w-10 h-10 rounded-xl hover:bg-red-50 text-slate-400 hover:text-red-500 transition-all flex items-center justify-center">
                    <i class="fas fa-times text-lg"></i>
                </button>
            </div>
            <div id="modalContent" class="flex-1 bg-slate-100 flex items-center justify-center overflow-auto"></div>
        </div>
    </div>

    <footer class="bg-white border-t border-slate-100 py-12 mt-auto text-center">
        <p class="text-[10px] text-slate-400 font-bold uppercase tracking-[0.4em] mb-4">© <?= date('Y') ?> Thuan Thien Corp</p>
        <p class="text-[10px] text-slate-300 max-w-lg mx-auto italic font-medium">Hệ thống lưu trữ bảo mật dành cho đối tác chính thức. <br>Mọi truy cập đều được ghi lại để đảm bảo an toàn dữ liệu.</p>
    </footer>
    <?php endif; ?>

    <script>
        function filterFiles() {
            let input = document.getElementById('searchInput').value.toLowerCase();
            let grid = document.getElementById('fileGrid');
            let items = grid.getElementsByClassName('file-item');
            for (let i = 0; i < items.length; i++) {
                let name = items[i].getAttribute('data-name');
                if (name && name.includes(input)) items[i].classList.remove('hidden');
                else if (name) items[i].classList.add('hidden');
            }
        }
        const modal = document.getElementById('quickViewModal');
        const modalTitle = document.getElementById('modalTitle');
        const modalContent = document.getElementById('modalContent');
        function openQuickView(filename, ext) {
            modalTitle.innerText = filename.split('/').pop();
            modalContent.innerHTML = '<div class="text-slate-400 flex flex-col items-center gap-2"><i class="fas fa-spinner fa-spin text-2xl"></i><p class="text-[10px] font-bold uppercase">Đang mở tài liệu...</p></div>';
            modal.classList.add('quick-view-active');
            document.body.style.overflow = 'hidden';
            setTimeout(() => {
                if (ext === 'pdf') {
                    modalContent.innerHTML = `<iframe src="${filename}#toolbar=0" class="w-full h-full border-none bg-white"></iframe>`;
                } else if (['jpg', 'png', 'jpeg', 'svg'].includes(ext)) {
                    modalContent.innerHTML = `<img src="${filename}" class="max-w-full max-h-full object-contain p-4 shadow-xl rounded-2xl">`;
                } else {
                    modalContent.innerHTML = '<p class="text-slate-400 text-xs font-bold uppercase">Định dạng không hỗ trợ xem nhanh</p>';
                }
            }, 500);
        }
        function closeQuickView() {
            modal.classList.remove('quick-view-active');
            modalContent.innerHTML = '';
            document.body.style.overflow = '';
        }
        modal.addEventListener('click', (e) => { if (e.target === modal) closeQuickView(); });
        window.addEventListener('keydown', (e) => { if (e.key === "Escape") closeQuickView(); });
    </script>
</body>
</html>