<?php defined('ABSPATH') || exit;
/**
 * @var JRA_Merchant_Logger $logger
 * @var object[]            $entries
 * @var int                 $total
 * @var int                 $pages
 * @var int                 $page
 * @var string              $filter
 */

$statusLabels = [
    'info'    => '<span class="jra-badge jra-badge--blue">info</span>',
    'warning' => '<span class="jra-badge jra-badge--orange">warning</span>',
    'error'   => '<span class="jra-badge jra-badge--red">error</span>',
];
?>
<div class="wrap jra-merchant">
    <h1>Google Merchant Feed — Log</h1>

    <div class="jra-log-toolbar">
        <!-- Status filter -->
        <span>
            <a href="<?php echo esc_url(admin_url('admin.php?page=jra-merchant-feed-log')) ?>"
               class="button <?php echo $filter === '' ? 'button-primary' : '' ?>">All (<?php echo esc_html($logger->countAll()) ?>)</a>
            <a href="<?php echo esc_url(admin_url('admin.php?page=jra-merchant-feed-log&status=error')) ?>"
               class="button <?php echo $filter === 'error' ? 'button-primary' : '' ?>">Errors (<?php echo esc_html($logger->countAll('error')) ?>)</a>
            <a href="<?php echo esc_url(admin_url('admin.php?page=jra-merchant-feed-log&status=warning')) ?>"
               class="button <?php echo $filter === 'warning' ? 'button-primary' : '' ?>">Warnings (<?php echo esc_html($logger->countAll('warning')) ?>)</a>
            <a href="<?php echo esc_url(admin_url('admin.php?page=jra-merchant-feed-log&status=info')) ?>"
               class="button <?php echo $filter === 'info' ? 'button-primary' : '' ?>">Info (<?php echo esc_html($logger->countAll('info')) ?>)</a>
        </span>

        <!-- Clear log -->
        <form method="post" action="<?php echo esc_url(admin_url('admin-post.php')) ?>" style="display:inline">
            <?php wp_nonce_field('jra_merchant_clear_log') ?>
            <input type="hidden" name="action" value="jra_merchant_clear_log">
            <button type="submit" class="button button-link-delete"
                    onclick="return confirm('Clear all log entries?')">Clear log</button>
        </form>
    </div>

    <?php if (empty($entries)): ?>
        <p>No log entries<?php echo $filter ? ' matching the selected filter' : '' ?>.</p>
    <?php else: ?>
    <table class="widefat striped jra-log-table">
        <thead>
            <tr>
                <th style="width:150px">Time (UTC)</th>
                <th style="width:60px">Type</th>
                <th style="width:60px">Status</th>
                <th style="width:80px">Product</th>
                <th style="width:160px">Offer ID</th>
                <th>Message</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($entries as $entry): ?>
            <tr class="jra-log-row--<?php echo esc_attr($entry->status) ?>">
                <td style="white-space:nowrap;font-size:12px"><?php echo esc_html($entry->created_at) ?></td>
                <td><code><?php echo esc_html($entry->sync_type ?: '—') ?></code></td>
                <td><?php echo wp_kses($statusLabels[$entry->status] ?? esc_html($entry->status), ['span' => ['class' => []]]) ?></td>
                <td>
                    <?php if ($entry->product_id): ?>
                        <a href="<?php echo esc_url(get_edit_post_link((int)$entry->product_id)) ?>">
                            <?php echo esc_html($entry->product_id) ?>
                        </a>
                    <?php else: ?>—<?php endif; ?>
                </td>
                <td style="font-size:12px"><?php echo esc_html($entry->offer_id ?? '—') ?></td>
                <td><?php echo esc_html($entry->message) ?></td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>

    <!-- Pagination -->
    <?php if ($pages > 1): ?>
    <div class="tablenav bottom">
        <div class="tablenav-pages">
            <span class="displaying-num"><?php echo esc_html($total) ?> items</span>
            <?php
            $baseUrl = admin_url('admin.php?page=jra-merchant-feed-log' . ($filter ? '&status=' . urlencode($filter) : ''));
            for ($p = 1; $p <= $pages; $p++):
            ?>
                <a href="<?php echo esc_url($baseUrl . '&paged=' . $p) ?>"
                   class="button <?php echo $p === $page ? 'button-primary' : '' ?>"><?php echo esc_html( (string) $p ) ?></a>
            <?php endfor; ?>
        </div>
    </div>
    <?php endif; ?>
    <?php endif; ?>
</div>
