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

$auth = new Auth();
$auth->requireLogin();
?>
<!DOCTYPE html>
<html lang="zh-TW">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>AI功能測試</title>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 1200px;
            margin: 50px auto;
            padding: 20px;
        }
        .test-section {
            background: #f8f9fa;
            border: 1px solid #dee2e6;
            border-radius: 8px;
            padding: 20px;
            margin-bottom: 20px;
        }
        .btn {
            background: #007bff;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 5px;
            cursor: pointer;
            margin: 5px;
        }
        .btn:hover {
            background: #0056b3;
        }
        .result {
            background: white;
            border: 1px solid #ccc;
            border-radius: 5px;
            padding: 15px;
            margin-top: 10px;
            white-space: pre-wrap;
        }
        .error {
            background: #f8d7da;
            color: #721c24;
            border-color: #f5c6cb;
        }
        .success {
            background: #d4edda;
            color: #155724;
            border-color: #c3e6cb;
        }
        .modal {
            display: none;
            position: fixed;
            z-index: 2000;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.5);
        }
        .modal-content {
            background-color: white;
            margin: 5% auto;
            padding: 0;
            border-radius: 15px;
            width: 90%;
            max-width: 800px;
            max-height: 90vh;
            overflow-y: auto;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
        }
        .modal-header {
            background: linear-gradient(135deg, #667eea, #764ba2);
            color: white;
            padding: 20px 30px;
            border-radius: 15px 15px 0 0;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        .modal-header h2 {
            margin: 0;
            font-size: 1.5rem;
        }
        .modal-header .close {
            color: white;
            font-size: 28px;
            font-weight: bold;
            cursor: pointer;
            transition: opacity 0.3s ease;
        }
        .modal-header .close:hover {
            opacity: 0.7;
        }
        .modal-body {
            padding: 30px;
        }
    </style>
</head>
<body>
    <h1>AI功能測試頁面</h1>
    
    <div class="test-section">
        <h3>測試1: 標題生成器模態框</h3>
        <button class="btn" onclick="testTitleModal()">測試標題生成器</button>
        <div id="titleModalResult" class="result" style="display: none;"></div>
    </div>
    
    <div class="test-section">
        <h3>測試2: 內容生成器模態框</h3>
        <button class="btn" onclick="testContentModal()">測試內容生成器</button>
        <div id="contentModalResult" class="result" style="display: none;"></div>
    </div>
    
    <div class="test-section">
        <h3>測試3: JavaScript函數檢查</h3>
        <button class="btn" onclick="testJavaScriptFunctions()">檢查JavaScript函數</button>
        <div id="jsResult" class="result" style="display: none;"></div>
    </div>
    
    <div class="test-section">
        <h3>測試4: API端點測試</h3>
        <button class="btn" onclick="testAPIEndpoints()">測試API端點</button>
        <div id="apiResult" class="result" style="display: none;"></div>
    </div>
    
    <!-- 測試用模態框 -->
    <div id="testModal" class="modal">
        <div class="modal-content">
            <div class="modal-header">
                <h2><i class="fas fa-test"></i> 測試模態框</h2>
                <span class="close" onclick="closeTestModal()">&times;</span>
            </div>
            <div class="modal-body">
                <p>這是一個測試模態框，用於驗證模態框功能是否正常。</p>
                <button class="btn" onclick="closeTestModal()">關閉</button>
            </div>
        </div>
    </div>
    
    <script>
        function testTitleModal() {
            const resultDiv = document.getElementById('titleModalResult');
            resultDiv.style.display = 'block';
            resultDiv.className = 'result';
            
            // 檢查標題生成器相關函數
            const functions = [
                'showTitleGeneratorModal',
                'closeTitleGeneratorModal',
                'generateAITitles',
                'displayGeneratedTitles',
                'useTitle',
                'analyzeTitle',
                'displayTitleAnalysis'
            ];
            
            let results = [];
            functions.forEach(func => {
                if (typeof window[func] === 'function') {
                    results.push(`✅ ${func} 函數存在`);
                } else {
                    results.push(`❌ ${func} 函數不存在`);
                }
            });
            
            // 檢查模態框元素
            const modal = document.getElementById('titleGeneratorModal');
            if (modal) {
                results.push('✅ 標題生成器模態框元素存在');
            } else {
                results.push('❌ 標題生成器模態框元素不存在');
            }
            
            resultDiv.textContent = '標題生成器功能檢查：\n' + results.join('\n');
        }
        
        function testContentModal() {
            const resultDiv = document.getElementById('contentModalResult');
            resultDiv.style.display = 'block';
            resultDiv.className = 'result';
            
            // 檢查內容生成器相關函數
            const functions = [
                'showContentGeneratorModal',
                'closeContentGeneratorModal',
                'switchTab',
                'generateFullContent',
                'generateOutline',
                'expandSection',
                'generateIntro',
                'generateConclusion',
                'displayGeneratedContent',
                'displayGeneratedOutline',
                'useGeneratedContent',
                'appendGeneratedContent',
                'useGeneratedOutline'
            ];
            
            let results = [];
            functions.forEach(func => {
                if (typeof window[func] === 'function') {
                    results.push(`✅ ${func} 函數存在`);
                } else {
                    results.push(`❌ ${func} 函數不存在`);
                }
            });
            
            // 檢查模態框元素
            const modal = document.getElementById('contentGeneratorModal');
            if (modal) {
                results.push('✅ 內容生成器模態框元素存在');
            } else {
                results.push('❌ 內容生成器模態框元素不存在');
            }
            
            resultDiv.textContent = '內容生成器功能檢查：\n' + results.join('\n');
        }
        
        function testJavaScriptFunctions() {
            const resultDiv = document.getElementById('jsResult');
            resultDiv.style.display = 'block';
            resultDiv.className = 'result';
            
            // 檢查所有AI相關函數
            const allFunctions = [
                // 標題生成器
                'showTitleGeneratorModal', 'closeTitleGeneratorModal', 'generateAITitles',
                'displayGeneratedTitles', 'useTitle', 'analyzeTitle', 'displayTitleAnalysis',
                'closeTitleAnalysisModal',
                // 內容生成器
                'showContentGeneratorModal', 'closeContentGeneratorModal', 'switchTab',
                'generateFullContent', 'generateOutline', 'expandSection', 'generateIntro',
                'generateConclusion', 'displayGeneratedContent', 'displayGeneratedOutline',
                'useGeneratedContent', 'appendGeneratedContent', 'useGeneratedOutline',
                'optimizeGeneratedContent',
                // 通用函數
                'showNotification', 'updateStats'
            ];
            
            let results = [];
            let existingCount = 0;
            
            allFunctions.forEach(func => {
                if (typeof window[func] === 'function') {
                    results.push(`✅ ${func}`);
                    existingCount++;
                } else {
                    results.push(`❌ ${func}`);
                }
            });
            
            results.unshift(`JavaScript函數檢查結果：\n存在: ${existingCount}/${allFunctions.length}\n\n`);
            
            resultDiv.textContent = results.join('\n');
        }
        
        async function testAPIEndpoints() {
            const resultDiv = document.getElementById('apiResult');
            resultDiv.style.display = 'block';
            resultDiv.className = 'result';
            resultDiv.textContent = '測試中...';
            
            const endpoints = [
                { name: '標題生成API', url: '/api/ai_title_generator.php', action: 'generate_titles' },
                { name: '內容生成API', url: '/api/ai_content_generator.php', action: 'generate_content' }
            ];
            
            let results = [];
            
            for (const endpoint of endpoints) {
                try {
                    const response = await fetch(endpoint.url, {
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/json',
                        },
                        credentials: 'same-origin',
                        body: JSON.stringify({
                            action: endpoint.action,
                            plan_id: '1',
                            title: '測試標題'
                        })
                    });
                    
                    if (response.ok) {
                        results.push(`✅ ${endpoint.name}: 連接成功`);
                    } else {
                        results.push(`❌ ${endpoint.name}: HTTP ${response.status}`);
                    }
                } catch (error) {
                    results.push(`❌ ${endpoint.name}: ${error.message}`);
                }
            }
            
            resultDiv.textContent = 'API端點測試結果：\n' + results.join('\n');
        }
        
        function closeTestModal() {
            document.getElementById('testModal').style.display = 'none';
        }
        
        // 測試模態框功能
        function showTestModal() {
            document.getElementById('testModal').style.display = 'block';
        }
        
        // 檢查模態框點擊外部關閉功能
        window.onclick = function(event) {
            const testModal = document.getElementById('testModal');
            if (event.target === testModal) {
                closeTestModal();
            }
        }
    </script>
</body>
</html>
