<?php
$nosession = true;
$nosecu = true;
include("../includes/common.php");
if ($islogin == 1) {
} else exit();

$urlarr = explode('/', $_SERVER['PATH_INFO']);
if (($length = count($urlarr)) > 1) {
    $url = $urlarr[$length - 1];
}
$extension = explode('&', $url);
if (($length = count($extension)) > 1) {
    $pwd = $extension[$length - 1];
    $url = $extension[0];
}

if (strpos($url, ".")) {
    $hash = substr($url, 0, strpos($url, "."));
    $type = substr($url, strpos($url, ".") + 1);
} else {
    $hash = $url;
}

$row = $DB->getRow("SELECT * FROM `pre_file` WHERE `hash`=:hash limit 1", [':hash' => $hash]);
if (!$row || !$stor->exists($row['hash'])) {
    exit('<!DOCTYPE html><html><head><meta charset="utf-8"><title>文件不存在</title></head><body style="display:flex;justify-content:center;align-items:center;height:100vh;margin:0;font-family:sans-serif;"><div style="text-align:center;"><h1 style="color:#ef4444;">文件不存在</h1><p>该文件可能已被删除或不存在</p></div></body></html>');
}

$isViewable = is_view($row['type']);
$viewType = get_view_type($row['type']);

$uploaderInfo = null;
if ($row['uid'] > 0) {
    $uploaderInfo = $DB->getRow("SELECT username, email FROM `pre_user` WHERE `uid`=:uid limit 1", [':uid' => $row['uid']]);
}

if (empty($row['share_key'])) {
    $shareKey = substr(md5(uniqid((string)$row['uid'], true) . mt_rand()), 0, 16);
    $DB->exec("UPDATE pre_file SET share_key=:k WHERE id=:fid", [':k' => $shareKey, ':fid' => $row['id']]);
    $row['share_key'] = $shareKey;
}

$title = '文件详情 - ' . htmlspecialchars($row['name']);
?>
<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="utf-8"/>
    <meta name="renderer" content="webkit">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo $title; ?></title>
    <link href="https://s4.zstatic.net/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
    <link href="/assets/css/admin-file-view.css?v=1" rel="stylesheet"/>
