<?php
/**
 * Admin Orders View
 */

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

<div class="wrap">
    <h1><?php esc_html_e('SeaXPay Orders', 'paystarsea-crypto-payment-gateway'); ?></h1>
    
    <div class="seaxpay-filters">
        <form method="get">
            <input type="hidden" name="page" value="seaxpay">
            
            <select name="status">
                <option value=""><?php esc_html_e('All Statuses', 'paystarsea-crypto-payment-gateway'); ?></option>
                <option value="pending" <?php selected($status_filter, 'pending'); ?>><?php esc_html_e('Pending', 'paystarsea-crypto-payment-gateway'); ?></option>
                <option value="confirming" <?php selected($status_filter, 'confirming'); ?>><?php esc_html_e('Confirming', 'paystarsea-crypto-payment-gateway'); ?></option>
                <option value="paid" <?php selected($status_filter, 'paid'); ?>><?php esc_html_e('Paid', 'paystarsea-crypto-payment-gateway'); ?></option>
                <option value="expired" <?php selected($status_filter, 'expired'); ?>><?php esc_html_e('Expired', 'paystarsea-crypto-payment-gateway'); ?></option>
                <option value="failed" <?php selected($status_filter, 'failed'); ?>><?php esc_html_e('Failed', 'paystarsea-crypto-payment-gateway'); ?></option>
                <option value="risk_blocked" <?php selected($status_filter, 'risk_blocked'); ?>><?php esc_html_e('Under Review', 'paystarsea-crypto-payment-gateway'); ?></option>
                <option value="refunded" <?php selected($status_filter, 'refunded'); ?>><?php esc_html_e('Refunded', '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><?php esc_html_e('ID', 'paystarsea-crypto-payment-gateway'); ?></th>
                <th><?php esc_html_e('WC Order', 'paystarsea-crypto-payment-gateway'); ?></th>
                <th><?php esc_html_e('Platform Order', 'paystarsea-crypto-payment-gateway'); ?></th>
                <th><?php esc_html_e('Coin/Network', 'paystarsea-crypto-payment-gateway'); ?></th>
                <th><?php esc_html_e('Amount', 'paystarsea-crypto-payment-gateway'); ?></th>
                <th><?php esc_html_e('Status', 'paystarsea-crypto-payment-gateway'); ?></th>
                <th><?php esc_html_e('TX Hash', 'paystarsea-crypto-payment-gateway'); ?></th>
                <th><?php esc_html_e('Created', 'paystarsea-crypto-payment-gateway'); ?></th>
            </tr>
        </thead>
        <tbody>
            <?php if (empty($orders)): ?>
                <tr>
                    <td colspan="8" style="text-align: center;">
                        <?php esc_html_e('No orders found', 'paystarsea-crypto-payment-gateway'); ?>
                    </td>
                </tr>
            <?php else: ?>
                <?php foreach ($orders as $order): ?>
                    <tr>
                        <td><?php echo esc_html($order->id); ?></td>
                        <td>
                            <a href="<?php echo esc_url(admin_url('post.php?post=' . $order->wc_order_id . '&action=edit')); ?>">
                                #<?php echo esc_html($order->wc_order_id); ?>
                            </a>
                        </td>
                        <td><?php echo esc_html($order->platform_order_no); ?></td>
                        <td><?php echo esc_html($order->coin . '/' . $order->network); ?></td>
                        <td>
                            <?php echo esc_html($order->fiat_amount . ' ' . $order->fiat_currency); ?>
                            <?php if ($order->crypto_amount): ?>
                                <br><small><?php echo esc_html($order->crypto_amount . ' ' . $order->coin); ?></small>
                            <?php endif; ?>
                        </td>
                        <td>
                            <?php
                            // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
                            $status_labels = array(
                                'pending'        => 'Pending',
                                'confirming'     => 'Confirming',
                                'paid'           => 'Paid',
                                'expired'        => 'Expired',
                                'failed'         => 'Failed',
                                'risk_blocked'   => 'Under Review',
                                'risk_rejected'  => 'Rejected',
                                'risk_refunded'  => 'Refunded (Risk)',
                                'refunded'       => 'Refunded',
                            );
                            // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
                            $label = isset($status_labels[$order->status]) ? $status_labels[$order->status] : ucfirst($order->status);
                            ?>
                            <span class="seaxpay-status seaxpay-status-<?php echo esc_attr($order->status); ?>">
                                <?php echo esc_html($label); ?>
                            </span>
                        </td>
                        <td>
                            <?php if ($order->tx_hash): ?>
                                <code style="font-size: 11px;"><?php echo esc_html(substr($order->tx_hash, 0, 16) . '...'); ?></code>
                            <?php else: ?>
                                -
                            <?php endif; ?>
                        </td>
                        <td><?php echo esc_html($order->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>
