<?php

if (!defined('ABSPATH')) { exit; }

?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title><?php echo esc_html($doc_label . ' ' . $doc_number); ?></title>
<style type="text/css"><?php include __DIR__ . '/style.css'; ?></style>
<style>
table.order-details { width:100%; table-layout: fixed; border-collapse: collapse; }
table.order-details th.col-sku    { width:18%; text-align:left;   white-space: nowrap; }
table.order-details th.col-qty    { width: 7%; text-align:center; white-space: nowrap; }
table.order-details th.col-desc   { width:62%; text-align:left; }
table.order-details th.col-price  { width:13%; text-align:right;  white-space: nowrap; }
/* !important on the wrap rules — the bundled style.css ships a generic
   `td { word-wrap: break-word }` that otherwise cracks "4,99 €" and
   "Price" across two lines inside the narrow Price column. */
table.order-details td.col-sku    { font-family: monospace; font-size: 11px; white-space: nowrap !important; word-wrap: normal !important; vertical-align: top; }
table.order-details td.col-qty    { text-align:center; white-space: nowrap !important; word-wrap: normal !important; vertical-align: top; }
table.order-details td.col-desc   { word-wrap: break-word; vertical-align: top; }
table.order-details td.col-desc .ff-barcode { font-family: monospace; font-size: 10px; color:#646970; margin-top:2px; }
table.order-details td.col-price  { text-align:right; white-space: nowrap !important; word-wrap: normal !important; vertical-align: top; }
table.order-details tfoot th       { text-align:right; font-weight:600; padding-right:10px; white-space: nowrap; }
table.order-details tfoot td.ff-v  { text-align:left; }
table.order-details tfoot tr.ff-cod td.ff-v,
table.order-details tfoot tr.ff-cod th { color:#b91c1c; font-weight:700; }
table.order-details tfoot tr.ff-total td.ff-v,
table.order-details tfoot tr.ff-total th { font-size:15px; font-weight:700; }
</style>
</head>
<body class="<?php echo esc_attr($doc_type); ?>">

<table class="head container">
    <tr>
        <td class="header">
            <?php if (!empty($shop['logo_url'])): ?>
                <img src="<?php echo esc_url($shop['logo_url']); ?>" alt="">
            <?php else: ?>
                <?php echo esc_html(strtoupper(__('Packing slip', 'ai-eshop-optimizer'))); ?>
            <?php endif; ?>
        </td>
        <td class="shop-info">
            <?php  ?>
            <div class="shop-name"><h3><?php echo esc_html($shop['name']); ?></h3></div>
        </td>
    </tr>
</table>

<h1 class="document-type-label">
    #<?php echo esc_html((string) $order->get_order_number()); ?>
</h1>

<table class="order-data-addresses">
    <tr>
        <td class="address shipping-address">
            <?php
            $shipping = $order->get_formatted_shipping_address() ?: $order->get_formatted_billing_address();
            echo wp_kses_post($shipping);
            if ($order->get_billing_phone()) { echo '<div class="shipping-phone">' . esc_html($order->get_billing_phone()) . '</div>'; }
            ?>
        </td>
        <td class="address billing-address"></td>
        <td class="order-data">
            <table>
                <tr class="order-date">
                    <th><?php esc_html_e('Order date:', 'ai-eshop-optimizer'); ?></th>
                    <td><?php
                        $order_dt = $order->get_date_created();
                        echo esc_html($order_dt ? $order_dt->date_i18n(get_option('date_format')) : '—');
                    ?></td>
                </tr>
                <tr class="print-date">
                    <th><?php esc_html_e('Print date:', 'ai-eshop-optimizer'); ?></th>
                    <td><?php echo esc_html(date_i18n(get_option('date_format'), current_time('timestamp'))); ?></td>
                </tr>
            </table>
        </td>
    </tr>
</table>

<?php
$pay_method    = $order->get_payment_method_title();
$pay_method_id = (string) $order->get_payment_method();
$is_cod        = in_array($pay_method_id, array('cod', 'wc-cod'), true)
              || (function_exists('mb_strpos') && mb_strpos((string) $pay_method, 'ντικαταβολ') !== false)
              || stripos((string) $pay_method, 'cash on delivery') !== false;

usort($lines, function ($a, $b) {
    $as = (string) ($a['sku'] ?? '');
    $bs = (string) ($b['sku'] ?? '');
    if ($as === '' && $bs !== '') return 1;
    if ($as !== '' && $bs === '') return -1;
    return strnatcmp($as, $bs);
});
?>

<table class="order-details">
    <thead>
        <tr>
            <th class="col-sku"><?php esc_html_e('SKU', 'ai-eshop-optimizer'); ?></th>
            <th class="col-qty"><?php esc_html_e('Qty', 'ai-eshop-optimizer'); ?></th>
            <th class="col-desc"><?php esc_html_e('Title', 'ai-eshop-optimizer'); ?></th>
            <th class="col-price"><?php esc_html_e('Price', 'ai-eshop-optimizer'); ?></th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($lines as $line): ?>
        <tr>
            <td class="col-sku"><?php echo esc_html((string) $line['sku']); ?></td>
            <td class="col-qty"><?php echo esc_html((string) $line['qty']); ?></td>
            <td class="col-desc">
                <div class="item-name"><?php echo esc_html($line['name']); ?></div>
                <?php if ($line['gtin']): ?>
                    <div class="ff-barcode"><?php esc_html_e('Barcode', 'ai-eshop-optimizer'); ?>: <?php echo esc_html((string) $line['gtin']); ?></div>
                <?php endif; ?>
            </td>
            <td class="col-price"><?php
                echo wp_kses_post(wc_price((float) $line['price'], array('currency' => $totals['currency'])));
            ?></td>
        </tr>
        <?php endforeach; ?>
    </tbody>
    <tfoot>
        <?php $ship_method = $order->get_shipping_method(); if ($ship_method): ?>
        <tr class="ff-ship-row">
            <th colspan="2"><?php esc_html_e('Shipping method:', 'ai-eshop-optimizer'); ?></th>
            <td colspan="2" class="ff-v"><?php echo esc_html($ship_method); ?></td>
        </tr>
        <?php endif; ?>
        <?php if ($pay_method): ?>
        <tr class="ff-pay-row<?php echo $is_cod ? ' ff-cod' : ''; ?>">
            <th colspan="2"><?php esc_html_e('Payment method:', 'ai-eshop-optimizer'); ?></th>
            <td colspan="2" class="ff-v"><?php echo esc_html($pay_method); ?><?php if ($is_cod): ?> ⚠<?php endif; ?></td>
        </tr>
        <?php endif; ?>
        <tr class="ff-total-row ff-total">
            <th colspan="2"><?php esc_html_e('Total:', 'ai-eshop-optimizer'); ?></th>
            <td colspan="2" class="ff-v"><?php echo wp_kses_post(wc_price((float) $totals['total'], array('currency' => $totals['currency']))); ?></td>
        </tr>
    </tfoot>
</table>

<div class="bottom-spacer"></div>

<?php if ($order->get_customer_note()): ?>
<div class="customer-notes">
    <h3><?php esc_html_e('Notes', 'ai-eshop-optimizer'); ?></h3>
    <p><?php echo esc_html($order->get_customer_note()); ?></p>
</div>
<?php endif; ?>

</body>
</html>
