<?php
// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}

// Get current user information
$current_user = wp_get_current_user();
$user_id = get_current_user_id();
$first_name = get_user_meta($user_id, 'first_name', true);
$last_name = get_user_meta($user_id, 'last_name', true);
$full_name = trim($first_name . ' ' . $last_name);
if (empty($full_name)) {
    $full_name = $current_user->display_name;
}
$avatar_url = get_avatar_url($user_id, array('size' => 32));

// Get WooCommerce order statistics
$order_stats = array(
    'processing' => 0,
    'completed' => 0,
    'refunded' => 0,
    'cancelled' => 0,
    'total' => 0
);

if (function_exists('wc_get_orders')) {
    $statuses = array('processing', 'completed', 'refunded', 'cancelled');
    foreach ($statuses as $status) {
        $orders = wc_get_orders(array(
            'customer' => $user_id,
            'status' => $status,
            'limit' => -1
        ));
        $order_stats[$status] = count($orders);
        $order_stats['total'] += count($orders);
    }
}

// Get user orders for the table
$customer_orders = array();
if (function_exists('wc_get_orders') && is_user_logged_in()) {
    $customer_orders = wc_get_orders(array(
        'customer' => $user_id,
        'limit' => 20,
        'orderby' => 'date',
        'order' => 'DESC',
    ));
}
?>

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

    <!-- Start Table -->
    <div class="table-responsive">
        <table class="table dashboard-kaveh-table">
                        <thead>
                            <tr>
                                <th>
                                    <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="none" viewBox="0 0 20 20">
                                        <path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M9.167 16.25H17.5"/>
                                        <path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-opacity=".24" stroke-width="1.5" d="M16.667 10.417h.833m-8.333 0h4.575"/>
                                        <path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M9.167 4.583H17.5m-15 0 .833.834 2.5-2.5"/>
                                        <path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-opacity=".24" stroke-width="1.5" d="m2.5 10.417.833.833 2.5-2.5"/>
                                        <path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="m2.5 16.25.833.833 2.5-2.5"/>
                                    </svg>
                                    <?php _e('Order', 'kavehTheme'); ?>
                                </th>
                                <th>
                                    <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="none" viewBox="0 0 20 20">
                                        <path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" stroke-width="1.5" d="M6.667 1.667v2.5m6.666-2.5v2.5M2.917 7.575h14.166"/>
                                        <path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" stroke-opacity=".24" stroke-width="1.5" d="M2.5 10.842V7.083c0-2.5 1.25-4.166 4.167-4.166h6.666c2.917 0 4.167 1.666 4.167 4.166v7.084c0 2.5-1.25 4.166-4.167 4.166H6.667c-2.917 0-4.167-1.666-4.167-4.166"/>
                                        <path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M13.079 11.417h.007m-.007 2.5h.007m-3.09-2.5h.008m-.008 2.5h.008m-3.092-2.5h.007m-.007 2.5h.007"/>
                                    </svg>
                                    <?php _e('Date', 'kavehTheme'); ?>
                                </th>
                                <th>
                                    <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="none" viewBox="0 0 20 20">
                                        <path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-opacity=".25" stroke-width="1.5" d="M10 11.667h.833c.917 0 1.667-.75 1.667-1.667V1.667H5a3.33 3.33 0 0 0-2.908 1.708"/>
                                        <path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-opacity=".24" stroke-width="1.5" d="M1.667 14.167c0 1.383 1.116 2.5 2.5 2.5H5C5 15.75 5.75 15 6.667 15c.916 0 1.666.75 1.666 1.667h3.334c0-.917.75-1.667 1.666-1.667.917 0 1.667.75 1.667 1.667h.833c1.384 0 2.5-1.117 2.5-2.5v-2.5h-2.5a.836.836 0 0 1-.833-.834v-2.5c0-.458.375-.833.833-.833h1.075l-1.425-2.492a1.68 1.68 0 0 0-1.45-.841H12.5V10c0 .917-.75 1.667-1.667 1.667H10"/>
                                        <path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M6.667 18.333a1.667 1.667 0 1 0 0-3.333 1.667 1.667 0 0 0 0 3.333Zm6.666 0a1.667 1.667 0 1 0 0-3.333 1.667 1.667 0 0 0 0 3.333Zm5-8.333v1.667h-2.5a.836.836 0 0 1-.833-.834v-2.5c0-.458.375-.833.833-.833h1.075l1.425 2.5ZM1.667 6.667h5m-5 2.5H5m-3.333 2.5h1.666"/>
                                    </svg>
                                    <?php _e('Status', 'kavehTheme'); ?>
                                </th>
                                <th>
                                    <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="none" viewBox="0 0 20 20">
                                        <path stroke="currentColor" stroke-linecap="round" stroke-opacity=".24" stroke-width="1.5" d="M8.333 5.833H17.5"/>
                                        <path stroke="currentColor" stroke-linecap="round" stroke-width="1.5" d="M2.5 5.833H5"/>
                                        <path stroke="currentColor" stroke-linecap="round" stroke-opacity=".24" stroke-width="1.5" d="M5 10h10"/>
                                        <path stroke="currentColor" stroke-linecap="round" stroke-width="1.5" d="M8.333 14.167h3.334"/>
                                    </svg>
                                    <?php _e('Quantity', 'kavehTheme'); ?>
                                </th>
                                <th>
                                    <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="none" viewBox="0 0 20 20">
                                        <path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M11.8 8.333h.492m-4.584 0h2"/>
                                        <path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-opacity=".24" stroke-width="1.5" d="M2.917 9.167v6.083c0 2.217 1.216 2.742 2.691 1.158l.009-.008c.683-.725 1.725-.667 2.316.125l.842 1.125c.675.892 1.767.892 2.442 0l.841-1.125c.6-.8 1.642-.858 2.325-.125 1.484 1.583 2.692 1.058 2.692-1.158V5.867c0-3.359-.783-4.2-3.933-4.2h-6.3c-3.15 0-3.934.841-3.934 4.2"/>
                                    </svg>
                                    <?php _e('Total', 'kavehTheme'); ?>
                                </th>
                                <th>
                                    <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="none" viewBox="0 0 20 20">
                                        <path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" stroke-width="1.5" d="M7.342 1.667 4.325 4.692m8.333-3.025 3.017 3.025"/>
                                        <path stroke="currentColor" stroke-opacity=".24" stroke-width="1.5" d="M1.667 6.542c0-1.542.825-1.667 1.85-1.667h12.966c1.025 0 1.85.125 1.85 1.667 0 1.791-.825 1.666-1.85 1.666H3.517c-1.025 0-1.85.125-1.85-1.666Z"/>
                                        <path stroke="currentColor" stroke-linecap="round" stroke-width="1.5" d="M8.133 11.667v2.958m3.834-2.958v2.958m-7.875.908c.266 1.617.908 2.8 3.291 2.8h5.025c2.592 0 2.975-1.133 3.275-2.7l1.4-7.3m-14.166 0 .6 3.65"/>
                                    </svg>
                                    <?php _e('Actions', 'kavehTheme'); ?>
                                </th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if (!empty($customer_orders)): ?>
                                <?php foreach ($customer_orders as $order): 
                                    $order_id = $order->get_id();
                                    $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, '.', ',');
                                    
                                    // 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;
                                    
                                    // Status badge class
                                    $status_class = '';
                                    switch ($order_status) {
                                        case 'completed':
                                            $status_class = 'badge-success';
                                            break;
                                        case 'processing':
                                            $status_class = 'badge-warning';
                                            break;
                                        case 'cancelled':
                                        case 'failed':
                                            $status_class = 'badge-danger';
                                            break;
                                        case 'refunded':
                                            $status_class = 'badge-info';
                                            break;
                                        default:
                                            $status_class = 'badge-secondary';
                                    }
                                ?>
                                <tr>
                                    <td class="code">#<?php echo esc_html($order_id); ?></td>
                                    <td class="date"><?php echo esc_html($order_date_formatted); ?></td>
                                    <td class="status"><?php echo esc_html($status_label); ?></td>
                                    <td class="quantity"><?php echo esc_html($order_item_count); ?></td>
                                    <td class="price">
                                        <span><?php echo esc_html($formatted_total); ?></span>
                                        <?php _e('Toman', 'kavehTheme'); ?>
                                    </td>
                                    <td class="btns">
                                        <div class="d-flex align-items-center justify-content-end gap-2">
                                            <?php if ($order_status === 'pending'): ?>
                                            <a href="<?php echo esc_url($order->get_checkout_payment_url()); ?>" class="btn btn-order btn-orange-2 d-flex align-items-center justify-content-center gap-1">
                                                <i class="icon-transaction-minus lh-1"></i>
                                                <?php _e('Pay', 'kavehTheme'); ?>
                                            </a>
                                            <?php endif; ?>
                                            
                                            <?php if (in_array($order_status, array('completed', 'processing'))): ?>
                                            <a href="<?php echo esc_url(get_permalink() . 'order-details/' . $order->get_id() . '/'); ?>" 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 else: ?>
                                            <div class="not-factor fw-normal"><?php _e('This product has no invoice', 'kavehTheme'); ?></div>
                                            <?php endif; ?>
                                            
                                            <a href="<?php echo esc_url(get_permalink() . 'order-details/' . $order->get_id() . '/'); ?>" class="btn btn-view btn-gradient-blue-2 d-flex align-items-center justify-content-center position-relative">
                                                <?php _e('View', 'kavehTheme'); ?>
                                                <i class="icon-arrow-left-9 lh-1"></i>
                                            </a>
                                        </div>
                                    </td>
                                </tr>
                                <?php endforeach; ?>
                            <?php else: ?>
                                <tr>
                                    <td colspan="6" class="no-orders text-center py-4">
                                        <div class="empty-state d-flex flex-column align-items-center gap-3">
                                            <i class="icon-bag-4 lh-1" style="font-size: 48px; opacity: 0.3;"></i>
                                            <span class="fw-normal"><?php _e('No orders found', 'kavehTheme'); ?></span>
                                            <a href="<?php echo esc_url(wc_get_page_permalink('shop')); ?>" class="btn btn-primary">
                                                <?php _e('Start Shopping', 'kavehTheme'); ?>
                                            </a>
                                        </div>
                                    </td>
                                </tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
                <!-- End Table -->
            </div>
        </div>
        <!-- End Orders Table -->
    </div>
</div>
<!-- End Content -->