<?php
/**
 * TitanCart — Standalone Packing Slip document.
 *
 * Warehouse-internal pick list rendered by StorefrontModule::maybe_render_packing_slip()
 * at /order-packing-slip/{number}/. Admin-only; carries NO prices or payment
 * details. Mirrors the invoice document's clean print styling.
 *
 * In scope (set by the handler):
 *   @var array  $order                  Order row (flat billing_/shipping_ fields).
 *   @var array  $items                  Line items: name, sku, options_summary, quantity.
 *   @var string $status_label           Translated order status.
 *   @var string $shipping_method_title  Resolved shipping method name ('' if none).
 *   @var array  $company                name/address/phone/website.
 *   @var string $logo_url               Logo URL ('' → falls back to company name).
 *   @var string $accent_color           Accent colour (#hex).
 *   @var bool   $auto_print             Fire window.print() on load.
 *
 * @package TitanCart
 */

defined( 'ABSPATH' ) || exit;

$tc_number  = (string) ( $order['order_number'] ?? ( '#' . ( $order['id'] ?? '' ) ) );
$tc_accent  = preg_match( '/^#[0-9a-fA-F]{3,8}$/', (string) $accent_color ) ? $accent_color : '#1F6AE1';
$tc_date    = ! empty( $order['created_at'] ) ? mysql2date( get_option( 'date_format' ), $order['created_at'] ) : '';

$tc_has_ship = ! empty( $order['shipping_address_1'] ) || ! empty( $order['shipping_first_name'] );

// Total units (sum of quantities) + line count.
$tc_units = 0;
foreach ( (array) $items as $tc_it ) {
    $tc_units += (int) ( $tc_it['quantity'] ?? 1 );
}
$tc_lines = count( (array) $items );

/* Render one address block from a billing_/shipping_ prefix. */
$tc_addr = static function ( array $o, string $p ): string {
    $name = trim( ( $o[ $p . 'first_name' ] ?? '' ) . ' ' . ( $o[ $p . 'last_name' ] ?? '' ) );
    $out  = [];
    if ( $name !== '' ) { $out[] = esc_html( $name ); }
    if ( ! empty( $o[ $p . 'company' ] ) )   { $out[] = esc_html( $o[ $p . 'company' ] ); }
    if ( ! empty( $o[ $p . 'address_1' ] ) ) { $out[] = esc_html( $o[ $p . 'address_1' ] ); }
    if ( ! empty( $o[ $p . 'address_2' ] ) ) { $out[] = esc_html( $o[ $p . 'address_2' ] ); }
    $line3 = esc_html( $o[ $p . 'city' ] ?? '' );
    if ( ! empty( $o[ $p . 'state' ] ) )    { $line3 .= ', ' . esc_html( $o[ $p . 'state' ] ); }
    if ( ! empty( $o[ $p . 'postcode' ] ) ) { $line3 .= ' ' . esc_html( $o[ $p . 'postcode' ] ); }
    if ( trim( $line3 ) !== '' )            { $out[] = $line3; }
    if ( ! empty( $o[ $p . 'country' ] ) )  { $out[] = esc_html( $o[ $p . 'country' ] ); }
    if ( ! empty( $o[ $p . 'phone' ] ) )    { $out[] = '<span class="tc-ps__meta">' . esc_html( $o[ $p . 'phone' ] ) . '</span>'; }
    return implode( '<br>', $out );
};
?><!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
    <meta charset="<?php bloginfo( 'charset' ); ?>">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="robots" content="noindex, nofollow">
    <title><?php echo esc_html( sprintf( /* translators: %s = order number */ __( 'Packing Slip %s', 'titancart-lite' ), $tc_number ) ); ?></title>
    <?php
    // WP.org R3: document styles live in a static stylesheet registered and
    // printed through the core styles API. This is a standalone document (own
    // <!DOCTYPE html>, rendered outside the theme, no wp_head), so
    // wp_print_styles() is invoked directly at the point the <head> needs it —
    // same pipeline, explicit print. The brand accent is the only dynamic
    // value and travels as a CSS custom property via wp_add_inline_style().
    wp_register_style(
        'titancart-packing-slip-print',
        TITANCART_ASSETS_URL . 'css/packing-slip-print.css',
        [],
        TITANCART_VERSION
    );
    $tc_ps_accent = sanitize_hex_color( (string) $tc_accent );
    if ( ! is_string( $tc_ps_accent ) || $tc_ps_accent === '' ) {
        $tc_ps_accent = '#2563EB';
    }
    wp_add_inline_style( 'titancart-packing-slip-print', ':root{--tc-ps-accent:' . $tc_ps_accent . ';}' );
    wp_print_styles( [ 'titancart-packing-slip-print' ] );
    ?>
