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

// $order variable is passed from the calling function
?>
<div class="wrap">
    <h1><?php _e('Order Details', 'wp-digital-download'); ?></h1>
    
    <div class="order-details-container" style="max-width: 800px;">
        
        <div class="order-summary" style="background: #fff; border: 1px solid #ccd0d4; padding: 20px; margin-bottom: 20px;">
            <h2><?php _e('Order Summary', 'wp-digital-download'); ?></h2>
            
            <table class="widefat fixed">
                <tbody>
                    <tr>
                        <td style="width: 200px;"><strong><?php _e('Order Number', 'wp-digital-download'); ?></strong></td>
                        <td><?php echo esc_html($order->order_number); ?></td>
                    </tr>
                    <tr>
                        <td><strong><?php _e('Status', 'wp-digital-download'); ?></strong></td>
                        <td>
                            <span class="order-status status-<?php echo esc_attr($order->status); ?>" style="padding: 4px 8px; border-radius: 3px; font-size: 12px; <?php 
                                echo $order->status === 'completed' ? 'background: #d1e7dd; color: #0f5132;' : 
                                    ($order->status === 'pending' ? 'background: #fff3cd; color: #856404;' : 
                                    'background: #f8d7da; color: #721c24;'); 
                            ?>">
                                <?php echo esc_html(ucfirst($order->status)); ?>
                            </span>
                        </td>
                    </tr>
                    <tr>
                        <td><strong><?php _e('Purchase Date', 'wp-digital-download'); ?></strong></td>
                        <td><?php echo esc_html(date_i18n(get_option('date_format') . ' ' . get_option('time_format'), strtotime($order->purchase_date))); ?></td>
                    </tr>
                    <tr>
                        <td><strong><?php _e('Payment Method', 'wp-digital-download'); ?></strong></td>
                        <td>
                            <?php 
                            $payment_methods = array(
                                'paypal' => 'PayPal',
                                'free' => 'Free Download',
                                'manual' => 'Manual Payment'
                            );
                            echo esc_html($payment_methods[$order->payment_method] ?? ucfirst($order->payment_method));
                            ?>
                        </td>
                    </tr>
                    <?php if (!empty($order->transaction_id)) : ?>
                    <tr>
                        <td><strong><?php _e('Transaction ID', 'wp-digital-download'); ?></strong></td>
                        <td><code><?php echo esc_html($order->transaction_id); ?></code></td>
                    </tr>
                    <?php endif; ?>
                    <tr>
                        <td><strong><?php _e('Amount', 'wp-digital-download'); ?></strong></td>
                        <td>
                            <strong style="font-size: 16px;">
                                <?php echo wpdd_format_price($order->amount, $order->currency); ?>
                            </strong>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
        
        <div class="customer-details" style="background: #fff; border: 1px solid #ccd0d4; padding: 20px; margin-bottom: 20px;">
            <h2><?php _e('Customer Information', 'wp-digital-download'); ?></h2>
            
            <table class="widefat fixed">
                <tbody>
                    <tr>
                        <td style="width: 200px;"><strong><?php _e('Customer Name', 'wp-digital-download'); ?></strong></td>
                        <td><?php echo esc_html($order->customer_name); ?></td>
                    </tr>
                    <tr>
                        <td><strong><?php _e('Email Address', 'wp-digital-download'); ?></strong></td>
                        <td>
                            <a href="mailto:<?php echo esc_attr($order->customer_email); ?>">
                                <?php echo esc_html($order->customer_email); ?>
                            </a>
                        </td>
                    </tr>
                    <?php if ($order->customer_id > 0) : ?>
                    <tr>
                        <td><strong><?php _e('WordPress User', 'wp-digital-download'); ?></strong></td>
                        <td>
                            <a href="<?php echo admin_url('user-edit.php?user_id=' . $order->customer_id); ?>">
                                <?php _e('View User Profile', 'wp-digital-download'); ?>
                            </a>
                        </td>
                    </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
        
        <div class="product-details" style="background: #fff; border: 1px solid #ccd0d4; padding: 20px; margin-bottom: 20px;">
            <h2><?php _e('Product Information', 'wp-digital-download'); ?></h2>
            
            <table class="widefat fixed">
                <tbody>
                    <tr>
                        <td style="width: 200px;"><strong><?php _e('Product', 'wp-digital-download'); ?></strong></td>
                        <td>
                            <a href="<?php echo admin_url('post.php?post=' . $order->product_id . '&action=edit'); ?>">
                                <?php echo esc_html($order->product_name); ?>
                            </a>
                        </td>
                    </tr>
                    <tr>
                        <td><strong><?php _e('Product ID', 'wp-digital-download'); ?></strong></td>
                        <td><?php echo esc_html($order->product_id); ?></td>
                    </tr>
                    <?php if ($order->creator_id > 0) : ?>
                    <tr>
                        <td><strong><?php _e('Creator', 'wp-digital-download'); ?></strong></td>
                        <td>
                            <?php 
                            $creator = get_userdata($order->creator_id);
                            if ($creator) : ?>
                                <a href="<?php echo admin_url('user-edit.php?user_id=' . $order->creator_id); ?>">
                                    <?php echo esc_html($creator->display_name); ?>
                                </a>
                            <?php else : ?>
                                <?php _e('Creator not found', 'wp-digital-download'); ?>
                            <?php endif; ?>
                        </td>
                    </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
        
        <?php
        // Get download links for this order
        global $wpdb;
        $download_links = $wpdb->get_results($wpdb->prepare(
            "SELECT * FROM {$wpdb->prefix}wpdd_download_links WHERE order_id = %d ORDER BY created_at DESC",
            $order->id
        ));
        ?>
        
        <?php if (!empty($download_links)) : ?>
        <div class="download-links" style="background: #fff; border: 1px solid #ccd0d4; padding: 20px; margin-bottom: 20px;">
            <h2><?php _e('Download Links', 'wp-digital-download'); ?></h2>
            
            <table class="wp-list-table widefat fixed striped">
                <thead>
                    <tr>
                        <th><?php _e('Download Token', 'wp-digital-download'); ?></th>
                        <th><?php _e('Downloads', 'wp-digital-download'); ?></th>
                        <th><?php _e('Expires', 'wp-digital-download'); ?></th>
                        <th><?php _e('Created', 'wp-digital-download'); ?></th>
                        <th><?php _e('Status', 'wp-digital-download'); ?></th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($download_links as $link) : 
                        $is_expired = strtotime($link->expires_at) < current_time('timestamp');
                        $is_used_up = $link->download_count >= $link->max_downloads;
                    ?>
                    <tr>
                        <td>
                            <code style="font-size: 11px;"><?php echo esc_html(substr($link->token, 0, 20)) . '...'; ?></code>
                        </td>
                        <td>
                            <?php echo esc_html($link->download_count); ?> / <?php echo esc_html($link->max_downloads); ?>
                        </td>
                        <td>
                            <?php 
                            if ($is_expired) : ?>
                                <span style="color: #d63384;"><?php _e('Expired', 'wp-digital-download'); ?></span>
                            <?php else : ?>
                                <?php echo esc_html(date_i18n(get_option('date_format'), strtotime($link->expires_at))); ?>
                            <?php endif; ?>
                        </td>
                        <td><?php echo esc_html(date_i18n(get_option('date_format'), strtotime($link->created_at))); ?></td>
                        <td>
                            <?php if ($is_expired) : ?>
                                <span style="color: #d63384;"><?php _e('Expired', 'wp-digital-download'); ?></span>
                            <?php elseif ($is_used_up) : ?>
                                <span style="color: #fd7e14;"><?php _e('Used Up', 'wp-digital-download'); ?></span>
                            <?php else : ?>
                                <span style="color: #198754;"><?php _e('Active', 'wp-digital-download'); ?></span>
                            <?php endif; ?>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
        <?php endif; ?>
        
        <?php
        // Get download history for this order
        $downloads = $wpdb->get_results($wpdb->prepare(
            "SELECT * FROM {$wpdb->prefix}wpdd_downloads WHERE order_id = %d ORDER BY download_date DESC LIMIT 20",
            $order->id
        ));
        ?>
        
        <?php if (!empty($downloads)) : ?>
        <div class="download-history" style="background: #fff; border: 1px solid #ccd0d4; padding: 20px; margin-bottom: 20px;">
            <h2><?php _e('Download History', 'wp-digital-download'); ?> 
                <small>(<?php printf(__('Last %d downloads', 'wp-digital-download'), count($downloads)); ?>)</small>
            </h2>
            
            <table class="wp-list-table widefat fixed striped">
                <thead>
                    <tr>
                        <th><?php _e('Date', 'wp-digital-download'); ?></th>
                        <th><?php _e('IP Address', 'wp-digital-download'); ?></th>
                        <th><?php _e('User Agent', 'wp-digital-download'); ?></th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($downloads as $download) : ?>
                    <tr>
                        <td><?php echo esc_html(date_i18n(get_option('date_format') . ' ' . get_option('time_format'), strtotime($download->download_date))); ?></td>
                        <td><code><?php echo esc_html($download->ip_address); ?></code></td>
                        <td style="font-size: 11px;">
                            <?php echo esc_html(wp_trim_words($download->user_agent, 10)); ?>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
        <?php endif; ?>
        
        <div class="order-actions" style="margin-top: 30px;">
            <a href="<?php echo admin_url('edit.php?post_type=wpdd_product&page=wpdd-orders'); ?>" class="button">
                <?php _e('← Back to Orders', 'wp-digital-download'); ?>
            </a>
            
            <?php if ($order->status === 'completed') : ?>
            <a href="mailto:<?php echo esc_attr($order->customer_email); ?>?subject=<?php echo urlencode('Your Order: ' . $order->order_number); ?>" class="button button-secondary">
                <?php _e('Email Customer', 'wp-digital-download'); ?>
            </a>
            <?php endif; ?>
        </div>
        
    </div>
</div>

<style>
.order-details-container table.widefat td {
    padding: 12px;
    border-bottom: 1px solid #f0f0f1;
}
.order-details-container table.widefat td:first-child {
    background-color: #f6f7f7;
}
.order-status {
    font-weight: bold;
    text-transform: uppercase;
}
</style>