<?php
/**
 * Template: account/orders.php
 *
 * Full paginated order history for logged-in customers.
 *
 * Available variables:
 *   $customer     — tc_customers row
 *   $items        — Array of order rows
 *   $total        — Total order count
 *   $pages        — Total page count
 *   $current_page — Current page number (int)
 *
 * Override: copy to your-theme/titancart/account/orders.php
 *
 * @package TitanCart
 */
defined( 'ABSPATH' ) || exit;

$base_url = home_url( '/my-account/' );
$back_url = add_query_arg( 'tc_account_page', 'dashboard', $base_url );
?>

<div class="tc-my-orders">

    <div class="tc-page-header">
        <a href="<?php echo esc_url( $back_url ); ?>" class="tc-back-link">← <?php esc_html_e( 'My Account', 'titancart-lite' ); ?></a>
        <h2><?php esc_html_e( 'Order History', 'titancart-lite' ); ?></h2>
    </div>

    <?php if ( empty( $items ) ) : ?>
        <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 ( $items as $order ) :
                    $order_url    = add_query_arg( [ 'tc_account_page' => 'order-detail', 'tc_order' => $order['id'] ], $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-num">
                            <a href="<?php echo esc_url( $order_url ); ?>">
                                <?php echo esc_html( $order['order_number'] ?? '#' . $order['id'] ); ?>
                            </a>
                        </td>
                        <td><?php echo esc_html( titancart_format_date( $order['created_at'] ) ); ?></td>
                        <td>
                            <span class="tc-status-badge" style="--tc-status-color:<?php echo esc_attr( $status_color ); ?>;">
                                <?php echo esc_html( $status_label ); ?>
                            </span>
                        </td>
                        <td><?php echo esc_html( number_format( (float) $order['total'], 2 ) ); ?></td>
                        <td>
                            <a href="<?php echo esc_url( $order_url ); ?>" class="tc-btn tc-btn--sm">
                                <?php esc_html_e( 'View', 'titancart-lite' ); ?>
                            </a>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>

        <?php if ( isset( $pages ) && $pages > 1 ) : ?>
            <nav class="tc-pagination" aria-label="<?php esc_attr_e( 'Orders pagination', 'titancart-lite' ); ?>">
                <?php
                $paged_base = add_query_arg( 'tc_account_page', 'orders', $base_url );
                for ( $p = 1; $p <= $pages; $p++ ) :
                    $page_url    = add_query_arg( 'paged', $p, $paged_base );
                    $active_cls  = $p === $current_page ? ' tc-pagination__btn--active' : '';
                ?>
                    <a href="<?php echo esc_url( $page_url ); ?>"
                       class="tc-pagination__btn<?php echo esc_attr( $active_cls ); ?>">
                        <?php echo esc_html( $p ); ?>
                    </a>
                <?php endfor; ?>
            </nav>
        <?php endif; ?>

    <?php endif; ?>

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