<?php

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

?>
<!doctype html>
<html lang="<?php echo esc_attr($language); ?>">
<head>
<meta charset="UTF-8">
<title><?php echo esc_html($doc_label . ' ' . $doc_number); ?></title>
<style><?php include __DIR__ . '/style.css'; ?></style>
<style>
/* New 4-column layout — Barcode now lives beneath Title. */
table.ff-lines { width:100%; table-layout: fixed; border-collapse: collapse; }
table.ff-lines th.col-sku    { width:18%; text-align:left;   white-space: nowrap; }
table.ff-lines th.col-qty    { width: 7%; text-align:center; white-space: nowrap; }
table.ff-lines th.col-desc   { width:62%; text-align:left; }
table.ff-lines 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 still cracks "4,99 €"
   and "Price" across two lines inside the narrow Price column. */
table.ff-lines td.col-sku    { font-family: monospace; font-size: 11px; white-space: nowrap !important; word-wrap: normal !important; vertical-align: top; }
table.ff-lines td.col-qty    { text-align:center; white-space: nowrap !important; word-wrap: normal !important; vertical-align: top; }
table.ff-lines td.col-desc   { word-wrap: break-word; vertical-align: top; }
/* Barcode rendered as a sibling <div> below the title — DOMPDF (3.x)
   doesn't always honour `display:block` on inline elements so we use
   a block element from the start. */
table.ff-lines td.col-desc .ff-barcode { font-family: monospace; font-size: 10px; color:#646970; margin-top:2px; }
table.ff-lines td.col-price  { text-align:right; white-space: nowrap !important; word-wrap: normal !important; vertical-align: top; }
/* Footer — give the label generous space, push the value to its own
   half. 2+2 colspan split = ~25% label, ~75% value. */
table.ff-lines tfoot th       { text-align:right; font-weight:600; padding-right:10px; white-space: nowrap; }
table.ff-lines tfoot td.ff-v  { text-align:left; }
table.ff-lines tfoot tr.ff-cod td.ff-v,
table.ff-lines tfoot tr.ff-cod th { color:#b91c1c; font-weight:700; }
table.ff-lines tfoot tr.ff-total td.ff-v,
table.ff-lines tfoot tr.ff-total th { font-size:15px; font-weight:700; }
</style>
</head>
<body>

<div class="ff-doc">

  <header class="ff-header">
    <div class="ff-shop">
      <?php if (!empty($shop['logo_url'])): ?><img class="ff-logo" src="<?php echo esc_url($shop['logo_url']); ?>" alt=""><?php endif; ?>
      <div class="ff-shop-name"><?php echo esc_html($shop['name']); ?></div>
      <?php  ?>
    </div>
    <div class="ff-doc-meta">
      <h1>#<?php echo esc_html((string) $order->get_order_number()); ?></h1>
      <table>
        <tr>
          <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>
          <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>
    </div>
  </header>

  <section class="ff-addresses">
    <div class="ff-addr">
      <h3><?php esc_html_e('Ship to', 'ai-eshop-optimizer'); ?></h3>
      <?php
      $shipping = $order->get_formatted_shipping_address() ?: $order->get_formatted_billing_address();
      echo wp_kses_post($shipping);
      if ($order->get_billing_phone()) { echo '<br>' . esc_html($order->get_billing_phone()); }
      ?>
    </div>
  </section>

  <?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="ff-lines">
    <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="ff-title"><?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">
        <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<?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">
        <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>

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

</div>
</body>
</html>
