<?php
/**
 * Consent Logs View
 */
if (!defined('ABSPATH')) {
    exit;
}

global $wpdb;
$table_name = $wpdb->prefix . 'mgc_consent_logs';

// Get logs with pagination
$per_page = 50;
$page = isset($_GET['paged']) ? absint($_GET['paged']) : 1;
$offset = ($page - 1) * $per_page;

$logs = $wpdb->get_results(
    $wpdb->prepare(
        "SELECT * FROM $table_name ORDER BY consent_date DESC LIMIT %d OFFSET %d",
        $per_page,
        $offset
    )
);

$total_logs = $wpdb->get_var("SELECT COUNT(*) FROM $table_name");
$total_pages = ceil($total_logs / $per_page);
?>

<div class="wrap mgc-admin-wrap">
    <h1><?php echo esc_html(get_admin_page_title()); ?></h1>
    
    <div class="mgc-panel">
        <div class="mgc-panel-header">
            <h3><?php _e('Consent Logs', 'maple-gdpr-cookies'); ?></h3>
        </div>
        <div class="mgc-panel-body">
            <p><?php printf(__('Total logs: %d', 'maple-gdpr-cookies'), $total_logs); ?></p>
            
            <?php if (!empty($logs)) : ?>
                <table class="wp-list-table widefat fixed striped">
                    <thead>
                        <tr>
                            <th><?php _e('Date', 'maple-gdpr-cookies'); ?></th>
                            <th><?php _e('User', 'maple-gdpr-cookies'); ?></th>
                            <th><?php _e('IP Address', 'maple-gdpr-cookies'); ?></th>
                            <th><?php _e('Consent Type', 'maple-gdpr-cookies'); ?></th>
                            <th><?php _e('Categories', 'maple-gdpr-cookies'); ?></th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($logs as $log) : ?>
                            <tr>
                                <td><?php echo esc_html($log->consent_date); ?></td>
                                <td>
                                    <?php 
                                    if ($log->user_id) {
                                        $user = get_userdata($log->user_id);
                                        echo $user ? esc_html($user->display_name) : __('Unknown', 'maple-gdpr-cookies');
                                    } else {
                                        echo __('Guest', 'maple-gdpr-cookies');
                                    }
                                    ?>
                                </td>
                                <td><?php echo esc_html($log->ip_address); ?></td>
                                <td>
                                    <span class="mgc-consent-<?php echo esc_attr($log->consent_type); ?>">
                                        <?php echo esc_html(ucfirst($log->consent_type)); ?>
                                    </span>
                                </td>
                                <td>
                                    <?php 
                                    $categories = json_decode($log->categories, true);
                                    if (!empty($categories) && is_array($categories)) {
                                        echo esc_html(implode(', ', $categories));
                                    } else {
                                        echo __('N/A', 'maple-gdpr-cookies');
                                    }
                                    ?>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
                
                <?php if ($total_pages > 1) : ?>
                    <div class="tablenav">
                        <div class="tablenav-pages">
                            <?php
                            echo paginate_links(array(
                                'base' => add_query_arg('paged', '%#%'),
                                'format' => '',
                                'prev_text' => __('&laquo;'),
                                'next_text' => __('&raquo;'),
                                'total' => $total_pages,
                                'current' => $page
                            ));
                            ?>
                        </div>
                    </div>
                <?php endif; ?>
            <?php else : ?>
                <p><?php _e('No consent logs found.', 'maple-gdpr-cookies'); ?></p>
            <?php endif; ?>
        </div>
    </div>
</div>
