<?php

require __DIR__ . '/link_helper.php';

$config = load_app_config();
$page = $config['download_page'];

$isValidLink = false;
$message = '链接已过期';

try {
    $pdo = create_pdo($config);
    $result = resolve_request_link($pdo, $config, $_SERVER['HTTP_HOST'] ?? '');

    if ($result['ok']) {
        $isValidLink = true;

        if (!empty($result['record']['id'])) {
            touch_link_access($pdo, (int) $result['record']['id']);
        }
    } else {
        $message = $result['message'];
        http_response_code(410);
    }
} catch (Throwable $exception) {
    $message = '服务暂不可用，请稍后再试';
    http_response_code(500);
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1">
<title>Down_File</title>
<style>
*{margin:0;padding:0;box-sizing:border-box;font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto;}
body{background:#f7f8fa;color:#1f2329;}
.container{max-width:420px;margin:0 auto;padding:48px 20px;text-align:center;}
.logo{width:88px;height:88px;margin:0 auto 16px;border-radius:20px;}
h1{font-size:22px;margin-bottom:6px;}
.desc{font-size:14px;color:#666;margin-bottom:32px;}
.btn{display:block;width:100%;padding:14px 0;margin-bottom:14px;border-radius:12px;font-size:16px;text-decoration:none;}
.btn-ios{background:#000;color:#fff;}
.btn-android{background:#1677ff;color:#fff;}
.tip{font-size:12px;color:#999;margin-top:20px;line-height:1.6;}
.footer{margin-top:40px;font-size:12px;color:#aaa;}
.mask{position:fixed;top:0;left:0;right:0;bottom:0;background:rgba(0,0,0,.7);display:none;align-items:center;justify-content:center;z-index:999;}
.tip-box{width:80%;max-width:300px;background:#fff;border-radius:16px;padding:24px 18px;text-align:center;box-shadow:0 10px 30px rgba(0,0,0,.2);}
.tip-box .title{font-size:18px;font-weight:600;margin-bottom:10px;}
.tip-box .text{font-size:13px;color:#666;line-height:1.6;}
.tip-box .icon{font-size:32px;margin-top:12px;}
.expired-box{margin-top:72px;padding:28px 22px;background:#fff;border-radius:18px;box-shadow:0 12px 40px rgba(15,23,42,.08);}
.expired-title{font-size:22px;font-weight:700;margin-bottom:12px;}
.expired-text{font-size:14px;line-height:1.8;color:#666;}
</style>
</head>
<body>
<?php if ($isValidLink): ?>
<div class="container">
    <img src="<?= htmlspecialchars($page['logo'], ENT_QUOTES, 'UTF-8') ?>" class="logo" alt="<?= htmlspecialchars($page['title'], ENT_QUOTES, 'UTF-8') ?>">

    <h1><?= htmlspecialchars($page['title'], ENT_QUOTES, 'UTF-8') ?></h1>
    <div class="desc"><?= htmlspecialchars($page['description'], ENT_QUOTES, 'UTF-8') ?></div>

    <a href="./download-file.php?platform=ios" class="btn btn-ios">Iphone 下载</a>
    <a href="./download-file.php?platform=android" class="btn btn-android">Android 下载</a>

    <div class="tip">
        如无法下载，请使用系统浏览器打开本页面
    </div>

    <div class="footer">
        <?= htmlspecialchars($page['footer'], ENT_QUOTES, 'UTF-8') ?>
    </div>
</div>

<div class="mask" id="browserMask">
    <div class="tip-box">
        <div class="title">请在浏览器中打开</div>
        <div class="text">
            微信 / QQ 内无法下载应用<br>
            请点击右上角 ··· 选择“在浏览器打开”
        </div>
        <div class="icon">🌐</div>
    </div>
</div>

<script>
(function(){
    var ua = navigator.userAgent.toLowerCase();
    if(ua.indexOf('micromessenger')>-1 || ua.indexOf('qq/')>-1){
        document.getElementById('browserMask').style.display='flex';
    }
})();
</script>
<?php else: ?>
<div class="container">
    <div class="expired-box">
        <div class="expired-title">链接已过期</div>
        <div class="expired-text"><?= htmlspecialchars($message, ENT_QUOTES, 'UTF-8') ?><br>请返回上级页面重新生成下载链接。</div>
    </div>
</div>
<?php endif; ?>
</body>
</html>
