<?php
if (!defined('ABSPATH')) exit;

global $wpdb;
$table = $wpdb->prefix . 'dima_security_logs';

// پاکسازی
if (isset($_POST['clear_logs']) && check_admin_referer('dima_clear_logs_nonce')) {
    $wpdb->query("TRUNCATE TABLE $table");
    echo '<div class="notice notice-success"><p>لاگ‌ها پاک شدند.</p></div>';
}

$filter = isset($_GET['filter']) ? sanitize_text_field($_GET['filter']) : 'all';
$where = $filter !== 'all' ? $wpdb->prepare("WHERE severity = %s", $filter) : '';
$logs = $wpdb->get_results("SELECT * FROM $table $where ORDER BY created_at DESC LIMIT 200");

$severity_colors = array('info' => '#3b82f6', 'warning' => '#f59e0b', 'error' => '#ef4444', 'critical' => '#dc2626');
?>

<div class="wrap">
    <h1>🔒 لاگ‌های امنیتی</h1>
    
    <div class="dima-filter-bar">
        <a href="?page=dima-shop-security-logs&filter=all" class="button <?php echo $filter === 'all' ? 'button-primary' : ''; ?>">همه</a>
        <a href="?page=dima-shop-security-logs&filter=info" class="button <?php echo $filter === 'info' ? 'button-primary' : ''; ?>">ℹ️ اطلاعات</a>
        <a href="?page=dima-shop-security-logs&filter=warning" class="button <?php echo $filter === 'warning' ? 'button-primary' : ''; ?>">⚠️ هشدار</a>
        <a href="?page=dima-shop-security-logs&filter=error" class="button <?php echo $filter === 'error' ? 'button-primary' : ''; ?>">❌ خطا</a>
        <a href="?page=dima-shop-security-logs&filter=critical" class="button <?php echo $filter === 'critical' ? 'button-primary' : ''; ?>">🚨 بحرانی</a>
        
        <form method="post" style="margin-right:auto;">
            <?php wp_nonce_field('dima_clear_logs_nonce'); ?>
            <button type="submit" name="clear_logs" class="button button-link-delete" onclick="return confirm('حذف همه لاگ‌ها؟');">
                🗑️ پاک کردن همه
            </button>
        </form>
    </div>
    
    <?php if (empty($logs)): ?>
    <div class="empty-state">
        <div class="empty-icon">✅</div>
        <h3>هیچ لاگ امنیتی ثبت نشده است</h3>
        <p>سیستم امنیتی به درستی کار می‌کند.</p>
    </div>
    <?php else: ?>
    <div class="table-responsive">
        <table class="wp-list-table widefat fixed striped">
            <thead>
                <tr>
                    <th>زمان</th>
                    <th>نوع رویداد</th>
                    <th>شدت</th>
                    <th>IP</th>
                    <th>کاربر</th>
                    <th>جزئیات</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($logs as $log): 
                    $user = $log->user_id > 0 ? get_userdata($log->user_id) : null;
                ?>
                <tr>
                    <td><?php echo date_i18n('j F Y - H:i:s', strtotime($log->created_at)); ?></td>
                    <td><code><?php echo esc_html($log->event_type); ?></code></td>
                    <td>
                        <span style="background:<?php echo $severity_colors[$log->severity] ?? '#6b7280'; ?>; color:white; padding:4px 12px; border-radius:20px; font-size:12px; font-weight:600;">
                            <?php echo esc_html($log->severity); ?>
                        </span>
                    </td>
                    <td><code><?php echo esc_html($log->ip_address); ?></code></td>
                    <td><?php echo $user ? esc_html($user->display_name) : 'مهمان'; ?></td>
                    <td>
                        <details>
                            <summary style="cursor:pointer; color:#6366f1;">نمایش جزئیات</summary>
                            <pre style="background:#f3f4f6; padding:10px; border-radius:6px; margin-top:8px; direction:ltr; text-align:left; max-width:400px; overflow-x:auto;"><?php echo esc_html($log->details); ?></pre>
                        </details>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <?php endif; ?>
</div>

<style>
.dima-filter-bar { display: flex; gap: 8px; margin: 20px 0; align-items: center; flex-wrap: wrap; }
</style>