<?php
include('db/cn.php');

// Validate order_id
$orderID = $_GET["order_id"] ?? null;
if (!$orderID) {
    die("Order ID is required");
}

// Get order details
$q  = mysqli_query($connection, "SELECT * FROM log_user_sales WHERE order_id = '$orderID' LIMIT 1");
$qr = mysqli_fetch_assoc($q);

// Default values
$orderDate     = $qr['d_date']        ?? 'N/A';
$customer_name = $qr['customer_name'] ?? null;

// Get customer details if found
$customerNo = 'N/A';
$customerName = $customer_name ?: 'Unknown';
$customerAddr = 'Address not available';

if ($customer_name) {
    $cus = mysqli_query($connection, "SELECT * FROM credit_note WHERE cr_name = '$customer_name' LIMIT 1");
    if ($cus && mysqli_num_rows($cus) > 0) {
        $re = mysqli_fetch_assoc($cus);
        $customerNo   = $re['cr_id']      ?? 'N/A';
        $customerName = $re['cr_name']    ?? $customer_name;
        $customerAddr = $re['cr_address'] ?? 'Address not available';
    }
}
?>

<!doctype html>
<html lang="de">
<head>
  <meta charset="utf-8">
  <title>World Diagnostic</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
  <style>
    body { background:#f5f6f8; }
    .sheet { max-width:794px; min-height:1123px; 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; }
    @media print {
      body { background:#fff; }
      .sheet { box-shadow:none; margin:0; max-width:none; min-height:auto; padding:16mm 18mm; }
      .no-print { display:none !important; }
    }
  </style>
</head>
<body>

<div class="sheet">
  <div class="text-center">
    <h6 class="mb-1 fw-bold">WORLD DIAGNOSTIC</h6>
    <div class="small">
        <strong>0333-0721712</strong><br>
      Office No. 4 First Floor<br>Khan Medical City,<br>Nishter Road, Multan.<br>
      
    </div>
  </div>

  <!-- Right meta -->
  <div class="d-flex justify-content-end mt-3">
    <div class="text-end top-meta">
      <small><strong>Delivery Note No.</strong> <?php echo htmlspecialchars($orderID); ?></small>
      <small><strong>Customer No.</strong> <?php echo htmlspecialchars($customerNo); ?></small>
      <small><strong>Date</strong> <?php echo htmlspecialchars($orderDate); ?></small>
    </div>
  </div>

  <div class="text-center my-3">
    <h4 class="doc-title m-0">Delivery Slip</h4>
  </div>

  <!-- Recipient -->
  <div class="row mt-3">
    <div class="col-12">
      <div class="fw-semibold mb-1">Recipient:</div>
      <div class="lh-sm">
        <div><strong><?php echo htmlspecialchars($customerName); ?></strong><br>
        <?php echo htmlspecialchars($customerAddr); ?></div>
      </div>
    </div>
  </div>

  <!-- Items -->
  <div class="mt-4">
    <table class="table table-borderless align-middle">
      <thead class="border-bottom">
        <tr class="fw-semibold">
          <th style="width:18%;">Article No.</th>
          <th>Description</th>
          <th class="text-end" style="width:18%;">Quantity</th>
        </tr>
      </thead>
      <tbody>
        <?php
        $rt1 = mysqli_query($connection,"SELECT * FROM log_user_sales WHERE order_id = '$orderID'");
        if ($rt1 && mysqli_num_rows($rt1) > 0) {
            while ($TR = mysqli_fetch_assoc($rt1)) {
                echo "<tr>";
                echo "<td>".htmlspecialchars($TR["product_id"])."</td>";
                echo "<td>".htmlspecialchars($TR["product_name"])."</td>";
                echo "<td class='text-end'>".htmlspecialchars($TR["product_quantity"])." PCS</td>";
                echo "</tr>";
            }
        } else {
            echo "<tr><td colspan='3' class='text-center'>No items found</td></tr>";
        }
        ?>
      </tbody>
    </table>
  </div>
</div>
</body>
</html>
