<?php
  // Set page title
  $page_title = 'Detail Barang Berdasarkan Resi'; 
  
  // Include necessary files and check user level
  require_once('includes/load.php');
  page_require_level(3);

  // Check if AWB is set in the URL
  if(isset($_GET['awb'])) {
    $awb = $db->escape($_GET['awb']);

    // Query to fetch the details of items based on AWB
    $barang_resi = $db->query("SELECT awb, sender, receiver, nama_barang FROM internal_resi WHERE awb = '{$awb}'");
  } else {
    // Redirect to internal resi page if AWB is not provided
    $session->msg('d', 'No AWB selected.');
    redirect('internal_resi.php', false);
  }

  // Include the header
  include_once('layouts/header.php');
?>

<!-- Page content -->
<div class="container">
  <h1><?php echo $page_title; ?></h1>
  
  <!-- Check if data exists -->
  <?php if($db->num_rows($barang_resi) > 0): ?>
    <table class="table table-bordered">
      <thead>
        <tr>
          <th>AWB</th>
          <th>Nama Pengirim</th>
          <th>Nama Penerima</th>
          <th>Nama Barang</th>
        </tr>
      </thead>
      <tbody>
        <!-- Loop through the data and display it in the table -->
        <?php while($row = $db->fetch_assoc($barang_resi)): ?>
          <tr>
            <td><?php echo remove_junk($row['awb']); ?></td>
            <td><?php echo remove_junk($row['sender']); ?></td>
            <td><?php echo remove_junk($row['receiver']); ?></td>
            <td><?php echo remove_junk($row['nama_barang']); ?></td>
          </tr>
        <?php endwhile; ?>
      </tbody>
    </table>
  <?php else: ?>
    <div class="alert alert-warning">Tidak ada barang terkait dengan resi ini.</div>
  <?php endif; ?>
</div>

<?php
  // Include the footer
  include_once('layouts/footer.php');
?>
