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

  // Fetch unique AWB, sender, and receiver from internal_resi table
  $internal_resi_data = $db->query("SELECT DISTINCT awb, sender, receiver FROM internal_resi");

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

<!-- Page content -->
<div class="container">
  <h1><?php echo $page_title; ?></h1>
  
  <!-- CSV Import Form -->
  <form method="POST" enctype="multipart/form-data">
    <div class="form-group">
      <label for="csv_file">Import CSV (with ; delimiter):</label>
      <input type="file" name="csv_file" class="form-control" required>
    </div>
    <button type="submit" name="import" class="btn btn-primary">Import CSV</button>
  </form>

  <!-- Display any success/error messages -->
  <?php echo display_msg($msg); ?>

  <!-- Print All Button -->
  <a href="print_all_resi.php" class="btn btn-success">Print All Resi</a> <!-- Button for printing all resi -->

  <!-- Check if data exists -->
  <?php if($db->num_rows($internal_resi_data) > 0): ?>
    <table class="table table-bordered">
      <thead>
        <tr>
          <th>AWB</th>
          <th>Nama Pengirim</th>
          <th>Nama Penerima</th>
          <th>Aksi</th> <!-- Column for action -->
        </tr>
      </thead>
      <tbody>
        <!-- Loop through the data and display it in the table -->
        <?php while($row = $db->fetch_assoc($internal_resi_data)): ?>
          <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>
              <!-- Link to view_barang_resi.php and print_resi.php, passing the AWB as a parameter -->
              <a href="view_barang_resi.php?awb=<?php echo urlencode($row['awb']); ?>" class="btn btn-info btn-sm">Lihat Barang</a>
              <a href="print_resi.php?awb=<?php echo urlencode($row['awb']); ?>" class="btn btn-secondary btn-sm">Print Resi</a>
            </td>
          </tr>
        <?php endwhile; ?>
      </tbody>
    </table>
  <?php else: ?>
    <div class="alert alert-warning">Tidak ada data resi internal yang ditemukan.</div>
  <?php endif; ?>
</div>

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