<?php require KAVEHBOARD_PATH . 'template-part/header.php'; ?>
<?php
/**
 * Order Details Template
 * Displays detailed information about a specific order
 */

// Security check
if (!defined('ABSPATH')) {
    exit;
}

// Get current user
$current_user = wp_get_current_user();
$user_id = $current_user->ID;

// Get order ID from URL parameter
$order_id = get_query_var('kaveh_action');
if (empty($order_id)) {
    $order_id = isset($_GET['order_id']) ? intval($_GET['order_id']) : 0;
}
$order_id = intval($order_id);

if (empty($order_id)) {
    wp_redirect(home_url('/dashboard/orders/'));
    exit;
}

// Get the order
$order = wc_get_order($order_id);

// Check if order exists and belongs to current user
if (!$order || $order->get_customer_id() != $user_id) {
    wp_redirect(home_url('/dashboard/orders/'));
    exit;
}

// Get order details
$order_date = $order->get_date_created();
$order_date_formatted = $order_date ? $order_date->date_i18n('Y/m/d') : '';
$order_total = $order->get_total();
$order_status = $order->get_status();
$order_item_count = $order->get_item_count();
$formatted_total = number_format($order_total, 0, '.', ',');

// Get billing details
$billing_first_name = $order->get_billing_first_name();
$billing_last_name = $order->get_billing_last_name();
$billing_email = $order->get_billing_email();
$billing_phone = $order->get_billing_phone();
$billing_address = $order->get_formatted_billing_address();

// Status translation
$status_labels = array(
    'pending' => __('Pending Payment', 'kavehTheme'),
    'processing' => __('Processing', 'kavehTheme'),
    'on-hold' => __('On Hold', 'kavehTheme'),
    'completed' => __('Completed', 'kavehTheme'),
    'cancelled' => __('Cancelled', 'kavehTheme'),
    'refunded' => __('Refunded', 'kavehTheme'),
    'failed' => __('Failed', 'kavehTheme')
);
$status_label = isset($status_labels[$order_status]) ? $status_labels[$order_status] : $order_status;

// Get order items
$order_items = $order->get_items();

// Calculate totals
$subtotal = $order->get_subtotal();
$discount = $order->get_discount_total();
$shipping = $order->get_shipping_total();
$tax = $order->get_total_tax();

// Format prices
$formatted_subtotal = number_format($subtotal, 0, '.', ',');
$formatted_discount = number_format($discount, 0, '.', ',');
$formatted_shipping = number_format($shipping, 0, '.', ',');
$formatted_tax = number_format($tax, 0, '.', ',');

// Generate tracking code (you can customize this logic)
$tracking_code = str_pad($order_id, 8, '0', STR_PAD_LEFT);
?>

