<%# templates/pdf/internalShippingLabelTemplate.ejs %>
<%# Internal-shipping parcel labels — mirror the external carrier's label (100x100 mm) and its A4 sheet of 2x4 labels. %>
<!DOCTYPE html>
<html lang="he" dir="rtl">
<head>
  <meta charset="UTF-8">
  <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@400;700&family=Libre+Barcode+39&display=block" rel="stylesheet">
  <style>
    * { margin: 0; padding: 0; box-sizing: border-box; }

    @page { size: A4; margin: 0; }

    html, body { background: #fff; }

    body {
      font-family: 'Heebo', Arial, sans-serif;
      color: #000;
      direction: rtl;
    }

    /* Sheet geometry copied from bulkPrintController.combineLabelsIntoPDF: A4 (595.28x841.89pt),
       20pt margins, 2 columns x 4 rows, each 283pt label scaled to 98% of its cell and centered.
       Filled left-to-right, top-to-bottom, like the carrier sheet. */
    .sheet {
      width: 595pt;
      height: 841pt;
      padding: 20pt;
      display: grid;
      grid-template-columns: repeat(2, 277.64pt);
      grid-template-rows: repeat(4, 200.47pt);
      direction: ltr;
      overflow: hidden;
      page-break-after: always;
    }

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

    .cell { position: relative; overflow: hidden; }

    .label {
      position: absolute;
      top: 50%;
      left: 50%;
      width: 100mm;
      height: 100mm;
      padding: 8mm 9mm 0 21mm;
      direction: rtl;
      overflow: hidden;
      transform: translate(-50%, -50%) scale(0.6942);
    }

    .barcode {
      direction: ltr;
      text-align: center;
      font-family: 'Libre Barcode 39', monospace;
      font-size: 13mm;
      line-height: 1;
      height: 11mm;
      overflow: hidden;
      white-space: nowrap;
    }

    .barcode-text {
      direction: ltr;
      text-align: center;
      font-family: Arial, sans-serif;
      font-size: 2.4mm;
      letter-spacing: 1.6mm;
      line-height: 1.2;
    }

    .meta {
      display: flex;
      justify-content: space-between;
      font-size: 1.8mm;
      font-weight: 700;
      margin: 0.6mm 0 1.4mm;
    }

    .box {
      border: 0.25mm solid #000;
      border-radius: 0.3mm;
      padding: 0 0.8mm;
      font-weight: 700;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
    }

    .name-box {
      display: flex;
      align-items: center;
      height: 4.2mm;
      font-size: 2.7mm;
      margin-bottom: 0.6mm;
    }

    .name-box .counter { flex: 0 0 auto; }
    .name-box .name { flex: 1; text-align: center; overflow: hidden; text-overflow: ellipsis; }

    .lines {
      font-size: 2.6mm;
      font-weight: 700;
      line-height: 1.3;
      max-height: 10.2mm;
      overflow: hidden;
      overflow-wrap: anywhere;
    }

    .row {
      display: flex;
      gap: 1mm;
      align-items: center;
      margin-top: 0.8mm;
    }

    .row > * { flex: 1; min-width: 0; }

    .for {
      font-size: 2.6mm;
      font-weight: 700;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }

    .reference {
      height: 3.4mm;
      font-size: 1.9mm;
      line-height: 3.2mm;
      direction: ltr;
      text-align: left;
    }

    .sender .box {
      height: 4.2mm;
      font-size: 2.6mm;
      line-height: 4mm;
    }

    .bottom {
      display: flex;
      gap: 1mm;
      margin-top: 1mm;
    }

    .bottom-main { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 0.8mm; }

    .bottom-main .row { margin: 0; }

    .bottom-main .box {
      height: 4.2mm;
      font-size: 2.6mm;
      line-height: 4mm;
    }

    .bottom-main .row .narrow { flex: 0 0 13.5mm; }

    .bottom-main .footer .box {
      height: 3.4mm;
      font-size: 2.1mm;
      line-height: 3.2mm;
      font-weight: 400;
    }

    /* The order number can be long (e.g. email orders) — wider box, smaller text. */
    .bottom-main .footer .narrow { flex-basis: 22mm; text-align: center; font-size: 1.9mm; direction: ltr; }

    .driver {
      flex: 0 0 17mm;
      display: flex;
      align-items: center;
      justify-content: center;
      text-align: center;
      white-space: normal;
      font-size: 3.6mm;
      line-height: 1.1;
      padding: 0.5mm;
    }
  </style>
</head>
<body>
<% sheets.forEach(function (sheet) { %>
<div class="sheet">
<% sheet.forEach(function (label) { %>
 <div class="cell">
  <div class="label">
    <div class="barcode">*<%= label.barcode %>*</div>
    <div class="barcode-text">*<%= label.barcode %>*</div>

    <div class="meta">
      <span>סוג: שילוח פנימי <%= label.printedAt %></span>
      <span><% if (label.driverName) { %>נהג: <%= label.driverName %><% } %></span>
    </div>

    <div class="box name-box">
      <span class="counter"><%= String(label.packageIndex).padStart(2, '0') %>/<%= String(label.packagesCount).padStart(2, '0') %></span>
      <span class="name"><%= label.customerName %></span>
    </div>

    <div class="lines">
      <div><%= label.addressLine %></div>
      <div>
        <% if (label.phones) { %>טל:<%= label.phones %><% } %>
        <% if (label.notes) { %> הערות כלליות: <%= label.notes %><% } %>
      </div>
    </div>

    <div class="row">
      <div class="for">עבור:<%= label.customerName %></div>
      <div class="box reference"><%= label.reference %></div>
    </div>

    <div class="row sender">
      <div class="box"><%= label.senderName %></div>
      <div class="box"><%= label.senderAddress %></div>
    </div>

    <div class="bottom">
      <div class="bottom-main">
        <div class="row">
          <div class="box narrow">פריטים <%= label.itemsCount %></div>
          <div class="box"><%= label.platformName %></div>
        </div>
        <div class="row footer">
          <div class="box narrow"><%= label.orderNumber %></div>
          <div class="box">נשלח באמצעות שילוח פנימי</div>
        </div>
      </div>
      <div class="box driver"><%= label.driverName || 'ללא נהג' %></div>
    </div>
  </div>
 </div>
<% }); %>
</div>
<% }); %>
</body>
</html>
