<?php
require_once '../includes/auth.php';

$auth = new Auth();
$auth->requireLogin();

// Get brands data
require_once '../config/database.php';
$database = new Database();
$db = $database->getConnection();

$brands = [];
$query = "SELECT * FROM brands ORDER BY created_at DESC";
$stmt = $db->prepare($query);
$stmt->execute();
$brands = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<!DOCTYPE html>
<html lang="zh-TW">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>SEO分析測試</title>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            margin: 0;
            padding: 20px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
        }
        
        .container {
            max-width: 1200px;
            margin: 0 auto;
            background: white;
            border-radius: 15px;
            padding: 30px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
        }
        
        .header {
            text-align: center;
            margin-bottom: 30px;
        }
        
        .header h1 {
            color: #333;
            margin: 0;
            font-size: 2.5rem;
        }
        
        .test-section {
            background: #f8f9fa;
            border: 2px solid #e9ecef;
            border-radius: 10px;
            padding: 20px;
            margin-bottom: 20px;
        }
        
        .test-section h3 {
            color: #333;
            margin: 0 0 15px 0;
            display: flex;
            align-items: center;
            gap: 10px;
        }
        
        .btn {
            padding: 12px 24px;
            border: none;
            border-radius: 8px;
            font-size: 1rem;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            display: inline-flex;
            align-items: center;
            gap: 8px;
            text-decoration: none;
            margin: 5px;
        }
        
        .btn-primary {
            background: linear-gradient(135deg, #667eea, #764ba2);
            color: white;
        }
        
        .btn-primary:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
        }
        
        .btn-success {
            background: #28a745;
            color: white;
        }
        
        .btn-success:hover {
            background: #218838;
        }
        
        .btn-danger {
            background: #dc3545;
            color: white;
        }
        
        .btn-danger:hover {
            background: #c82333;
        }
        
        .form-group {
            margin-bottom: 20px;
        }
        
        .form-group label {
            display: block;
            margin-bottom: 8px;
            color: #333;
            font-weight: 500;
        }
        
        .form-control {
            width: 100%;
            padding: 12px;
            border: 2px solid #e0e0e0;
            border-radius: 8px;
            font-size: 1rem;
            transition: border-color 0.3s ease;
        }
        
        .form-control:focus {
            outline: none;
            border-color: #667eea;
        }
        
        .result {
            background: white;
            border: 1px solid #e0e0e0;
            border-radius: 8px;
            padding: 15px;
            margin-top: 15px;
            font-family: monospace;
            white-space: pre-wrap;
        }
        
        .brand-list {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
            gap: 15px;
            margin-top: 15px;
        }
        
        .brand-item {
            background: white;
            border: 1px solid #e0e0e0;
            border-radius: 8px;
            padding: 15px;
        }
        
        .brand-item h4 {
            margin: 0 0 10px 0;
            color: #333;
        }
        
        .brand-item p {
            margin: 5px 0;
            color: #666;
            font-size: 0.9rem;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1><i class="fas fa-search"></i> SEO分析測試</h1>
            <p>診斷SEO分析功能的問題</p>
        </div>
        
        <div class="test-section">
            <h3><i class="fas fa-database"></i> 資料庫連接測試</h3>
            <p>資料庫連接狀態：<strong style="color: green;">✅ 正常</strong></p>
            <p>品牌數量：<strong><?php echo count($brands); ?></strong></p>
        </div>
        
        <div class="test-section">
            <h3><i class="fas fa-building"></i> 品牌資料</h3>
            <?php if (count($brands) > 0): ?>
                <div class="brand-list">
                    <?php foreach ($brands as $brand): ?>
                        <div class="brand-item">
                            <h4><?php echo htmlspecialchars($brand['name']); ?></h4>
                            <p><strong>ID:</strong> <?php echo $brand['id']; ?></p>
                            <p><strong>網站:</strong> <?php echo htmlspecialchars($brand['website']); ?></p>
                            <p><strong>行業:</strong> <?php echo htmlspecialchars($brand['industry']); ?></p>
                        </div>
                    <?php endforeach; ?>
                </div>
            <?php else: ?>
                <p style="color: red;">❌ 沒有找到品牌資料</p>
            <?php endif; ?>
        </div>
        
        <div class="test-section">
            <h3><i class="fas fa-cog"></i> API測試</h3>
            <div class="form-group">
                <label for="testBrandId">選擇品牌</label>
                <select id="testBrandId" class="form-control">
                    <option value="">請選擇品牌</option>
                    <?php foreach ($brands as $brand): ?>
                        <option value="<?php echo $brand['id']; ?>"><?php echo htmlspecialchars($brand['name']); ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            
            <div class="form-group">
                <label for="testAnalysisType">分析類型</label>
                <select id="testAnalysisType" class="form-control">
                    <option value="basic">基本分析</option>
                    <option value="keyword">關鍵字分析</option>
                    <option value="competitor">競爭對手分析</option>
                    <option value="technical">技術SEO分析</option>
                </select>
            </div>
            
            <button class="btn btn-primary" onclick="testSEOAnalysis()">
                <i class="fas fa-play"></i>
                測試SEO分析
            </button>
            
            <button class="btn btn-success" onclick="testAPI()">
                <i class="fas fa-code"></i>
                測試API連接
            </button>
            
            <div id="testResult" class="result" style="display: none;"></div>
        </div>
        
        <div class="test-section">
            <h3><i class="fas fa-link"></i> 測試連結</h3>
            <a href="seo_analysis.php" class="btn btn-primary">
                <i class="fas fa-external-link-alt"></i>
                測試SEO分析頁面
            </a>
            <a href="brands.php" class="btn btn-primary">
                <i class="fas fa-building"></i>
                品牌管理
            </a>
            <a href="dashboard.php" class="btn btn-primary">
                <i class="fas fa-tachometer-alt"></i>
                返回儀表板
            </a>
        </div>
    </div>
    
    <script>
        async function testSEOAnalysis() {
            const brandId = document.getElementById('testBrandId').value;
            const analysisType = document.getElementById('testAnalysisType').value;
            
            if (!brandId) {
                alert('請選擇品牌');
                return;
            }
            
            const resultDiv = document.getElementById('testResult');
            resultDiv.style.display = 'block';
            resultDiv.textContent = '測試中...';
            
            try {
                const response = await fetch('/api/seo_analysis.php', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    credentials: 'same-origin',
                    body: JSON.stringify({
                        brand_id: brandId,
                        analysis_type: analysisType
                    })
                });
                
                const result = await response.json();
                resultDiv.textContent = JSON.stringify(result, null, 2);
                
                if (result.success) {
                    resultDiv.style.borderColor = '#28a745';
                    resultDiv.style.backgroundColor = '#d4edda';
                } else {
                    resultDiv.style.borderColor = '#dc3545';
                    resultDiv.style.backgroundColor = '#f8d7da';
                }
            } catch (error) {
                resultDiv.textContent = '錯誤: ' + error.message;
                resultDiv.style.borderColor = '#dc3545';
                resultDiv.style.backgroundColor = '#f8d7da';
            }
        }
        
        async function testAPI() {
            const resultDiv = document.getElementById('testResult');
            resultDiv.style.display = 'block';
            resultDiv.textContent = '測試API連接中...';
            
            try {
                const response = await fetch('/api/seo_analysis.php', {
                    method: 'GET',
                    credentials: 'same-origin'
                });
                
                const result = await response.json();
                resultDiv.textContent = JSON.stringify(result, null, 2);
                
                if (response.ok) {
                    resultDiv.style.borderColor = '#28a745';
                    resultDiv.style.backgroundColor = '#d4edda';
                } else {
                    resultDiv.style.borderColor = '#dc3545';
                    resultDiv.style.backgroundColor = '#f8d7da';
                }
            } catch (error) {
                resultDiv.textContent = '錯誤: ' + error.message;
                resultDiv.style.borderColor = '#dc3545';
                resultDiv.style.backgroundColor = '#f8d7da';
            }
        }
    </script>
</body>
</html>
