<?php
/**
 * Email template: New Order — Admin Notification.
 *
 * Sent to the store admin immediately after a new order is placed.
 *
 * Available variables (via Mailer::render()):
 *   $order        — Full order row array (includes status_handle, status_label, etc.)
 *   $items        — Array of order line items with $item['options']
 *   $site_name    — Blog name
 *   $site_url     — Home URL
 *   $mailer       — Mailer instance (for format_money())
 *
 * @package TitanCart
 */
defined( 'ABSPATH' ) || exit;

$admin_url = admin_url( 'admin.php?page=titancart#/orders/' . (int) $order['id'] );
$currency  = $order['currency'] ?? 'USD';
?>

<h1 style="margin-bottom:4px;"><?php esc_html_e( '🛒 New Order Received', 'titancart-lite' ); ?></h1>
<p style="color:#5A6270;font-size:13px;margin-bottom:24px;">
    <?php
    printf(
        /* translators: %s: order number */
        esc_html__( 'A new order has been placed on your store. Order %s is awaiting your review.', 'titancart-lite' ),
        '<strong>' . esc_html( $order['order_number'] ) . '</strong>'
    );
    ?>
</p>

<!-- CTA button -->
<p style="text-align:center;margin-bottom:32px;">
    <a href="<?php echo esc_url( $admin_url ); ?>" class="btn">
        <?php esc_html_e( 'View Order in Admin', 'titancart-lite' ); ?>
    </a>
</p>

<hr class="divider">

<!-- Order summary -->
<h2><?php esc_html_e( 'Order Summary', 'titancart-lite' ); ?></h2>

<table class="order-table">
    <thead>
        <tr>
            <th><?php esc_html_e( 'Product', 'titancart-lite' ); ?></th>
            <th style="text-align:center;"><?php esc_html_e( 'Qty', 'titancart-lite' ); ?></th>
            <th style="text-align:right;"><?php esc_html_e( 'Total', 'titancart-lite' ); ?></th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ( $items as $item ) :
            $line_total = isset( $item['total'] ) ? $item['total'] : ( $item['line_total'] ?? 0 );
        ?>
        <tr>
            <td>
                <strong><?php echo esc_html( $item['name'] ); ?></strong>
                <?php if ( ! empty( $item['sku'] ) ) : ?>
                    <br><span style="font-size:11px;color:#5A6270;">SKU: <?php echo esc_html( $item['sku'] ); ?></span>
                <?php endif; ?>
                <?php if ( ! empty( $item['options'] ) ) : ?>
                    <br><span style="font-size:11px;color:#5A6270;">
                        <?php echo esc_html( implode( ', ', array_map( fn( $o ) => $o['option_name'] . ': ' . $o['variant_name'], $item['options'] ) ) ); ?>
                    </span>
                <?php endif; ?>
            </td>
            <td style="text-align:center;"><?php echo (int) $item['quantity']; ?></td>
            <td style="text-align:right;"><?php echo esc_html( $mailer->format_money( $line_total, $currency ) ); ?></td>
        </tr>
        <?php endforeach; ?>
    </tbody>
</table>

<!-- Totals -->
<table class="totals-table" style="max-width:280px;margin-left:auto;">
    <?php if ( (float) ( $order['discount'] ?? 0 ) > 0 ) : ?>
    <tr>
        <td><?php esc_html_e( 'Discount', 'titancart-lite' ); ?></td>
        <td>−<?php echo esc_html( $mailer->format_money( $order['discount'], $currency ) ); ?></td>
    </tr>
    <?php endif; ?>
    <tr>
        <td><?php esc_html_e( 'Shipping', 'titancart-lite' ); ?></td>
        <td><?php echo esc_html( $mailer->format_money( $order['shipping_cost'] ?? 0, $currency ) ); ?></td>
    </tr>
    <?php if ( (float) ( $order['tax_total'] ?? 0 ) > 0 ) : ?>
    <tr>
        <td><?php esc_html_e( 'Tax', 'titancart-lite' ); ?></td>
        <td><?php echo esc_html( $mailer->format_money( $order['tax_total'], $currency ) ); ?></td>
    </tr>
    <?php endif; ?>
    <?php if ( (float) ( $order['tip_total'] ?? 0 ) > 0 ) : ?>
    <tr>
        <td><?php echo esc_html( apply_filters( 'titancart_tip_line_label', __( 'Tip', 'titancart-lite' ), $order ) ); ?></td>
        <td><?php echo esc_html( $mailer->format_money( $order['tip_total'], $currency ) ); ?></td>
    </tr>
    <?php endif; ?>
    <tr class="totals-total">
        <td><strong><?php esc_html_e( 'Order Total', 'titancart-lite' ); ?></strong></td>
        <td><strong><?php echo esc_html( $mailer->format_money( $order['total'], $currency ) ); ?></strong></td>
    </tr>
</table>

<hr class="divider">

<!-- Customer + Address -->
<h2><?php esc_html_e( 'Customer Details', 'titancart-lite' ); ?></h2>

<p>
    <strong><?php esc_html_e( 'Name:', 'titancart-lite' ); ?></strong>
    <?php echo esc_html( trim( $order['billing_first_name'] . ' ' . $order['billing_last_name'] ) ); ?><br>
    <strong><?php esc_html_e( 'Email:', 'titancart-lite' ); ?></strong>
    <a href="mailto:<?php echo esc_attr( $order['billing_email'] ); ?>"><?php echo esc_html( $order['billing_email'] ); ?></a><br>
    <?php if ( ! empty( $order['billing_phone'] ) ) : ?>
    <strong><?php esc_html_e( 'Phone:', 'titancart-lite' ); ?></strong> <?php echo esc_html( $order['billing_phone'] ); ?><br>
    <?php endif; ?>
    <strong><?php esc_html_e( 'IP:', 'titancart-lite' ); ?></strong> <?php echo esc_html( $order['ip_address'] ?? '—' ); ?>
</p>

<div class="address-grid">
    <div class="address-block">
        <strong><?php esc_html_e( 'Billing Address', 'titancart-lite' ); ?></strong><br>
        <?php echo nl2br( esc_html( titancart_format_address( $order, 'billing' ) ) ); ?>
    </div>
    <div class="address-block">
        <strong><?php esc_html_e( 'Shipping Address', 'titancart-lite' ); ?></strong><br>
        <?php echo nl2br( esc_html( titancart_format_address( $order, 'shipping' ) ) ); ?>
    </div>
</div>

<?php if ( ! empty( $order['customer_note'] ) ) : ?>
<hr class="divider">
<h3><?php esc_html_e( 'Customer Note', 'titancart-lite' ); ?></h3>
<p style="background:#F6F8FB;padding:12px;border-radius:8px;font-style:italic;">
    <?php echo esc_html( $order['customer_note'] ); ?>
</p>
<?php endif; ?>