</head>
<body>
    <div class="tc-ps__print-bar">
        <button type="button" class="tc-ps__print-btn" onclick="window.print()"><?php esc_html_e( 'Print', 'titancart-lite' ); ?></button>
    </div>

    <div class="tc-ps">
        <div class="tc-ps__top">
            <div class="tc-ps__brand">
                <?php if ( ! empty( $logo_url ) ) : ?>
                    <img src="<?php echo esc_url( $logo_url ); ?>" alt="<?php echo esc_attr( $company['name'] ?? '' ); ?>">
                <?php else : ?>
                    <?php echo esc_html( $company['name'] ?? get_bloginfo( 'name' ) ); ?>
                <?php endif; ?>
                <?php if ( ! empty( $company['address'] ) ) : ?>
                    <div class="tc-ps__brand-addr"><?php echo esc_html( $company['address'] ); ?></div>
                <?php endif; ?>
            </div>
            <div class="tc-ps__title">
                <h1><?php esc_html_e( 'Packing Slip', 'titancart-lite' ); ?></h1>
                <div class="tc-ps__ordno"><?php echo esc_html( $tc_number ); ?></div>
                <?php if ( $tc_date !== '' ) : ?><div class="tc-ps__meta"><?php echo esc_html( $tc_date ); ?></div><?php endif; ?>
            </div>
        </div>

        <div class="tc-ps__cols">
            <?php if ( $tc_has_ship ) : ?>
            <div class="tc-ps__col">
                <h3><?php esc_html_e( 'Ship To', 'titancart-lite' ); ?></h3>
                <div class="tc-ps__big"><?php echo wp_kses_post( $tc_addr( $order, 'shipping_' ) ); ?></div>
            </div>
            <?php endif; ?>
            <div class="tc-ps__col">
                <h3><?php echo $tc_has_ship ? esc_html__( 'Bill To', 'titancart-lite' ) : esc_html__( 'Customer', 'titancart-lite' ); ?></h3>
                <div class="tc-ps__big"><?php echo wp_kses_post( $tc_addr( $order, 'billing_' ) ); ?></div>
            </div>
        </div>

        <div class="tc-ps__facts">
            <div class="tc-ps__fact"><span><?php esc_html_e( 'Order', 'titancart-lite' ); ?></span><strong><?php echo esc_html( $tc_number ); ?></strong></div>
            <div class="tc-ps__fact"><span><?php esc_html_e( 'Status', 'titancart-lite' ); ?></span><strong><?php echo esc_html( $status_label ); ?></strong></div>
            <?php if ( ! empty( $shipping_method_title ) ) : ?>
            <div class="tc-ps__fact"><span><?php esc_html_e( 'Shipping', 'titancart-lite' ); ?></span><strong><?php echo esc_html( $shipping_method_title ); ?></strong></div>
            <?php endif; ?>
            <div class="tc-ps__fact"><span><?php esc_html_e( 'Items', 'titancart-lite' ); ?></span><strong><?php echo esc_html( sprintf( /* translators: 1: line count, 2: total units */ _n( '%1$d line · %2$d unit', '%1$d lines · %2$d units', $tc_lines, 'titancart-lite' ), $tc_lines, $tc_units ) ); ?></strong></div>
        </div>

        <table class="tc-ps__items">
            <thead>
                <tr>
                    <th class="tc-ps__pick"><?php esc_html_e( 'Pick', 'titancart-lite' ); ?></th>
                    <th><?php esc_html_e( 'Product', 'titancart-lite' ); ?></th>
                    <th><?php esc_html_e( 'Product Code', 'titancart-lite' ); ?></th>
                    <th class="tc-ps__qty"><?php esc_html_e( 'Qty', 'titancart-lite' ); ?></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ( (array) $items as $item ) : ?>
                    <tr>
                        <td class="tc-ps__pick"><span class="tc-ps__pick-box"></span></td>
                        <td>
                            <span class="tc-ps__pname"><?php echo esc_html( $item['name'] ?? '' ); ?></span>
                            <?php if ( ! empty( $item['options_summary'] ) ) : ?><span class="tc-ps__popts"><?php echo esc_html( $item['options_summary'] ); ?></span><?php endif; ?>
                        </td>
                        <td><?php echo ! empty( $item['sku'] ) ? esc_html( $item['sku'] ) : '&mdash;'; ?></td>
                        <td class="tc-ps__qty"><?php echo esc_html( (int) ( $item['quantity'] ?? 1 ) ); ?></td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>

        <?php if ( ! empty( $order['customer_note'] ) ) : ?>
        <div class="tc-ps__note">
            <span><?php esc_html_e( 'Customer Note', 'titancart-lite' ); ?></span>
            <?php echo esc_html( $order['customer_note'] ); ?>
        </div>
        <?php endif; ?>

        <div class="tc-ps__sign">
            <div><?php esc_html_e( 'Picked by', 'titancart-lite' ); ?></div>
            <div><?php esc_html_e( 'Checked by', 'titancart-lite' ); ?></div>
            <div><?php esc_html_e( 'Date', 'titancart-lite' ); ?></div>
        </div>
    </div>

    <?php if ( ! empty( $auto_print ) ) : ?>
    <?php
    // WP.org R1 🅱1: standalone document — no footer to enqueue into, so use the
    // core script-tag API directly.
    wp_print_inline_script_tag( "window.addEventListener('load', function(){ window.print(); });" );
    ?>
    <?php endif; ?>
</body>
</html>
