<!DOCTYPE html>
<html lang="<%= isHebrew ? 'he' : 'en' %>" dir="<%= isHebrew ? 'rtl' : 'ltr' %>">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title><%= i18n.title %></title>
  <link rel="preconnect" href="https://fonts.googleapis.com">
  <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  <link href="https://fonts.googleapis.com/css2?family=Heebo:wght@300;400;500;600;700&display=swap" rel="stylesheet">
  <style>
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }

    @page {
      size: A4;
      margin: 14mm 14mm;
    }

    body {
      font-family: 'Heebo', sans-serif;
      font-size: 12px;
      line-height: 1.5;
      color: #000;
      background: #fff;
      direction: <%= isHebrew ? 'rtl' : 'ltr' %>;
    }

    .driver-page {
      page-break-after: always;
    }

    .driver-page:last-child {
      page-break-after: auto;
    }

    .header {
      margin-bottom: 18px;
      padding-bottom: 12px;
      border-bottom: 1px solid #000;
    }

    .header-eyebrow {
      font-size: 10px;
      font-weight: 400;
      letter-spacing: 1px;
      text-transform: uppercase;
      color: #000;
      margin-bottom: 4px;
    }

    .driver-name {
      font-size: 22px;
      font-weight: 600;
      color: #000;
      margin-bottom: 6px;
    }

    .driver-meta {
      font-size: 11px;
      color: #000;
    }

    .driver-meta-separator {
      margin: 0 6px;
    }

    .summary {
      font-size: 11px;
      color: #000;
      margin-bottom: 10px;
    }

    .products-table {
      width: 100%;
      border-collapse: collapse;
      page-break-inside: auto;
    }

    .products-table thead th {
      padding: 8px 6px;
      text-align: <%= isHebrew ? 'right' : 'left' %>;
      font-weight: 600;
      color: #000;
      border-bottom: 1px solid #000;
      font-size: 12px;
    }

    .products-table tbody tr {
      border-bottom: 1px solid #d1d5db;
      page-break-inside: avoid;
    }

    .products-table tbody td {
      padding: 8px 6px;
      text-align: <%= isHebrew ? 'right' : 'left' %>;
      color: #000;
      font-size: 12px;
      vertical-align: top;
    }

    /* Each order is its own row group, kept together on one page where possible. */
    .products-table tbody.order-block {
      page-break-inside: avoid;
    }

    /* Strong divider between consecutive orders so each delivery reads as one block. */
    .products-table tbody.order-block + tbody.order-block tr:first-child td {
      border-top: 2px solid #000;
    }

    .products-table .col-index {
      width: 4%;
      text-align: center;
      font-weight: 600;
    }

    .products-table thead th.col-index {
      text-align: center;
    }

    .products-table .col-phone {
      width: 13%;
      direction: ltr;
      text-align: <%= isHebrew ? 'right' : 'left' %>;
      unicode-bidi: embed;
    }

    /* Optional second contact number, printed under the main phone. */
    .products-table .phone-secondary {
      display: block;
      margin-top: 2px;
    }

    .products-table .internal-note {
      white-space: pre-wrap;
      word-break: break-word;
    }

    .products-table .col-customer-name {
      width: 12%;
      word-break: break-word;
    }

    .products-table .col-city {
      width: 10%;
    }

    .products-table .col-address {
      width: 16%;
      word-break: break-word;
    }

    .products-table .col-sku {
      width: 13%;
      direction: ltr;
      text-align: <%= isHebrew ? 'right' : 'left' %>;
      unicode-bidi: embed;
    }

    .products-table .col-product-name {
      width: 18%;
      word-break: break-word;
    }

    .products-table .col-quantity {
      width: 7%;
      text-align: center;
    }

    .products-table thead th.col-quantity {
      text-align: center;
    }

    /* Cartons of the whole stop — one value, merged across the stop's item rows. */
    .products-table .col-packages {
      width: 7%;
      text-align: center;
      font-weight: 600;
    }

    .products-table thead th.col-packages {
      text-align: center;
    }

    .empty-state {
      text-align: center;
      color: #000;
      padding: 24px;
      font-size: 12px;
    }

    .footer {
      text-align: center;
      margin-top: 18px;
      padding-top: 10px;
      border-top: 1px solid #d1d5db;
      font-size: 10px;
      color: #000;
    }
  </style>
</head>
<body>
  <% driverGroups.forEach(function(group) { %>
    <section class="driver-page">
      <div class="header">
        <div class="header-eyebrow"><%= i18n.title %></div>
        <div class="driver-name"><%- escapeHtml(group.driver.name || '-') %></div>
        <div class="driver-meta"><%= i18n.printedAt %>: <span dir="ltr"><%= printDate %></span></div>
      </div>

      <% if (group.orders.length > 0) { %>
        <table class="products-table">
          <thead>
            <tr>
              <th class="col-index"><%= i18n.colIndex %></th>
              <th class="col-phone"><%= i18n.colPhone %></th>
              <th class="col-customer-name"><%= i18n.colCustomerName %></th>
              <th class="col-city"><%= i18n.colCity %></th>
              <th class="col-address"><%= i18n.colAddress %></th>
              <th class="col-sku"><%= i18n.colSku %></th>
              <th class="col-product-name"><%= i18n.colProductName %></th>
              <th class="col-quantity"><%= i18n.colQuantity %></th>
              <th class="col-packages"><%= i18n.colPackages %></th>
            </tr>
          </thead>
          <% group.orders.forEach(function(order) { %>
            <%
              // Customer columns are merged across all of the order's item rows so the
              // extra rows are visibly part of the same order; they render once, on the
              // first item row, spanning the rest via rowspan.
              var span = order.items.length;
            %>
            <tbody class="order-block">
              <% order.items.forEach(function(item, idx) { %>
                <tr>
                  <% if (idx === 0) { %>
                    <td class="col-index" rowspan="<%= span %>"><%= order.index %></td>
                    <td class="col-phone" rowspan="<%= span %>">
                      <%- escapeHtml(order.phone || '-') %>
                      <% if (order.phone2) { %>
                        <span class="phone-secondary"><%- escapeHtml(order.phone2) %></span>
                      <% } %>
                    </td>
                    <td class="col-customer-name" rowspan="<%= span %>"><%- escapeHtml(order.customerName || '-') %></td>
                    <td class="col-city" rowspan="<%= span %>"><%- escapeHtml(order.city || '-') %></td>
                    <td class="col-address" rowspan="<%= span %>"><%- escapeHtml(order.address || '-') %></td>
                  <% } %>
                  <td class="col-sku"><%- escapeHtml(item.sku || '-') %></td>
                  <td class="col-product-name"><%- escapeHtml(item.productName || '-') %></td>
                  <td class="col-quantity"><%= item.quantity || 0 %></td>
                  <% if (idx === 0) { %>
                    <%# Last column: in RTL the cartons sit to the left of the quantity. %>
                    <td class="col-packages" rowspan="<%= span %>"><%= order.packagesCount || 1 %></td>
                  <% } %>
                </tr>
              <% }); %>
              <% if (order.internalNotes && order.internalNotes.length > 0) { %>
                <tr>
                  <td class="internal-note" colspan="9"><strong><%= i18n.internalNoteLabel %>:</strong> <%- escapeHtml(order.internalNotes.join('\n')) %></td>
                </tr>
              <% } %>
            </tbody>
          <% }); %>
        </table>
      <% } else { %>
        <div class="empty-state"><%= i18n.noRows %></div>
      <% } %>

    </section>
  <% }); %>
</body>
</html>