</head>
<body>
    <div class="file-view-container">
        <div class="file-view-header">
            <div class="header-left">
                <i class="fa <?php echo type_to_icon($row['type']); ?> file-icon"></i>
                <div class="file-title">
                    <h1><?php echo htmlspecialchars($row['name']); ?></h1>
                    <span class="file-hash">Hash: <?php echo $row['hash']; ?></span>
                </div>
            </div>
            <div class="header-right">
                <?php if ($isViewable): ?>
                <button class="btn btn-primary" id="previewBtn" onclick="togglePreview()">
                    <i class="fa fa-eye"></i> 在线预览
                </button>
                <?php endif; ?>
                <a href="down.php?hash=<?php echo $row['hash']; ?>" class="btn btn-success">
                    <i class="fa fa-download"></i> 下载文件
                </a>
                <button class="btn btn-secondary" onclick="window.close()">
                    <i class="fa fa-times"></i> 关闭
                </button>
            </div>
        </div>

        <div class="file-view-content">
            <div class="file-info-panel">
                <div class="info-section">
                    <h3><i class="fa fa-info-circle"></i> 基本信息</h3>
                    <div class="info-grid">
                        <div class="info-item">
                            <span class="info-label">文件名称</span>
                            <span class="info-value" title="<?php echo htmlspecialchars($row['name']); ?>"><?php echo htmlspecialchars($row['name']); ?></span>
                        </div>
                        <div class="info-item">
                            <span class="info-label">文件格式</span>
                            <span class="info-value">
                                <span class="file-type-badge"><?php echo strtoupper($row['type'] ?: '未知'); ?></span>
                            </span>
                        </div>
                        <div class="info-item">
                            <span class="info-label">文件大小</span>
                            <span class="info-value"><?php echo size_format($row['size']); ?></span>
                        </div>
                        <div class="info-item">
                            <span class="info-label">文件状态</span>
                            <span class="info-value">
                                <?php if ($row['block'] == 0): ?>
                                    <span class="status-badge normal"><i class="fa fa-check-circle"></i> 正常</span>
                                <?php else: ?>
                                    <span class="status-badge banned"><i class="fa fa-ban"></i> 已屏蔽</span>
                                <?php endif; ?>
                            </span>
                        </div>
                    </div>
                </div>

                <div class="info-section">
                    <h3><i class="fa fa-clock-o"></i> 时间信息</h3>
                    <div class="info-grid">
                        <div class="info-item">
                            <span class="info-label">上传时间</span>
                            <span class="info-value"><?php echo $row['addtime']; ?></span>
                        </div>
                        <div class="info-item">
                            <span class="info-label">最后访问</span>
                            <span class="info-value"><?php echo $row['lasttime'] ?: '暂无记录'; ?></span>
                        </div>
                        <div class="info-item">
                            <span class="info-label">下载次数</span>
                            <span class="info-value"><i class="fa fa-download"></i> <?php echo $row['count']; ?> 次</span>
                        </div>
                    </div>
                </div>

                <div class="info-section">
                    <h3><i class="fa fa-user"></i> 上传者信息</h3>
                    <div class="info-grid">
                        <div class="info-item">
                            <span class="info-label">上传者</span>
                            <span class="info-value">
                                <?php if ($row['uid'] > 0 && $uploaderInfo): ?>
                                    <i class="fa fa-user-circle"></i> <?php echo htmlspecialchars($uploaderInfo['username']); ?>
                                    <span class="uid-badge">UID: <?php echo $row['uid']; ?></span>
                                <?php else: ?>
                                    <i class="fa fa-user-o"></i> 游客上传
                                <?php endif; ?>
                            </span>
                        </div>
                        <div class="info-item">
                            <span class="info-label">上传IP</span>
                            <span class="info-value">
                                <i class="fa fa-globe"></i> <?php echo $row['ip']; ?>
                            </span>
                        </div>
                        <?php if ($row['uid'] > 0 && $uploaderInfo && $uploaderInfo['email']): ?>
                        <div class="info-item">
                            <span class="info-label">联系邮箱</span>
                            <span class="info-value"><?php echo htmlspecialchars($uploaderInfo['email']); ?></span>
                        </div>
                        <?php endif; ?>
                    </div>
                </div>

                <?php if ($row['pwd']): ?>
                <div class="info-section">
                    <h3><i class="fa fa-lock"></i> 访问控制</h3>
                    <div class="info-grid">
                        <div class="info-item">
                            <span class="info-label">下载密码</span>
                            <span class="info-value">
                                <span class="pwd-badge"><i class="fa fa-key"></i> <?php echo htmlspecialchars($row['pwd']); ?></span>
                            </span>
                        </div>
                    </div>
                </div>
                <?php endif; ?>

                <div class="info-section">
                    <h3><i class="fa fa-link"></i> 文件链接</h3>
                    <div class="link-list">
                        <div class="link-item">
                            <span class="link-label">文件直链</span>
                            <div class="link-input-group">
                                <input type="text" value="<?php echo (is_https() ? 'https://' : 'http://').$_SERVER['HTTP_HOST']; ?>/down.php?hash=<?php echo $row['hash']; ?>" readonly id="directLink">
                                <button class="btn-copy" onclick="copyLink('directLink')">
                                    <i class="fa fa-copy"></i>
                                </button>
                            </div>
                        </div>
                        <div class="link-item">
                            <span class="link-label">分享链接</span>
                            <div class="link-input-group">
                                <input type="text" value="<?php echo (is_https() ? 'https://' : 'http://').$_SERVER['HTTP_HOST']; ?>/share.php?key=<?php echo $row['share_key']; ?>" readonly id="pageLink">
                                <button class="btn-copy" onclick="copyLink('pageLink')">
                                    <i class="fa fa-copy"></i>
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <?php if ($isViewable): ?>
            <div class="file-preview-panel" id="previewPanel" style="display: none;">
                <div class="preview-header">
                    <h3><i class="fa fa-eye"></i> 在线预览</h3>
                    <button class="btn-close-preview" onclick="togglePreview()">
                        <i class="fa fa-times"></i>
                    </button>
                </div>
                <div class="preview-content">
                    <?php if ($viewType == 'image'): ?>
                        <img src="down.php?hash=<?php echo $row['hash']; ?>" alt="<?php echo htmlspecialchars($row['name']); ?>" class="preview-image">
                    <?php elseif ($viewType == 'video'): ?>
                        <video controls class="preview-video">
                            <source src="down.php?hash=<?php echo $row['hash']; ?>" type="<?php echo minetype($row['type']); ?>">
                            您的浏览器不支持视频播放
                        </video>
                    <?php elseif ($viewType == 'audio'): ?>
                        <audio controls class="preview-audio">
                            <source src="down.php?hash=<?php echo $row['hash']; ?>" type="<?php echo minetype($row['type']); ?>">
                            您的浏览器不支持音频播放
                        </audio>
                    <?php endif; ?>
                </div>
            </div>
            <?php endif; ?>
        </div>
    </div>

    <div class="toast" id="toast">
        <i class="fa fa-check-circle"></i>
        <span id="toastMsg">复制成功</span>
    </div>

    <script src="/assets/js/admin-file-view.js?v=1"></script>
</body>
</html>
