<?php
$config = include('config.php');
$cookie_name = "fast_route_passed";

$enable_verification = isset($config['enable_verification']) ? (bool)$config['enable_verification'] : true;

// 收集所有备用线路（包括大奖线路和其它线路列表，用于按索引精准跳转）
$all_routes = [];
if (isset($config['backup_route']) && !empty($config['backup_route'])) {
    $all_routes[] = $config['backup_route'];
}   
if (isset($config['list_routes']) && is_array($config['list_routes'])) {
    $all_routes = array_merge($all_routes, $config['list_routes']);
}

// 兼容老配置：如果没配 list_routes，则退回到原本的 target_directory 随机逻辑
$raw_target = isset($config['target_directory']) ? $config['target_directory'] : '';
$fallback_urls = array_filter(array_map('trim', preg_split("/\r\n|\n|\r/", $raw_target)));

// 智能捕获并保存请求中带有的参数（兼容 /cid=4502630 或 ?cid=4502630）
$cid_val = '';
if (isset($_GET['cid']) && $_GET['cid'] !== '') {
    $cid_val = trim($_GET['cid']);
} else {
    $request_uri = $_SERVER['REQUEST_URI'];
    if (preg_match('/cid[=\/]([a-zA-Z0-9_-]+)/', $request_uri, $matches)) {
        $cid_val = $matches[1];
    }
}

// 当用户点击具体线路触发跳转时
if (isset($_GET['action']) && $_GET['action'] === 'pass') {
    setcookie($cookie_name, "1", time() + 3600, "/");
    
    // 获取前端传过来的目标线路索引
    $target_index = isset($_GET['route_idx']) ? (int)$_GET['route_idx'] : 0;
    
    $target_url = '';
    if (isset($all_routes[$target_index]['url'])) {
        $target_url = $all_routes[$target_index]['url'];
    } elseif (!empty($fallback_urls)) {
        // 兼容降级
        $target_url = $fallback_urls[array_rand($fallback_urls)];
    }

    $final_url = rtrim($target_url, '/');
    
    // 如果存在 cid，拼接到最后面（带问号形式：?cid=4502630）
    if ($cid_val !== '') {
        $separator = (strpos($final_url, '?') !== false) ? '&' : '?';
        $final_url .= $separator . 'cid=' . urlencode($cid_val);
    }
    
    header("Location: " . $final_url);
    exit;
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>开元棋牌</title>
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; font-family: "PingFang SC", "Microsoft YaHei", sans-serif; }
        body { background-color: #f0f2f5; display: flex; flex-direction: column; align-items: center; justify-content: center; min-height: 100vh; padding: 15px 10px; }
        body.dark-theme { background-color: #1a0202; background-image: repeating-conic-gradient(from 0deg at 50% 50%, #2b0404 0deg, #150101 30deg, #2b0404 60deg); justify-content: flex-start; }
        .step-container { display: none; width: 100%; max-width: 400px; flex-direction: column; align-items: center; }
        .step-container.active { display: flex; }

        /* 第一步验证样式（已将转圈改为固定安全盾牌图标） */
        #step-1 { background: #ffffff; padding: 40px 30px; border-radius: 16px; box-shadow: 0 10px 30px rgba(0, 0, 0, 0.08); text-align: center; }
        .security-badge { width: 64px; height: 64px; background: linear-gradient(135deg, #e3f9ed 0%, #d1f7e2 100%); border: 2px solid #00b050; border-radius: 50%; margin: 0 auto 20px auto; display: flex; align-items: center; justify-content: center; color: #00b050; box-shadow: 0 4px 12px rgba(0, 176, 80, 0.15); }
        .security-badge svg { width: 32px; height: 32px; fill: currentColor; }
        
        .v-title { font-size: 22px; font-weight: bold; color: #111; margin-bottom: 10px; }
        .v-sub { font-size: 15px; color: #666; margin-bottom: 30px; }
        .v-btn { background-color: #00b050; color: #ffffff; font-size: 16px; font-weight: bold; padding: 14px 0; width: 100%; border: none; border-radius: 8px; cursor: pointer; transition: all 0.3s; box-shadow: 0 4px 12px rgba(0, 176, 80, 0.3); }
        .v-btn:hover { opacity: 0.9; }

        /* 顶部卡片 */
        .header-card { background: linear-gradient(135deg, #7a1212 0%, #4a0707 100%); border: 2px solid #D5AB55; box-shadow: 0 0 18px rgba(213, 171, 85, 0.4); border-radius: 14px; padding: 12px 25px; text-align: center; margin-bottom: 15px; width: 100%; color: #D5AB55; flex-shrink: 0; }

        /* 第二步：测速页 */
        #step-2 { text-align: center; }
        .sunburst-ring { width: 150px; height: 150px; margin: 12px auto; position: relative; display: flex; align-items: center; justify-content: center; }
        .sunburst-ring::before {
            content: ''; position: absolute; inset: 0; border-radius: 50%;
            background: conic-gradient(#D5AB55 0deg 20deg, transparent 20deg 40deg, #D5AB55 40deg 60deg, transparent 60deg 80deg, #D5AB55 80deg 100deg, transparent 100deg 120deg, #D5AB55 120deg 140deg, transparent 140deg 160deg, #D5AB55 160deg 180deg, transparent 180deg 200deg, #D5AB55 200deg 220deg, transparent 220deg 240deg, #D5AB55 240deg 260deg, transparent 260deg 280deg, #D5AB55 280deg 300deg, transparent 300deg 320deg, #D5AB55 320deg 340deg, transparent 340deg 360deg);
            mask: radial-gradient(circle, transparent 55%, black 56%);
            -webkit-mask: radial-gradient(circle, transparent 55%, black 56%);
            opacity: 0.85; animation: spin 10s linear infinite;
        }
        .coin-center { width: 64px; height: 64px; background: linear-gradient(135deg, #D5AB55 0%, #D5AB55 50%, #D5AB55 100%); border-radius: 50%; display: flex; align-items: center; justify-content: center; font-size: 32px; color: #4a0707; font-weight: bold; box-shadow: 0 0 25px rgba(213, 171, 85, 0.8); border: 2.5px solid #fff; position: relative; z-index: 2; }
        .t-sub { font-size: 14px; color: #D5AB55; margin-top: 6px; opacity: 0.9; }
        .t-main { font-size: 15px; color: #D5AB55; opacity: 0.85; margin-top: 12px; }