<?php
$pageTitle = 'Code & Ads';
ob_start();
?>

<div class="admin-card">
    <h2 class="text-xl font-semibold mb-4">Code & Ads Management</h2>
    <p class="text-gray-300 mb-6">Manage custom code injection and ad placements across your website.</p>
    
    <?php if ($success): ?>
    <div class="alert-success mb-6">
        <i class="fas fa-check-circle mr-2"></i>
        Settings saved successfully!
    </div>
    <?php endif; ?>
    
    <form class="admin-form" method="POST" action="/admin/code-and-ads">
        <!-- Code Injection Section -->
        <div class="mb-8 border-b border-gray-800 pb-6">
            <h3 class="text-lg font-medium mb-4 text-primary">Code Injection</h3>
            <p class="text-gray-400 mb-4">Add custom HTML, CSS, or JavaScript code to different parts of your website.</p>
            
            <div class="mb-4">
                <label for="head_code">Head Code</label>
                <p class="text-sm text-gray-400 mb-2">This code will be added to the &lt;head&gt; section of your website. Useful for meta tags, analytics, or CSS.</p>
                <textarea id="head_code" name="head_code" rows="6" class="code-editor"><?= htmlspecialchars($settings['head_code'] ?? '') ?></textarea>
            </div>
            
            <div class="mb-4">
                <label for="body_start_code">Body Start Code</label>
                <p class="text-sm text-gray-400 mb-2">This code will be added right after the opening &lt;body&gt; tag. Useful for analytics or top banners.</p>
                <textarea id="body_start_code" name="body_start_code" rows="6" class="code-editor"><?= htmlspecialchars($settings['body_start_code'] ?? '') ?></textarea>
            </div>
            
            <div>
                <label for="body_end_code">Body End Code</label>
                <p class="text-sm text-gray-400 mb-2">This code will be added right before the closing &lt;/body&gt; tag. Useful for scripts that need to load after the page content.</p>
                <textarea id="body_end_code" name="body_end_code" rows="6" class="code-editor"><?= htmlspecialchars($settings['body_end_code'] ?? '') ?></textarea>
            </div>
        </div>
        
        <!-- Ad Management Section -->
        <div class="mb-8 border-b border-gray-800 pb-6">
            <h3 class="text-lg font-medium mb-4 text-primary">Ad Management</h3>
            
            <div class="mb-6">
                <label class="flex items-center">
                    <input type="checkbox" name="enable_ads" value="1" <?= (isset($settings['enable_ads']) && $settings['enable_ads'] == '1') ? 'checked' : '' ?> class="mr-2">
                    <span>Enable Ads</span>
                </label>
                <p class="text-sm text-gray-400 mt-1">When disabled, no ads will be displayed on your website regardless of the settings below.</p>
            </div>
            
            <div class="mb-4">
                <label for="header_ad">Header Ad</label>
                <p class="text-sm text-gray-400 mb-2">This ad will be displayed at the top of your website, below the navigation bar.</p>
                <textarea id="header_ad" name="header_ad" rows="4" class="code-editor"><?= htmlspecialchars($settings['header_ad'] ?? '') ?></textarea>
            </div>
            
            <div class="mb-4">
                <label for="footer_ad">Footer Ad</label>
                <p class="text-sm text-gray-400 mb-2">This ad will be displayed at the bottom of your website, above the footer.</p>
                <textarea id="footer_ad" name="footer_ad" rows="4" class="code-editor"><?= htmlspecialchars($settings['footer_ad'] ?? '') ?></textarea>
            </div>
            
            <div class="mb-4">
                <label for="sidebar_ad">Sidebar Ad</label>
                <p class="text-sm text-gray-400 mb-2">This ad will be displayed in the sidebar where applicable.</p>
                <textarea id="sidebar_ad" name="sidebar_ad" rows="4" class="code-editor"><?= htmlspecialchars($settings['sidebar_ad'] ?? '') ?></textarea>
            </div>
            
            <div class="mb-4">
                <label for="between_content_ad">Between Content Ad</label>
                <p class="text-sm text-gray-400 mb-2">This ad will be displayed between content sections on various pages.</p>
                <textarea id="between_content_ad" name="between_content_ad" rows="4" class="code-editor"><?= htmlspecialchars($settings['between_content_ad'] ?? '') ?></textarea>
            </div>
        </div>
        
        <!-- Page-Specific Ads Section -->
        <div class="mb-8">
            <h3 class="text-lg font-medium mb-4 text-primary">Page-Specific Ads</h3>
            <p class="text-gray-400 mb-4">Configure ads for specific page types. These will be displayed in addition to the general ads above.</p>
            
            <div class="mb-4">
                <label for="game_page_ad">Game Page Ad</label>
                <p class="text-sm text-gray-400 mb-2">This ad will be displayed on individual game pages.</p>
                <textarea id="game_page_ad" name="game_page_ad" rows="4" class="code-editor"><?= htmlspecialchars($settings['game_page_ad'] ?? '') ?></textarea>
            </div>
            
            <div class="mb-4">
                <label for="category_page_ad">Category Page Ad</label>
                <p class="text-sm text-gray-400 mb-2">This ad will be displayed on category pages.</p>
                <textarea id="category_page_ad" name="category_page_ad" rows="4" class="code-editor"><?= htmlspecialchars($settings['category_page_ad'] ?? '') ?></textarea>
            </div>
            
            <div class="mb-4">
                <label for="blog_page_ad">Blog Page Ad</label>
                <p class="text-sm text-gray-400 mb-2">This ad will be displayed on the main blog page.</p>
                <textarea id="blog_page_ad" name="blog_page_ad" rows="4" class="code-editor"><?= htmlspecialchars($settings['blog_page_ad'] ?? '') ?></textarea>
            </div>
            
            <div class="mb-4">
                <label for="blog_post_ad">Blog Post Ad</label>
                <p class="text-sm text-gray-400 mb-2">This ad will be displayed on individual blog post pages.</p>
                <textarea id="blog_post_ad" name="blog_post_ad" rows="4" class="code-editor"><?= htmlspecialchars($settings['blog_post_ad'] ?? '') ?></textarea>
            </div>
            
            <div>
                <label for="search_page_ad">Search Page Ad</label>
                <p class="text-sm text-gray-400 mb-2">This ad will be displayed on search result pages.</p>
                <textarea id="search_page_ad" name="search_page_ad" rows="4" class="code-editor"><?= htmlspecialchars($settings['search_page_ad'] ?? '') ?></textarea>
            </div>
        </div>
        
        <div class="flex justify-end">
            <button type="submit" class="btn-primary">
                <i class="fas fa-save mr-2"></i>Save Changes
            </button>
        </div>
    </form>
</div>

<script>
    // Add syntax highlighting for code editors
    document.addEventListener('DOMContentLoaded', function() {
        const codeEditors = document.querySelectorAll('.code-editor');
        codeEditors.forEach(editor => {
            editor.style.fontFamily = 'monospace';
        });
    });
</script>

<?php
$content = ob_get_clean();
include __DIR__ . '/layout.php';
?> 