<?php
/**
 * 测试页面 - 检查配置
 */
?>
<!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>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 50px auto;
            padding: 20px;
            background: #f5f5f5;
        }
        .card {
            background: white;
            border-radius: 10px;
            padding: 30px;
            margin-bottom: 20px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        .status {
            display: inline-block;
            padding: 5px 15px;
            border-radius: 5px;
            margin-left: 10px;
            font-weight: bold;
        }
        .success {
            background: #4caf50;
            color: white;
        }
        .error {
            background: #f44336;
            color: white;
        }
        h1 {
            color: #333;
            border-bottom: 3px solid #0088cc;
            padding-bottom: 10px;
        }
        h2 {
            color: #0088cc;
        }
        pre {
            background: #f5f5f5;
            padding: 15px;
            border-radius: 5px;
            overflow-x: auto;
        }
        .test-item {
            padding: 10px;
            margin: 10px 0;
            border-left: 4px solid #0088cc;
            background: #f9f9f9;
        }
    </style>
</head>
<body>
    <div class="card">
        <h1>🎉 TG图片识别平台 - 系统测试</h1>
        
        <h2>📋 PHP配置</h2>
        <div class="test-item">
            <strong>PHP版本:</strong> <?php echo PHP_VERSION; ?>
            <?php if (version_compare(PHP_VERSION, '7.4.0', '>=')): ?>
                <span class="status success">✓ 正常</span>
            <?php else: ?>
                <span class="status error">✗ 版本过低</span>
            <?php endif; ?>
        </div>
        
        <div class="test-item">
            <strong>文档根目录:</strong> <?php echo $_SERVER['DOCUMENT_ROOT']; ?>
        </div>
        
        <div class="test-item">
            <strong>当前文件路径:</strong> <?php echo __FILE__; ?>
        </div>
        
        <h2>🔧 必需扩展检查</h2>
        <?php
        $required_extensions = ['pdo', 'pdo_mysql', 'curl', 'json', 'mbstring', 'gd', 'fileinfo'];
        foreach ($required_extensions as $ext) {
            echo '<div class="test-item">';
            echo "<strong>{$ext}:</strong> ";
            if (extension_loaded($ext)) {
                echo '<span class="status success">✓ 已安装</span>';
            } else {
                echo '<span class="status error">✗ 未安装</span>';
            }
            echo '</div>';
        }
        ?>
        
        <h2>🤖 AI服务检查</h2>
        <?php
        $ai_service_url = 'http://127.0.0.1:8000/stats';
        $ch = curl_init($ai_service_url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 5);
        $response = curl_exec($ch);
        $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        
        if ($http_code == 200 && $response) {
            $stats = json_decode($response, true);
            echo '<div class="test-item">';
            echo '<strong>AI服务状态:</strong> <span class="status success">✓ 运行正常</span>';
            echo '<pre>' . json_encode($stats, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) . '</pre>';
            echo '</div>';
        } else {
            echo '<div class="test-item">';
            echo '<strong>AI服务状态:</strong> <span class="status error">✗ 无法连接</span>';
            echo '<p>请确保AI服务正在运行: <code>ps aux | grep app_lite</code></p>';
            echo '</div>';
        }
        ?>
        
        <h2>💾 数据库检查</h2>
        <?php
        try {
            require_once __DIR__ . '/../vendor/autoload.php';
            
            $config = \App\Config\Config::getInstance();
            $db = \App\Database\Database::getInstance();
            
            echo '<div class="test-item">';
            echo '<strong>数据库连接:</strong> <span class="status success">✓ 成功</span>';
            echo '</div>';
            
            // 统计信息
            $stats = [
                'groups' => $db->count('tg_groups', 'is_active = 1'),
                'images' => $db->count('tg_images', 'is_downloaded = 1'),
                'processed' => $db->count('tg_images', 'is_processed = 1'),
                'features' => $db->count('image_features')
            ];
            
            echo '<div class="test-item">';
            echo '<strong>数据统计:</strong>';
            echo '<pre>' . json_encode($stats, JSON_PRETTY_PRINT) . '</pre>';
            echo '</div>';
            
        } catch (Exception $e) {
            echo '<div class="test-item">';
            echo '<strong>数据库连接:</strong> <span class="status error">✗ 失败</span>';
            echo '<p>错误: ' . htmlspecialchars($e->getMessage()) . '</p>';
            echo '</div>';
        }
        ?>
        
        <h2>📁 目录权限检查</h2>
        <?php
        $dirs = [
            'storage',
            'storage/images',
            'storage/uploads',
            'storage/logs',
            'storage/temp'
        ];
        
        foreach ($dirs as $dir) {
            $path = __DIR__ . '/../' . $dir;
            echo '<div class="test-item">';
            echo "<strong>{$dir}:</strong> ";
            if (is_dir($path)) {
                if (is_writable($path)) {
                    echo '<span class="status success">✓ 可写</span>';
                } else {
                    echo '<span class="status error">✗ 不可写</span>';
                }
            } else {
                echo '<span class="status error">✗ 不存在</span>';
            }
            echo '</div>';
        }
        ?>
        
        <h2>🚀 下一步操作</h2>
        <div class="test-item">
            <p><strong>1. 访问首页:</strong> <a href="index.php" style="color: #0088cc;">index.php</a></p>
            <p><strong>2. 查看使用指南:</strong> <a href="../立即开始使用.md" style="color: #0088cc;">立即开始使用.md</a></p>
            <p><strong>3. 如果一切正常，开始处理图片:</strong></p>
            <pre>cd /www/wwwroot/cweb.niunaiwu.com
php scripts/process_images_clip.php --limit=10 --verbose</pre>
        </div>
    </div>
</body>
</html>
