<?php
if (!defined('ABSPATH')) {
    die('You are not allowed to call this page directly.');
}

// CSS и JS се зареждат автоматично от formshield.php

// Localize script with AJAX URL and nonce
wp_localize_script('formshield-admin-js', 'ajaxConfig', array(
    'url' => admin_url('admin-ajax.php'),
    'nonce' => wp_create_nonce('formshield_form_enabled_change'),
    'isPremium' => false,
    'protectedFormsCt' => 0 // TODO: Get actual count
));

$options = form_shield()->get_options();
$forms_data = FormShieldForms::get_forms();

// Flatten the forms array for counting
$forms = array();
$protected_forms = array();

if (isset($forms_data['scanner'])) {
    $forms[] = $forms_data['scanner'];
    $protected_forms[] = $forms_data['scanner']; // Scanner is always protected
}

if (isset($forms_data['comments']) && is_array($forms_data['comments'])) {
    foreach ($forms_data['comments'] as $comment_form) {
        $forms[] = $comment_form;
        $protected_forms[] = $comment_form; // Comments are always protected
    }
}

if (isset($forms_data['plugins']) && is_array($forms_data['plugins'])) {
    foreach ($forms_data['plugins'] as $plugin_form) {
        $forms[] = $plugin_form;
        if (isset($plugin_form['enabled']) && $plugin_form['enabled'] == 1) {
            $protected_forms[] = $plugin_form;
        }
    }
}

// Изчисли процента на защита
$protection_percentage = count($forms) > 0 ? round((count($protected_forms) / count($forms)) * 100) : 0;

// Вземи последните атаки
$blocked_attempts = isset($options['blocked_attempts']) ? intval($options['blocked_attempts']) : 0;
$last_attack = isset($options['last_attack']) ? $options['last_attack'] : '';
$attack_types = isset($options['attack_types']) ? $options['attack_types'] : array(
    'spam' => 0,
    'bot' => 0,
    'injection' => 0,
    'other' => 0
);
?>

<div class="plugin-dashboard">
    <div class="dashboard-header">
        <h1>FormShield Dashboard</h1>
        <p class="subtitle">Edge-to-edge light UI. Sections are divided by lines, not cards.</p>
    </div>
    
    <section>
        <h2>Quick Actions</h2>
        <div class="protection-summary">
            <div class="actions">
                <button type="button" id="formshield-scan-all" class="btn-primary">🔍 Scan All Pages for Forms</button>
                <button type="button" id="formshield-scan-divi" class="btn-secondary">🎨 Scan for Divi Forms</button>
                <button type="button" id="formshield-update-protected" class="btn-secondary">🔄 Update Protected Forms</button>
                <p class="note">Use these tools to scan your site and manage form protection.</p>
        <div id="formshield-scan-status"></div>
            </div>
        </div>
    </section>

    <section>
        <h2>Protection Summary</h2>
        <div class="protection-summary">
            <div class="protection-status">
                <div class="progress-bar"><span style="width:<?php echo esc_attr($protection_percentage); ?>%"></span></div>
                <div class="value"><?php echo esc_html($protection_percentage); ?>% <span class="small">Protected</span></div>
                <div class="meta">
                    <span><?php echo esc_html(count($forms)); ?> Total Form<?php echo count($forms) !== 1 ? 's' : ''; ?></span>
                    <span><?php echo esc_html(count($protected_forms)); ?> Protected</span>
                </div>
                </div>
            <div class="blocked-stats">
                <div><label>Spam</label><strong><?php echo esc_html($attack_types['spam']); ?></strong></div>
                <div><label>Bots</label><strong><?php echo esc_html($attack_types['bot']); ?></strong></div>
                <div><label>Injection</label><strong><?php echo esc_html($attack_types['injection']); ?></strong></div>
                <div><label>Other</label><strong><?php echo esc_html($attack_types['other']); ?></strong></div>
                </div>
            <div class="actions">
                <p class="note">Protection status overview and attack statistics.</p>
                <?php if ($last_attack): ?>
                <p class="note">Last attack: <?php echo esc_html($last_attack); ?></p>
                <?php endif; ?>
            </div>
        </div>
    </section>

    <section>
        <h2>Forms Overview</h2>
        <?php if (!empty($forms)): ?>
            <table class="forms-table">
                <thead>
                    <tr>
                        <th>Enabled</th>
                        <th>Form Name</th>
                        <th>Type</th>
                        <th>Location</th>
                        <th>Blocked Attempts</th>
                        <th>Activity</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach (array_slice($forms, 0, 5) as $form): ?>
                        <tr>
                            <td>
                                <div class="formshield-toggle-wrapper">
                    <label class="switch">
                                        <input type="checkbox" 
                                               class="formshield-toggle" 
                                               data-form-id="<?php echo esc_attr($form['id']); ?>" 
                                               <?php checked(isset($form['enabled']) ? $form['enabled'] : 1, 1); ?>>
                        <span class="slider"></span>
                    </label>
                                    <span class="toggle-status <?php echo (isset($form['enabled']) ? $form['enabled'] : 1) ? 'enabled' : 'disabled'; ?>">
                                        <?php echo (isset($form['enabled']) ? $form['enabled'] : 1) ? 'Enabled' : 'Disabled'; ?>
                                    </span>
                </div>
                            </td>
                            <td><?php echo esc_html($form['name']); ?></td>
                            <td><span class="tag"><?php echo esc_html($form['type']); ?></span></td>
                            <td><?php echo esc_html($form['location']); ?></td>
                            <td><?php echo esc_html(isset($form['blocked_attempts']) ? $form['blocked_attempts'] : 0); ?></td>
                            <td><?php echo esc_html(isset($form['activity']) ? $form['activity'] : 'No activity'); ?></td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
            <div class="table-footer">
                <span class="items"><?php echo count($forms); ?> item<?php echo count($forms) !== 1 ? 's' : ''; ?></span>
                <a href="<?php echo esc_url(admin_url('admin.php?page=formshield')); ?>" class="btn-primary">View All Forms</a>
            </div>
        <?php else: ?>
            <div style="text-align: center; padding: 40px 20px; color: var(--muted);">
                <p>No forms detected yet. Use the scan tools above to find forms on your site.</p>
            </div>
        <?php endif; ?>
    </section>

    <section>
        <h2>Analytics Overview</h2>
        <div class="analytics">
            <div><label>Forms Protected</label><strong><?php echo esc_html(count($protected_forms)); ?></strong></div>
            <div><label>Attacks Blocked</label><strong><?php echo esc_html($blocked_attempts); ?></strong></div>
            <div><label>Protection Rate</label><strong><?php echo esc_html($protection_percentage); ?>%</strong></div>
            <div><label>Uptime</label><strong>100%</strong></div>
        </div>
    </section>
</div>

<?php
// Enqueue options home script
wp_enqueue_script( 'formshield-admin-options', plugins_url( 'js/options-home.js', dirname(__FILE__) ), array('jquery'), FORMSHIELD_VERSION, true );
?>