<?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);

// Get content plans
$plans_query = "SELECT cp.*, b.name as brand_name FROM content_plans cp 
               LEFT JOIN brands b ON cp.brand_id = b.id 
               ORDER BY cp.created_at DESC LIMIT 10";
$plans_stmt = $db->prepare($plans_query);
$plans_stmt->execute();
$recent_plans = $plans_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 AI 自動化系統</title>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <link href="../assets/css/admin.css" rel="stylesheet">
</head>
<body>
    <div class="admin-layout">
        <!-- Sidebar -->
        <aside class="sidebar">
            <div class="sidebar-header">
                <div class="logo">
                    <i class="fas fa-robot"></i>
                    <span>SEO AI</span>
                </div>
            </div>
            
            <nav class="sidebar-nav">
                <ul>
                    <li>
                        <a href="dashboard.php">
                            <i class="fas fa-tachometer-alt"></i>
                            <span>儀表板</span>
                        </a>
                    </li>
                    <li>
                        <a href="brands.php">
                            <i class="fas fa-building"></i>
                            <span>品牌管理</span>
                        </a>
                    </li>
                    <li>
                        <a href="reports.php">
                            <i class="fas fa-chart-bar"></i>
                            <span>SEO報告</span>
                        </a>
                    </li>
                    <li>
                        <a href="seo_analysis.php">
                            <i class="fas fa-search"></i>
                            <span>SEO分析</span>
                        </a>
                    </li>
                    <li class="active">
                        <a href="content.php">
                            <i class="fas fa-edit"></i>
                            <span>內容管理</span>
                        </a>
                    </li>
                    <li>
                        <a href="analytics.php">
                            <i class="fas fa-analytics"></i>
                            <span>成效分析</span>
                        </a>
                    </li>
                    <li>
                        <a href="settings.php">
                            <i class="fas fa-cog"></i>
                            <span>系統設定</span>
                        </a>
                    </li>
                    <li>
                        <a href="help.php">
                            <i class="fas fa-question-circle"></i>
                            <span>使用說明</span>
                        </a>
                    </li>
                    <li>
                        <a href="profile.php">
                            <i class="fas fa-user"></i>
                            <span>個人資料</span>
                        </a>
                    </li>
                </ul>
            </nav>
            
            <div class="sidebar-footer">
                <a href="logout.php" class="logout-btn">
                    <i class="fas fa-sign-out-alt"></i>
                    <span>登出</span>
                </a>
            </div>
        </aside>
        
        <!-- Main Content -->
        <main class="main-content">
            <header class="header">
                <div class="header-left">
                    <h1>內容管理</h1>
                    <p>內容計畫和文章管理系統</p>
                </div>
                <div class="header-right">
                    <button class="btn btn-primary" onclick="showCreatePlanModal()">
                        <i class="fas fa-plus"></i>
                        建立內容計畫
                    </button>
                </div>
            </header>
            
            <div class="content-management">
                <!-- Content Plans -->
                <div class="content-plans-section">
                    <h2>內容計畫</h2>
                    <?php if (empty($recent_plans)): ?>
                        <div class="empty-state">
                            <i class="fas fa-calendar-alt"></i>
                            <h3>還沒有內容計畫</h3>
                            <p>開始建立您的第一個內容計畫</p>
                        </div>
                    <?php else: ?>
                        <div class="plans-grid">
                            <?php foreach ($recent_plans as $plan): ?>
                                <div class="plan-card">
                                    <div class="plan-header">
                                        <h3><?php echo htmlspecialchars($plan['title']); ?></h3>
                                        <span class="plan-status status-<?php echo $plan['status']; ?>">
                                            <?php echo getStatusName($plan['status']); ?>
                                        </span>
                                    </div>
                                    
                                    <div class="plan-info">
                                        <div class="info-item">
                                            <i class="fas fa-building"></i>
                                            <span><?php echo htmlspecialchars($plan['brand_name']); ?></span>
                                        </div>
                                        <div class="info-item">
                                            <i class="fas fa-calendar"></i>
                                            <span><?php echo $plan['duration_months']; ?> 個月</span>
                                        </div>
                                        <div class="info-item">
                                            <i class="fas fa-clock"></i>
                                            <span>每週 <?php echo $plan['frequency_per_week']; ?> 篇</span>
                                        </div>
                                        <div class="info-item">
                                            <i class="fas fa-calendar-check"></i>
                                            <span><?php echo date('Y-m-d', strtotime($plan['created_at'])); ?></span>
                                        </div>
                                    </div>
                                    
                                    <div class="plan-description">
                                        <p><?php echo htmlspecialchars(substr($plan['description'] ?: '無描述', 0, 100)); ?><?php echo strlen($plan['description'] ?: '') > 100 ? '...' : ''; ?></p>
                                    </div>
                                    
                                    <div class="plan-actions">
                                        <button class="btn btn-secondary" onclick="viewPlan(<?php echo $plan['id']; ?>)">
                                            <i class="fas fa-eye"></i>
                                            查看詳情
                                        </button>
                                        <button class="btn btn-primary" onclick="manageArticles(<?php echo $plan['id']; ?>)">
                                            <i class="fas fa-edit"></i>
                                            管理文章
                                        </button>
                                    </div>
                                </div>
                            <?php endforeach; ?>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        </main>
    </div>
    
    <!-- Create Plan Modal -->
    <div id="createPlanModal" class="modal">
        <div class="modal-content">
            <div class="modal-header">
                <h2>建立內容計畫</h2>
                <button class="modal-close" onclick="closeModal('createPlanModal')">
                    <i class="fas fa-times"></i>
                </button>
            </div>
            <form id="createPlanForm">
                <div class="modal-body">
                    <div class="form-group">
                        <label for="planBrand">選擇品牌 *</label>
                        <select id="planBrand" name="brand_id" class="form-control" required>
                            <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="planTitle">計畫標題 *</label>
                        <input type="text" id="planTitle" name="title" class="form-control" required placeholder="例如：2024年Q1內容行銷計畫">
                    </div>
                    
                    <div class="form-group">
                        <label for="planDescription">計畫描述</label>
                        <textarea id="planDescription" name="description" class="form-control" rows="4" placeholder="請描述這個內容計畫的目標和重點..."></textarea>
                    </div>
                    
                    <div class="form-group">
                        <label for="planKeywords">目標關鍵字</label>
                        <textarea id="planKeywords" name="target_keywords" class="form-control" rows="3" placeholder="請輸入目標關鍵字，用逗號分隔"></textarea>
                    </div>
                    
                    <div class="form-row">
                        <div class="form-group">
                            <label for="planDuration">計畫期間 (月) *</label>
                            <select id="planDuration" name="duration_months" class="form-control" required>
                                <option value="1">1 個月</option>
                                <option value="3" selected>3 個月</option>
                                <option value="6">6 個月</option>
                                <option value="12">12 個月</option>
                            </select>
                        </div>
                        
                        <div class="form-group">
                            <label for="planFrequency">每週發布頻率 *</label>
                            <select id="planFrequency" name="frequency_per_week" class="form-control" required>
                                <option value="1">1 篇/週</option>
                                <option value="2" selected>2 篇/週</option>
                                <option value="3">3 篇/週</option>
                                <option value="5">5 篇/週</option>
                                <option value="7">7 篇/週</option>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" onclick="closeModal('createPlanModal')">取消</button>
                    <button type="submit" class="btn btn-primary">
                        <i class="fas fa-save"></i>
                        建立計畫
                    </button>
                </div>
            </form>
        </div>
    </div>
    
    <!-- Plan Details Modal -->
    <div id="planDetailsModal" class="modal">
        <div class="modal-content">
            <div class="modal-header">
                <h2 id="planDetailsTitle">計畫詳情</h2>
                <button class="modal-close" onclick="closeModal('planDetailsModal')">
                    <i class="fas fa-times"></i>
                </button>
            </div>
            <div class="modal-body">
                <div id="planDetailsContent">
                    <div class="loading">
                        <i class="fas fa-spinner fa-spin"></i>
                        <p>載入中...</p>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" onclick="closeModal('planDetailsModal')">關閉</button>
                <button type="button" class="btn btn-primary" onclick="editPlan()">
                    <i class="fas fa-edit"></i>
                    編輯計畫
                </button>
            </div>
        </div>
    </div>
    
    <!-- Articles Management Modal -->
    <div id="articlesModal" class="modal">
        <div class="modal-content articles-modal">
            <div class="modal-header">
                <h2 id="articlesTitle">文章管理</h2>
                <button class="modal-close" onclick="closeModal('articlesModal')">
                    <i class="fas fa-times"></i>
                </button>
            </div>
            <div class="modal-body">
                <div class="articles-toolbar">
                    <button class="btn btn-primary" onclick="createArticle()">
                        <i class="fas fa-plus"></i>
                        新增文章
                    <button class="btn btn-success" onclick="generateTitle()">
                        <i class="fas fa-magic"></i>
                        生成標題
                    </button>
                    <button class="btn btn-info" onclick="generateContent()">
                        <i class="fas fa-robot"></i>
                        生成內容
                    </button>
                    <button class="btn btn-warning" onclick="optimizeSEO()">
                        <i class="fas fa-search"></i>
                        SEO優化
                    </button>
                    </button>
                </div>
                <div id="articlesList" class="articles-list">
                    <div class="loading">
                        <i class="fas fa-spinner fa-spin"></i>
                        <p>載入中...</p>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <style>
        .content-management {
            padding: 30px;
        }
        
        .content-plans-section {
            margin-bottom: 40px;
        }
        
        .content-plans-section h2 {
            color: #333;
            margin-bottom: 20px;
            font-size: 1.5rem;
        }
        
        .plans-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(400px, 1fr));
            gap: 25px;
        }
        
        .plan-card {
            background: white;
            border-radius: 15px;
            padding: 25px;
            box-shadow: 0 5px 20px rgba(0, 0, 0, 0.08);
            transition: transform 0.3s ease;
        }
        
        .plan-card:hover {
            transform: translateY(-5px);
        }
        
        .plan-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
        }
        
        .plan-header h3 {
            font-size: 1.2rem;
            color: #333;
            margin: 0;
        }
        
        .plan-status {
            padding: 4px 12px;
            border-radius: 20px;
            font-size: 0.8rem;
            font-weight: 500;
        }
        
        .status-active {
            background: #d4edda;
            color: #155724;
        }
        
        .status-paused {
            background: #fff3cd;
            color: #856404;
        }
        
        .status-completed {
            background: #cce5ff;
            color: #004085;
        }
        
        .plan-info {
            margin-bottom: 20px;
        }
        
        .info-item {
            display: flex;
            align-items: center;
            gap: 10px;
            margin-bottom: 10px;
            color: #666;
            font-size: 0.9rem;
        }
        
        .info-item i {
            width: 16px;
            color: #667eea;
        }
        
        .plan-description {
            margin-bottom: 20px;
        }
        
        .plan-description p {
            color: #666;
            line-height: 1.6;
        }
        
        .plan-actions {
            display: flex;
            gap: 10px;
        }
        
        .empty-state {
            text-align: center;
            padding: 80px 20px;
            background: white;
            border-radius: 15px;
            box-shadow: 0 5px 20px rgba(0, 0, 0, 0.08);
        }
        
        .empty-state i {
            font-size: 4rem;
            color: #667eea;
            margin-bottom: 20px;
        }
        
        .empty-state h3 {
            font-size: 1.5rem;
            color: #333;
            margin-bottom: 10px;
        }
        
        .empty-state p {
            color: #666;
            margin-bottom: 30px;
        }
        
        .form-row {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 20px;
        }
        
        .articles-modal .modal-content {
            max-width: 900px;
        }
        
        .articles-toolbar {
            margin-bottom: 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        
        .articles-list {
            max-height: 60vh;
            overflow-y: auto;
        }
        
        .article-item {
            background: #f8f9fa;
            border-radius: 8px;
            padding: 15px;
            margin-bottom: 15px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        
        .article-info h4 {
            margin: 0 0 5px 0;
            color: #333;
        }
        
        .article-meta {
            font-size: 0.9rem;
            color: #666;
        }
        
        .article-actions {
            display: flex;
            gap: 10px;
        }
        
        .loading {
            text-align: center;
            padding: 40px;
            color: #666;
        }
        
        .loading i {
            font-size: 2rem;
            margin-bottom: 15px;
        }
        
        /* Modal Styles */
        .modal {
            display: none;
            position: fixed;
            z-index: 1000;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.5);
        }
        
        .modal-content {
            background-color: white;
            margin: 5% auto;
            border-radius: 15px;
            width: 90%;
            max-width: 600px;
            max-height: 90vh;
            overflow-y: auto;
        }
        
        .modal-header {
            padding: 25px 30px 20px;
            border-bottom: 1px solid #e0e0e0;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        
        .modal-header h2 {
            font-size: 1.5rem;
            font-weight: 600;
            color: #333;
        }
        
        .modal-close {
            background: none;
            border: none;
            font-size: 1.5rem;
            color: #666;
            cursor: pointer;
            padding: 5px;
        }
        
        .modal-body {
            padding: 30px;
        }
        
        .modal-footer {
            padding: 20px 30px 30px;
            border-top: 1px solid #e0e0e0;
            display: flex;
            justify-content: flex-end;
            gap: 15px;
        }
        
        .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;
        }
        
        .btn {
            padding: 12px 25px;
            border: none;
            border-radius: 8px;
            font-size: 1rem;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            text-decoration: none;
            display: inline-flex;
            align-items: center;
            gap: 8px;
        }
        
        .btn-primary {
            background: linear-gradient(135deg, #667eea, #764ba2);
            color: white;
        }
        
        .btn-primary:hover {
        
        .btn-success {
            background: #28a745;
            color: white;
        }
        
        .btn-success:hover {
            background: #218838;
        }
        
        .btn-info {
            background: #17a2b8;
            color: white;
        }
        
        .btn-info:hover {
            background: #138496;
        }
        
        .btn-warning {
            background: #ffc107;
            color: #212529;
        }
        
        .btn-warning:hover {
            background: #e0a800;
        }
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
        }
        
        .btn-secondary {
            background: #6c757d;
            color: white;
        }
        
        .btn-secondary:hover {
            background: #5a6268;
        }
        
        .notification {
            position: fixed;
            top: 20px;
            right: 20px;
            background: white;
            border-radius: 10px;
            box-shadow: 0 5px 20px rgba(0, 0, 0, 0.1);
            padding: 15px 20px;
            z-index: 2000;
            transform: translateX(400px);
            transition: transform 0.3s ease;
        }
        
        .notification.show {
            transform: translateX(0);
        }
        
        .notification-success {
            border-left: 4px solid #28a745;
        }
        
        .notification-error {
            border-left: 4px solid #dc3545;
        }
        
        .notification-info {
            border-left: 4px solid #17a2b8;
        }
        
        .notification-content {
            display: flex;
            align-items: center;
            gap: 10px;
        }
        
        .notification-content i {
            font-size: 1.2rem;
        }
        
        .notification-success .notification-content i {
            color: #28a745;
        }
        
        .notification-error .notification-content i {
            color: #dc3545;
        }
        
        .notification-info .notification-content i {
            color: #17a2b8;
        }
    </style>
    
    <script>
        let currentPlanId = null;
        
        document.addEventListener('DOMContentLoaded', function() {
            // Bind form submission
            const form = document.getElementById('createPlanForm');
            if (form) {
                form.addEventListener('submit', function(e) {
                    e.preventDefault();
                    createContentPlan();
                });
            }
        });
        
        function showCreatePlanModal() {
            document.getElementById('createPlanModal').style.display = 'block';
        }
        
        function closeModal(modalId) {
            document.getElementById(modalId).style.display = 'none';
        }
        
        async function createContentPlan() {
            const form = document.getElementById('createPlanForm');
            const formData = new FormData(form);
            const data = Object.fromEntries(formData);
            data.action = 'create_plan';
            
            const submitBtn = form.querySelector('button[type="submit"]');
            const originalText = submitBtn.innerHTML;
            submitBtn.innerHTML = '<i class="fas fa-spinner fa-spin"></i> 建立中...';
            submitBtn.disabled = true;
            
            try {
                const response = await fetch('/api/content.php', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    credentials: 'same-origin',
                    body: JSON.stringify(data)
                });
                
                const result = await response.json();
                
                if (result.success) {
                    showNotification('內容計畫建立成功！', 'success');
                    closeModal('createPlanModal');
                    form.reset();
                    // Reload page to show new plan
                    setTimeout(() => {
                        location.reload();
                    }, 1000);
                } else {
                    showNotification('建立失敗: ' + result.message, 'error');
                }
            } catch (error) {
                console.error('Error:', error);
                showNotification('網路錯誤，請稍後再試', 'error');
            } finally {
                submitBtn.innerHTML = originalText;
                submitBtn.disabled = false;
            }
        }
        
        async function viewPlan(planId) {
            currentPlanId = planId;
            document.getElementById('planDetailsModal').style.display = 'block';
            
            try {
                const response = await fetch(`/api/content.php?action=plan&plan_id=${planId}`, {
                    credentials: 'same-origin'
                });
                
                const result = await response.json();
                
                if (result.success) {
                    const plan = result.data;
                    document.getElementById('planDetailsTitle').textContent = plan.title;
                    document.getElementById('planDetailsContent').innerHTML = `
                        <div class="plan-details">
                            <div class="detail-section">
                                <h3>基本資訊</h3>
                                <p><strong>品牌:</strong> ${plan.brand_name}</p>
                                <p><strong>狀態:</strong> <span class="plan-status status-${plan.status}">${getStatusName(plan.status)}</span></p>
                                <p><strong>期間:</strong> ${plan.duration_months} 個月</p>
                                <p><strong>頻率:</strong> 每週 ${plan.frequency_per_week} 篇</p>
                                <p><strong>建立時間:</strong> ${new Date(plan.created_at).toLocaleDateString('zh-TW')}</p>
                            </div>
                            
                            <div class="detail-section">
                                <h3>描述</h3>
                                <p>${plan.description || '無描述'}</p>
                            </div>
                            
                            <div class="detail-section">
                                <h3>目標關鍵字</h3>
                                <p>${plan.target_keywords || '未設定'}</p>
                            </div>
                        </div>
                    `;
                } else {
                    document.getElementById('planDetailsContent').innerHTML = '<p>載入計畫詳情失敗: ' + result.message + '</p>';
                }
            } catch (error) {
                document.getElementById('planDetailsContent').innerHTML = '<p>載入計畫詳情失敗: ' + error.message + '</p>';
            }
        }
        
        async function manageArticles(planId) {
            currentPlanId = planId;
            document.getElementById('articlesModal').style.display = 'block';
            
            try {
                const response = await fetch(`/api/content.php?action=articles&plan_id=${planId}`, {
                    credentials: 'same-origin'
                });
                
                const result = await response.json();
                
                if (result.success) {
                    const articles = result.data;
                    document.getElementById('articlesTitle').textContent = `文章管理 (${articles.length} 篇)`;
                    
                    if (articles.length === 0) {
                        document.getElementById('articlesList').innerHTML = '<p>還沒有文章</p>';
                    } else {
                        document.getElementById('articlesList').innerHTML = articles.map(article => `
                            <div class="article-item">
                                <div class="article-info">
                                    <h4>${article.title}</h4>
                                    <div class="article-meta">
                                        <span>發布日期: ${article.publish_date}</span> | 
                                        <span>狀態: ${getArticleStatusName(article.status)}</span>
                                    </div>
                                </div>
                                <div class="article-actions">
                                    <button class="btn btn-secondary" onclick="editArticle(${article.id})">
                                        <i class="fas fa-edit"></i>
                                        編輯
                                    </button>
                                    <button class="btn btn-secondary" onclick="deleteArticle(${article.id})">
                                        <i class="fas fa-trash"></i>
                                        刪除
                                    </button>
                                </div>
                            </div>
                        `).join('');
                    }
                } else {
                    document.getElementById('articlesList').innerHTML = '<p>載入文章列表失敗: ' + result.message + '</p>';
                }
            } catch (error) {
                document.getElementById('articlesList').innerHTML = '<p>載入文章列表失敗: ' + error.message + '</p>';
            }
        }
        
        function createArticle() {
    // Redirect to article editor for new article
    window.location.href = `article_editor.php?plan_id=${currentPlanId}`;
            // Redirect to article editor for new article
            window.location.href = `article_editor.php?plan_id=${currentPlanId}`;
                    <button class="btn btn-success" onclick="generateTitle()">
                        <i class="fas fa-magic"></i>
                        生成標題
                    </button>
                    <button class="btn btn-info" onclick="generateContent()">
                        <i class="fas fa-robot"></i>
                        生成內容
                    </button>
                    <button class="btn btn-warning" onclick="optimizeSEO()">
                        <i class="fas fa-search"></i>
                        SEO優化
                    </button>
        }
        
        function editArticle(articleId) {
    // Redirect to article editor for editing
    window.location.href = `article_editor.php?id=${articleId}`;
            // Redirect to article editor for editing
            window.location.href = `article_editor.php?id=${articleId}`;
        }
        
        function deleteArticle(articleId) {
            if (confirm('確定要刪除此文章嗎？')) {
                fetch('/api/content.php', {
                    method: 'DELETE',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    credentials: 'same-origin',
                    body: JSON.stringify({
                        action: 'delete_article',
                        article_id: articleId
                    })
                })
                .then(response => response.json())
                .then(result => {
                    if (result.success) {
                        showNotification('文章刪除成功！', 'success');
                        loadArticles(currentPlanId);
                    } else {
                        showNotification('刪除失敗: ' + result.message, 'error');
                    }
                })
                .catch(error => {
                    console.error('Error:', error);
                    showNotification('網路錯誤，請稍後再試', 'error');
                });
            }
        }
        }
        
        function editPlan() {
        
        function generateTitle() {
            if (!currentPlanId) {
                showNotification('請先選擇一個內容計畫', 'error');
                return;
            }
            window.location.href = `article_editor.php?plan_id=${currentPlanId}&action=generate_title`;
        }
        
        function generateContent() {
            if (!currentPlanId) {
                showNotification('請先選擇一個內容計畫', 'error');
                return;
            }
            window.location.href = `article_editor.php?plan_id=${currentPlanId}&action=generate_content`;
        }
        
        function optimizeSEO() {
            if (!currentPlanId) {
                showNotification('請先選擇一個內容計畫', 'error');
                return;
            }
            window.location.href = `article_editor.php?plan_id=${currentPlanId}&action=optimize_seo`;
        }
        
        function editPlanModal() {
            showNotification('編輯計畫功能開發中...', 'info');
        }
            editPlanModal();
        }
        
        function getStatusName(status) {
            const statuses = {
                "active": "進行中",
                "paused": "暫停",
                "completed": "已完成",
                "draft": "草稿",
                "in_progress": "進行中",
                "published": "已發布",
                "scheduled": "已排程"
            };
            return statuses[status] || status;
        }
        
        function getArticleStatusName(status) {
            const statuses = {
                "draft": "草稿",
                "published": "已發布",
                "scheduled": "已排程"
            };
            return statuses[status] || status;
        }
        
        function showNotification(message, type = 'success') {
            const notification = document.createElement('div');
            notification.className = `notification notification-${type}`;
            notification.innerHTML = `
                <div class="notification-content">
                    <i class="fas fa-${type === 'success' ? 'check-circle' : type === 'error' ? 'exclamation-circle' : 'info-circle'}"></i>
                    <span>${message}</span>
                </div>
            `;
            
            document.body.appendChild(notification);
            
            setTimeout(() => {
                notification.classList.add('show');
            }, 100);
            
            setTimeout(() => {
                notification.classList.remove('show');
                setTimeout(() => {
                    if (document.body.contains(notification)) {
                        document.body.removeChild(notification);
                    }
                }, 300);
            }, 3000);
        }
    </script>
</body>
</html>

<?php
function getStatusName($status) {
    $statuses = [
        'active' => '進行中',
        'paused' => '暫停',
        'completed' => '已完成',
        'draft' => '草稿',
        'in_progress' => '進行中',
        'published' => '已發布',
        'scheduled' => '已排程'
    ];
    return $statuses[$status] ?? $status;
}

function getArticleStatusName($status) {
    $statuses = [
        'draft' => '草稿',
        'published' => '已發布',
        'scheduled' => '已排程'
    ];
    return $statuses[$status] ?? $status;
}
?>