<!-- Start Content -->
<div class="dashboard-kaveh-content">

    
    <div class="dashboard-kaveh-order">
        <!-- Start Info Order -->
        <div class="dashboard-kaveh-order-info d-flex align-items-center justify-content-lg-between gap-3 gap-lg-2 flex-column flex-lg-row">
            <div class="dashboard-kaveh-order-info-text">
                <?php _e('Order', 'kavehTheme'); ?>
                <span> #<?php echo esc_html($order_id); ?> </span>
                <?php _e('on date', 'kavehTheme'); ?>
                <span> <?php echo esc_html($order_date_formatted); ?> </span>
                <?php _e('was registered and is now', 'kavehTheme'); ?> <?php echo esc_html($status_label); ?>
            </div>
            <div class="d-flex align-items-center gap-2">
                <?php if (in_array($order_status, array('completed', 'processing'))): ?>
                <a href="<?php echo esc_url($order->get_checkout_order_received_url()); ?>" class="btn btn-factor btn-green d-flex align-items-center justify-content-center gap-1">
                    <i class="icon-note-2 lh-1"></i>
                    <?php _e('Invoice', 'kavehTheme'); ?>
                </a>
                <?php endif; ?>
                <div class="code fw-normal"><?php _e('Order tracking code:', 'kavehTheme'); ?> <span> <?php echo esc_html($tracking_code); ?> </span> </div>
            </div>
        </div>
        <!-- End Info Order -->
        
        <div class="row">
            <!-- Start Info User -->
            <div class="col-lg-7">
                <div class="dashboard-kaveh-order-info-user position-relative h-100">
                    <div class="dashboard-kaveh-order-info-user-heading d-flex align-items-center justify-content-between gap-2">
                        <div class="title"><?php _e('Order Details:', 'kavehTheme'); ?></div>
                        <div class="email"><?php _e('Email Address:', 'kavehTheme'); ?> <?php echo esc_html($billing_email); ?></div>
                    </div>
                    <ul class="info d-flex flex-wrap">
                        <li>
                            <div class="d-flex align-items-center">
                                <i class="icon-user-5 lh-1"></i>
                                <div class="text fw-normal">
                                    <?php _e('Name:', 'kavehTheme'); ?>
                                    <span class="fw-bold"> <?php echo esc_html($billing_first_name . ' ' . $billing_last_name); ?> </span>
                                </div>
                            </div>
                        </li>
                        <li>
                            <div class="d-flex align-items-center">
                                <i class="icon-call-calling lh-1"></i>
                                <div class="text fw-normal">
                                    <?php _e('Phone:', 'kavehTheme'); ?>
                                    <span class="fw-bold"> <?php echo esc_html($billing_phone); ?> </span>
                                </div>
                            </div>
                        </li>
                        <li>
                            <div class="d-flex align-items-center">
                                <i class="icon-fast-car lh-1"></i>
                                <div class="text fw-normal">
                                    <?php _e('Address:', 'kavehTheme'); ?>
                                    <span class="fw-bold"> <?php echo wp_kses_post($billing_address); ?> </span>
                                </div>
                            </div>
                        </li>
                    </ul>
                </div>
            </div>
            <!-- End Info User -->
            
            <!-- Start Total Price -->
            <div class="col-lg-5">
                <div class="dashboard-kaveh-order-total-price">
                    <div class="dashboard-kaveh-order-total-price-heading d-flex align-items-center justify-content-between gap-2">
                        <div class="title d-flex align-items-center text-end fw-bold">
                            <i class="icon-box-remove lh-1 fw-normal"></i>
                            <?php _e('Order Status', 'kavehTheme'); ?>
                        </div>
                        <div class="text position-relative fw-bold"> <?php echo esc_html($status_label); ?> </div>
                    </div>
                    <ul class="d-flex flex-column">
                        <li class="d-flex align-items-center justify-content-between gap-2">
                            <?php _e('Subtotal', 'kavehTheme'); ?>
                            <span>
                                <span> <?php echo esc_html($formatted_subtotal); ?> </span>
                                <?php _e('Toman', 'kavehTheme'); ?>
                            </span>
                        </li>
                        <?php if ($discount > 0): ?>
                        <li class="d-flex align-items-center justify-content-between gap-2">
                            <?php _e('Discount', 'kavehTheme'); ?>
                            <span>
                                <span> <?php echo esc_html($formatted_discount); ?> </span>
                                <?php _e('Toman', 'kavehTheme'); ?>
                            </span>
                        </li>
                        <?php endif; ?>
                        <?php if ($shipping > 0): ?>
                        <li class="d-flex align-items-center justify-content-between gap-2">
                            <?php _e('Shipping Cost', 'kavehTheme'); ?>
                            <span>
                                <span> <?php echo esc_html($formatted_shipping); ?> </span>
                                <?php _e('Toman', 'kavehTheme'); ?>
                            </span>
                        </li>
                        <?php endif; ?>
                        <li class="d-flex align-items-center justify-content-between gap-2">
                            <?php _e('Final Price', 'kavehTheme'); ?>
                            <span>
                                <span> <?php echo esc_html($formatted_total); ?> </span>
                                <?php _e('Toman', 'kavehTheme'); ?>
                            </span>
                        </li>
                    </ul>
                </div>
            </div>
            <!-- End Total Price -->
        </div>
        
        <!-- Start Products -->
        <ul class="dashboard-kaveh-order-products mt-4 d-flex flex-column">
            <?php foreach ($order_items as $item_id => $item): 
                $product = $item->get_product();
                $product_name = $item->get_name();
                $quantity = $item->get_quantity();
                $item_total = $item->get_total();
                $formatted_item_total = number_format($item_total, 0, '.', ',');
                
                // Get product image
                $product_image = '';
                if ($product) {
                    $image_id = $product->get_image_id();
                    if ($image_id) {
                        $product_image = wp_get_attachment_image_url($image_id, 'thumbnail');
                    }
                }
                
                // Fallback image
                if (empty($product_image)) {
                    $product_image = wc_placeholder_img_src('thumbnail');
                }
                
                // Get product URL
                $product_url = $product ? get_permalink($product->get_id()) : '#';
            ?>
            <li class="d-flex align-items-center flex-wrap flex-md-nowrap">
                <img src="<?php echo esc_url($product_image); ?>" alt="<?php echo esc_attr($product_name); ?>" width="60" height="60" class="d-block flex-fill flex-grow-0 flex-shrink-0">
                <div class="title overflow-hidden w-100"> <?php echo esc_html($product_name); ?> </div>
                <span class="count fw-normal f-iransans flex-fill flex-grow-0 flex-shrink-0"><?php _e('Quantity:', 'kavehTheme'); ?> <?php echo esc_html($quantity); ?></span>
                <div class="price ms-auto d-flex align-items-center flex-fill flex-grow-0 flex-shrink-0">
                    <div>
                        <div class="price fw-normal text-end me-0">
                            <span> <?php echo esc_html($formatted_item_total); ?> </span>
                            <?php _e('Toman', 'kavehTheme'); ?>
                        </div>
                    </div>
                </div>
                <a href="<?php echo esc_url($product_url); ?>" class="view d-flex align-items-center justify-content-center f-iransans flex-fill flex-grow-0 flex-shrink-0">
                    <?php _e('View Product', 'kavehTheme'); ?>
                    <i class="icon-arrow-left-9 lh-1"></i>
                </a>
            </li>
            <?php endforeach; ?>
        </ul>
        <!-- End Products -->
    </div>
</div>
<!-- End Content -->
<?php require KAVEHBOARD_PATH . 'template-part/footer.php'; ?>