<?php
// File: pages/lieferschein-a4.php
include('db/cn.php');
$orderID = isset($_GET['order_id']) ? mysqli_real_escape_string($connection, $_GET['order_id']) : '';

$q  = mysqli_query($connection, "SELECT * FROM log_user_sales WHERE order_id = '$orderID' LIMIT 1");
$qr = mysqli_fetch_array($q);
?>

<!doctype html>
<html lang="de">
<head>
  <meta charset="utf-8">
  <title>Lieferschein</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <!-- Bootstrap 5 -->
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
  <style>
    /* --- Global 12px reset (override Bootstrap rem sizing) --- */
    html, body { font-size:12px !important; }
    .sheet { font-size:12px !important; }
    .sheet * { font-size:12px !important; line-height:1.35; }
    h1, h2, h3, h4, h5, h6,
    .h1, .h2, .h3, .h4, .h5, .h6,
    .doc-title, .fw-bold, .fw-semibold,
    .btn, .form-control, .form-select,
    .table, .table th, .table td,
    .top-meta small, .small { font-size:12px !important; }

    body { background:#f5f6f8; }

    /* Screen layout: reduced width/height */
    .sheet {
      max-width: 650px;      /* reduced width for screen */
      min-height: 900px;     /* reduced height for screen */
      background:#fff;
      margin: 24px auto;
      padding: 28px 32px 48px;
      box-shadow: 0 6px 24px rgba(0,0,0,.08);
    }

    .doc-title {
      text-decoration: underline;
      font-weight: 700;
      letter-spacing: .3px;
    }
    .top-meta small { display:block; line-height:1.25; }
    .signature { height: 120px; }
    .bottom-rule { border-top: 2px solid #000; margin-top: 48px; }

    /* Table spacing tight for 12px */
    .table th, .table td { padding: .35rem .5rem; }

    /* Print layout: enforce A4 */
    @media print {
      @page {
        size: A4 portrait;   /* Force A4 size */
        margin: 16mm 18mm;   /* Printer margins */
      }
      html, body, .sheet { font-size:12px !important; }
      body { background:#fff; }
      .sheet {
        box-shadow: none;
        margin: 0 auto;
        width: 100%;       /* fill A4 page */
        min-height: auto;
        padding: 0;        /* @page margin handles spacing */
      }
      .no-print { display:none !important; }
    }
  </style>
</head>
<body>

<div class="sheet">

  <!-- Buttons -->
  <div class="d-flex justify-content-between mb-3 no-print">
    <button class="btn btn-secondary" onclick="window.location.href='order_detail_v.php'">Back</button>
    <button class="btn btn-primary" onclick="window.print()">Print</button>
  </div>

  <!-- Header -->
  <div class="text-center">
    <h6 class="mb-1 fw-bold">Fine &amp; Fair</h6>
    <div>
      Einzel und Großhandel<br>
      Indisches Lebensmittel Geschäft<br>
      Balinger Str. 1, 70567 Stuttgart, Deutschland.<br>
      Handy: +49 176 47075780
    </div>
  </div>

  <!-- Right-side meta -->
  <div class="d-flex justify-content-end mt-3">
    <div class="text-end top-meta">
      <?php
        $cutomer = $qr['customer_name'] ?? '';
        $cus = mysqli_query($connection, "SELECT * FROM credit_note WHERE cr_name = '$cutomer' LIMIT 1");
        $re  = mysqli_fetch_array($cus);
      ?>
      <small><strong>Lieferschein Nr.</strong> <?php echo htmlspecialchars($orderID); ?></small>
      <small><strong>Kunden Nr.</strong> <?php echo htmlspecialchars($re['cr_id'] ?? ''); ?></small>
      <small><strong>Datum.</strong> <?php echo htmlspecialchars($qr['d_date'] ?? ''); ?></small>
    </div>
  </div>

  <!-- Title -->
  <div class="text-center my-3">
    <h4 class="doc-title m-0">Lieferschein</h4>
  </div>

  <!-- Recipient -->
  <div class="row mt-3">
    <div class="col-12">
      <div class="fw-semibold mb-1">Empfänger:</div>
      <div class="lh-sm">
        <div>
          <strong><?php echo htmlspecialchars($re['cr_name'] ?? ''); ?></strong><br>
          <?php echo htmlspecialchars($re['phone_number'] ?? ''); ?><br>
          <?php echo htmlspecialchars($re['cr_address'] ?? ''); ?><br>
          <?php echo htmlspecialchars($re['city'] ?? ''); ?><br>
          <?php echo htmlspecialchars($re['p_code'] ?? ''); ?>
        </div>
      </div>
    </div>
  </div>

  <!-- Items Table -->
  <div class="mt-4">
    <table class="table table-borderless align-middle">
      <thead class="border-bottom">
        <tr class="fw-semibold">
          <th style="width: 18%;">Artikelnr</th>
          <th>Bezeichnung</th>
          <th class="text-end" style="width: 18%;">Menge</th>
        </tr>
      </thead>
      <tbody>
        <?php
          $rt1 = mysqli_query($connection, "SELECT * FROM log_user_sales WHERE order_id = '$orderID'");
          while ($TR = mysqli_fetch_array($rt1)) {
        ?>
        <tr>
          <td><?php echo htmlspecialchars($TR['product_id']); ?></td>
          <td><?php echo htmlspecialchars($TR['product_name']); ?></td>
          <td class="text-end"><?php echo htmlspecialchars($TR['product_quantity']) . ' PCS'; ?></td>
        </tr>
        <?php } ?>
      </tbody>
    </table>
  </div>

  <!-- Signature area -->
  <div class="signature"></div>

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