<?php
/**
 * NFe Order Meta Box Template
 * 
 * @var int    $order_id       Order ID
 * @var string  $invoice_id     Invoice ID from NFE server
 * @var string  $invoice_status Invoice status
 * @var string  $invoice_number Invoice number
 * @var string  $access_key    NFe access key (chave de acesso)
 * @var string  $error_message  Error message if any
 * @var string  $nonce          Nonce for AJAX requests
 * @var string  $ajax_url       AJAX URL
 */

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

<div class="cloudxm-nfe-meta-box">
    <?php if ($invoice_id): ?>
        <!-- NFe Issued -->
        <div class="cloudxm-nfe-issued">
            <h4 style="margin-top:0;"><?php esc_html_e('NFe Issued', 'woocommerce-cloudxm-nfe'); ?></h4>
            
            <div class="cloudxm-nfe-info">
                <?php if ($invoice_number): ?>
                    <p><strong><?php esc_html_e('Invoice Number:', 'woocommerce-cloudxm-nfe'); ?></strong>
                    <?php echo esc_html($invoice_number); ?></p>
                <?php endif; ?>
                
                <?php if ($access_key): ?>
                    <p><strong><?php esc_html_e('Access Key:', 'woocommerce-cloudxm-nfe'); ?></strong>
                    <code style="font-size:11px;"><?php echo esc_html($access_key); ?></code></p>
                <?php endif; ?>
                
                <?php if ($invoice_status): ?>
                    <p><strong><?php esc_html_e('Status:', 'woocommerce-cloudxm-nfe'); ?></strong>
                    <span class="cloudxm-nfe-status status-<?php echo esc_attr($invoice_status); ?>">
                        <?php 
                        $status_labels = [
                            'processing' => __('Processing', 'woocommerce-cloudxm-nfe'),
                            'authorized' => __('Authorized', 'woocommerce-cloudxm-nfe'),
                            'cancelled' => __('Cancelled', 'woocommerce-cloudxm-nfe'),
                            'error' => __('Error', 'woocommerce-cloudxm-nfe'),
                        ];
                        echo esc_html($status_labels[$invoice_status] ?? ucfirst($invoice_status));
                        ?>
                    </span>
                </p>
                <?php endif; ?>
            </div>
            
            <div class="cloudxm-nfe-actions">
                <button type="button" class="button button-small cloudxm-nfe-query-status"
                        data-order-id="<?php echo esc_attr($order_id); ?>"
                        data-nonce="<?php echo esc_attr($nonce); ?>">
                    <?php esc_html_e('Query Status', 'woocommerce-cloudxm-nfe'); ?>
                </button>
                
                <?php if (in_array($invoice_status, ['authorized', 'processing'])): ?>
                    <button type="button" class="button button-small button-small-danger cloudxm-nfe-cancel"
                            data-order-id="<?php echo esc_attr($order_id); ?>"
                            data-nonce="<?php echo esc_attr($nonce); ?>">
                        <?php esc_html_e('Cancel NFe', 'woocommerce-cloudxm-nfe'); ?>
                    </button>
                <?php endif; ?>
            </div>
        </div>
    <?php else: ?>
        <!-- No NFe Issued -->
        <div class="cloudxm-nfe-no-invoice">
            <p data-testid="cloudxm-nfe-no-invoice"><?php esc_html_e('No NFe issued for this order yet.', 'woocommerce-cloudxm-nfe'); ?></p>
            
            <?php 
            // Check if order status allows NFe issuance
            $order = wc_get_order($order_id);
            $terminal_statuses = ['cancelled', 'refunded', 'failed'];
            if ($order && !in_array($order->get_status(), $terminal_statuses)): 
            ?>
                <button type="button" class="button button-primary cloudxm-nfe-issue"
                        data-order-id="<?php echo esc_attr($order_id); ?>"
                        data-nonce="<?php echo esc_attr($nonce); ?>">
                    <?php esc_html_e('Issue NFe', 'woocommerce-cloudxm-nfe'); ?>
                </button>
            <?php else: ?>
                <p class="description">
                    <?php esc_html_e('NFe cannot be issued for cancelled, refunded, or failed orders.', 'woocommerce-cloudxm-nfe'); ?>
                </p>
            <?php endif; ?>
        </div>
    <?php endif; ?>
    
    <?php if ($error_message): ?>
        <div class="notice notice-error inline" style="margin-top:10px;">
            <p><?php echo esc_html($error_message); ?></p>
        </div>
    <?php endif; ?>
</div>
