<?php
/**
 * Template: [titancart_my_orders] — Customer order history list.
 *
 * Available variables:
 *   $orders       — Array of order rows (each includes status_handle, status_label, status_color)
 *   $total        — Total order count for this customer
 *   $pages        — Total page count
 *   $current_page — Current page number
 *
 * Override: copy to your-theme/titancart/account/my-orders.php
 *
 * @package TitanCart
 */
defined( 'ABSPATH' ) || exit;

$base_url = get_permalink() ?: home_url( add_query_arg( [] ) );
?>
<div class="tc-my-orders">

    <?php if ( empty( $orders ) ) : ?>
        <div class="tc-empty-state">
            <p><?php esc_html_e( "You haven't placed any orders yet.", 'titancart-lite' ); ?></p>
            <a href="<?php echo esc_url( titancart_shop_url() ); ?>" class="tc-btn tc-btn--primary">
                <?php esc_html_e( 'Start Shopping', 'titancart-lite' ); ?>
            </a>
        </div>
    <?php else : ?>

        <table class="tc-orders-table">
            <thead>
                <tr>
                    <th><?php esc_html_e( 'Order', 'titancart-lite' ); ?></th>
                    <th><?php esc_html_e( 'Date', 'titancart-lite' ); ?></th>
                    <th><?php esc_html_e( 'Status', 'titancart-lite' ); ?></th>
                    <th><?php esc_html_e( 'Total', 'titancart-lite' ); ?></th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ( $orders as $order ) :
                    $view_url = add_query_arg( 'tc_order', $order['order_number'], $base_url );
                    $status_color = $order['status_color'] ?? '#71797E';
                    $status_label = $order['status_label'] ?? $order['status_handle'] ?? '—';
                ?>
                <tr class="tc-order-row">
                    <td class="tc-order-number">
                        <a href="<?php echo esc_url( $view_url ); ?>">
                            <?php echo esc_html( $order['order_number'] ); ?>
                        </a>
                    </td>
                    <td class="tc-order-date">
                        <?php echo esc_html( titancart_format_date( $order['created_at'] ) ); ?>
                    </td>
                    <td class="tc-order-status">
                        <span class="tc-status-badge" style="--tc-status-color:<?php echo esc_attr( $status_color ); ?>;">
                            <?php echo esc_html( $status_label ); ?>
                        </span>
                    </td>
                    <td class="tc-order-total">
                        <?php echo esc_html( number_format( (float) $order['total'], 2 ) . ' ' . strtoupper( $order['currency'] ?? 'USD' ) ); ?>
                    </td>
                    <td class="tc-order-actions">
                        <a href="<?php echo esc_url( $view_url ); ?>" class="tc-btn tc-btn--small tc-btn--outline">
                            <?php esc_html_e( 'View', 'titancart-lite' ); ?>
                        </a>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>

        <?php if ( $pages > 1 ) : ?>
        <nav class="tc-pagination" aria-label="<?php esc_attr_e( 'Order history pages', 'titancart-lite' ); ?>">
            <?php for ( $p = 1; $p <= $pages; $p++ ) :
                $page_url = add_query_arg( 'tc_page', $p, $base_url );
            ?>
                <?php if ( $p === $current_page ) : ?>
                    <span class="tc-page-current" aria-current="page"><?php echo (int) $p; ?></span>
                <?php else : ?>
                    <a class="tc-page-link" href="<?php echo esc_url( $page_url ); ?>"><?php echo (int) $p; ?></a>
                <?php endif; ?>
            <?php endfor; ?>
        </nav>
        <?php endif; ?>

    <?php endif; ?>

</div><!-- .tc-my-orders -->

<?php
// WP.org R2: inline CSS is built as a PHP string (no output buffering, no
// markup literals) and attached via wp_add_inline_style on a stub handle.
$tc_myorders_css = <<<'CSS'
.tc-my-orders { font-family: inherit; max-width: 860px; }
.tc-empty-state { text-align: center; padding: 40px 20px; color: #5A6270; }
.tc-orders-table { width: 100%; border-collapse: collapse; font-size: 14px; }
.tc-orders-table th { background: #F6F8FB; padding: 10px 14px; text-align: left; font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.5px; color: #5A6270; border-bottom: 2px solid #E1E5EA; }
.tc-orders-table td { padding: 14px; border-bottom: 1px solid #F0F2F5; vertical-align: middle; }
.tc-orders-table tr:last-child td { border-bottom: none; }
.tc-order-number a { font-weight: 600; color: #1F6AE1; text-decoration: none; }
.tc-order-number a:hover { text-decoration: underline; }
.tc-status-badge { display: inline-block; padding: 3px 10px; border-radius: 12px; font-size: 11px; font-weight: 600; background: color-mix(in srgb, var(--tc-status-color, #71797E) 12%, transparent); color: var(--tc-status-color, #71797E); }
.tc-pagination { display: flex; gap: 4px; margin-top: 20px; align-items: center; }
.tc-page-link, .tc-page-current { display: inline-flex; align-items: center; justify-content: center; width: 32px; height: 32px; border-radius: 6px; font-size: 13px; text-decoration: none; }
.tc-page-link { border: 1px solid #E1E5EA; color: #5A6270; }
.tc-page-link:hover { background: #F6F8FB; }
.tc-page-current { background: #1F6AE1; color: #fff; font-weight: 600; }
.tc-btn { display: inline-block; text-decoration: none; border-radius: 8px; font-weight: 600; cursor: pointer; font-size: 14px; padding: 10px 20px; }
.tc-btn--primary { background: #1F6AE1; color: #fff; }
.tc-btn--small { font-size: 12px; padding: 5px 14px; }
.tc-btn--outline { border: 1px solid #E1E5EA; color: #5A6270; background: transparent; }
.tc-btn--outline:hover { background: #F6F8FB; }
CSS;
wp_register_style( 'titancart-myorders-inline', false, array(), TITANCART_VERSION );
wp_enqueue_style( 'titancart-myorders-inline' );
wp_add_inline_style( 'titancart-myorders-inline', $tc_myorders_css );
?>
