<?php
/**
 * Admin Logs View
 */

if (!defined('ABSPATH')) {
    exit;
}
?>

<div class="wrap">
    <h1><?php esc_html_e('SeaXPay Logs', 'paystarsea-crypto-payment-gateway'); ?></h1>
    
    <div class="seaxpay-filters">
        <form method="get">
            <input type="hidden" name="page" value="seaxpay-logs">
            
            <select name="type">
                <option value=""><?php esc_html_e('All Types', 'paystarsea-crypto-payment-gateway'); ?></option>
                <option value="api" <?php selected($type_filter, 'api'); ?>><?php esc_html_e('API', 'paystarsea-crypto-payment-gateway'); ?></option>
                <option value="webhook" <?php selected($type_filter, 'webhook'); ?>><?php esc_html_e('Webhook', 'paystarsea-crypto-payment-gateway'); ?></option>
                <option value="order" <?php selected($type_filter, 'order'); ?>><?php esc_html_e('Order', 'paystarsea-crypto-payment-gateway'); ?></option>
                <option value="error" <?php selected($type_filter, 'error'); ?>><?php esc_html_e('Error', 'paystarsea-crypto-payment-gateway'); ?></option>
            </select>
            
            <input type="submit" class="button" value="<?php esc_attr_e('Filter', 'paystarsea-crypto-payment-gateway'); ?>">
        </form>
    </div>
    
    <table class="wp-list-table widefat fixed striped">
        <thead>
            <tr>
                <th width="5%"><?php esc_html_e('ID', 'paystarsea-crypto-payment-gateway'); ?></th>
                <th width="10%"><?php esc_html_e('Type', 'paystarsea-crypto-payment-gateway'); ?></th>
                <th width="10%"><?php esc_html_e('WC Order', 'paystarsea-crypto-payment-gateway'); ?></th>
                <th width="15%"><?php esc_html_e('Platform Order', 'paystarsea-crypto-payment-gateway'); ?></th>
                <th width="40%"><?php esc_html_e('Message', 'paystarsea-crypto-payment-gateway'); ?></th>
                <th width="20%"><?php esc_html_e('Time', 'paystarsea-crypto-payment-gateway'); ?></th>
            </tr>
        </thead>
        <tbody>
            <?php if (empty($logs)): ?>
                <tr>
                    <td colspan="6" style="text-align: center;">
                        <?php esc_html_e('No logs found', 'paystarsea-crypto-payment-gateway'); ?>
                    </td>
                </tr>
            <?php else: ?>
                <?php // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound ?>
                <?php foreach ($logs as $log): ?>
                    <tr>
                        <td><?php echo esc_html($log->id); ?></td>
                        <td>
                            <span class="seaxpay-log-type seaxpay-log-type-<?php echo esc_attr($log->log_type); ?>">
                                <?php echo esc_html(ucfirst($log->log_type)); ?>
                            </span>
                        </td>
                        <td>
                            <?php if ($log->wc_order_id): ?>
                                <a href="<?php echo esc_url(admin_url('post.php?post=' . $log->wc_order_id . '&action=edit')); ?>">
                                    #<?php echo esc_html($log->wc_order_id); ?>
                                </a>
                            <?php else: ?>
                                -
                            <?php endif; ?>
                        </td>
                        <td>
                            <?php if ($log->platform_order_no): ?>
                                <code style="font-size: 11px;"><?php echo esc_html($log->platform_order_no); ?></code>
                            <?php else: ?>
                                -
                            <?php endif; ?>
                        </td>
                        <td><?php echo esc_html($log->message); ?></td>
                        <td><?php echo esc_html($log->create_time); ?></td>
                    </tr>
                <?php endforeach; ?>
            <?php endif; ?>
        </tbody>
    </table>
    
    <?php
    // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
    $total_pages = ceil($total / $per_page);
    if ($total_pages > 1):
    ?>
        <div class="tablenav">
            <div class="tablenav-pages">
                <?php
                echo wp_kses_post(paginate_links(array(
                    'base' => add_query_arg('paged', '%#%'),
                    'format' => '',
                    'prev_text' => __('&laquo;', 'paystarsea-crypto-payment-gateway'),
                    'next_text' => __('&raquo;', 'paystarsea-crypto-payment-gateway'),
                    'total' => $total_pages,
                    'current' => $paged
                )));
                ?>
            </div>
        </div>
    <?php endif; ?>
</div>
