<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

require_once 'config.php';
global $pdo;

echo "<h1>视频访问测试</h1>";

$stmt = $pdo->prepare("SELECT video_id, video_name, storage_path, file_name, duration, is_active FROM video_assets WHERE video_type='opening' ORDER BY created_at DESC LIMIT 3;");
$stmt->execute();
$videos = $stmt->fetchAll(PDO::FETCH_ASSOC);

echo "<table border='1' cellpadding='10'>";
echo "<tr><th>ID</th><th>名称</th><th>存储路径</th><th>文件名</th><th>时长</th><th>状态</th><th>文件存在</th><th>测试播放</th></tr>";

foreach ($videos as $video) {
    $filePath = __DIR__ . '/' . $video['storage_path'] . $video['file_name'];
    $fileExists = file_exists($filePath);
    
    $isSecure = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on';
    $scheme = $isSecure ? 'https' : 'http';
    $host = $_SERVER['HTTP_HOST'];
    $scriptDir = dirname(dirname($_SERVER['PHP_SELF']));
    $scriptDir = str_replace('\\', '/', $scriptDir);
    if ($scriptDir === '/' || $scriptDir === '\\') {
        $scriptDir = '';
    }
    $baseUrl = $scheme . '://' . $host . $scriptDir;
    $playUrl = $baseUrl . '/' . $video['storage_path'] . $video['file_name'];
    $relativeUrl = $video['storage_path'] . $video['file_name'];
    
    echo "<tr>";
    echo "<td>" . $video['video_id'] . "</td>";
    echo "<td>" . $video['video_name'] . "</td>";
    echo "<td>" . $video['storage_path'] . "</td>";
    echo "<td>" . $video['file_name'] . "</td>";
    echo "<td>" . $video['duration'] . "秒</td>";
    echo "<td>" . ($video['is_active'] ? '活跃' : '禁用') . "</td>";
    echo "<td>" . ($fileExists ? '<span style="color:green">✓</span>' : '<span style="color:red">✗</span>') . "</td>";
    echo "<td>";
    echo "<a href='" . $playUrl . "' target='_blank'>完整URL</a><br>";
    echo "<a href='" . $relativeUrl . "' target='_blank'>相对路径</a><br>";
    echo "</td>";
    echo "</tr>";
    
    if ($fileExists) {
        echo "<tr><td colspan='8'>";
        echo "<video width='600' controls>";
        echo "<source src='" . $relativeUrl . "' type='video/mp4'>";
        echo "您的浏览器不支持视频播放";
        echo "</video>";
        echo "</td></tr>";
    }
}

echo "</table>";

echo "<h2>URL计算信息</h2>";
echo "<p>HTTP_HOST: " . $_SERVER['HTTP_HOST'] . "</p>";
echo "<p>PHP_SELF: " . $_SERVER['PHP_SELF'] . "</p>";
echo "<p>项目根目录: " . $scriptDir . "</p>";
echo "<p>Base URL: " . $baseUrl . "</p>";
?>