<?php
$page_title = 'Bazzar Order Details';
require_once('includes/load.php');
page_require_level(1);

$order_id = $_GET['id'];  // Get the order ID from the URL

// Fetch the order details
$sql_order = "SELECT * FROM bazzar WHERE id = '$order_id'";
$order = $db->query($sql_order)->fetch_assoc();

// Fetch order details (products)
$sql_details = "SELECT * FROM bazzar_detail WHERE bazzar_id = '$order_id'";
$details = $db->query($sql_details);

// Fetch return details
$sql_returns = "SELECT * FROM bazzar_return WHERE bazzar_id = '$order_id'";
$returned_products = $db->query($sql_returns);

// Calculate total returned quantity
$total_returned = 0;
$total_item = 0; // To track the total quantity of items in the order
$total_remaining_subtotal = 0; // To track the total remaining subtotal after returns

while ($returned_product = $returned_products->fetch_assoc()) {
  $total_returned += $returned_product['quantity'];
}

while ($details_product = $details->fetch_assoc()) {
  $total_item += $details_product['quantity'];
}

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

<div class="row">
  <div class="col-md-12">
    <?php echo display_msg($msg); ?>
  </div>
  <div class="col-md-12">
    <div class="panel panel-default">
      <div class="panel-heading">
        <h3 class="panel-title">Bazzar Order Details</h3>
      </div>
      <div class="panel-body">
        <div class="row">
          <div class="col-sm-6">
            <h4><strong>Order Number:</strong> <?php echo htmlspecialchars($order['bazzar_sn']); ?></h4>
            <p><strong>Salesman:</strong> <?php echo htmlspecialchars($order['sales_man']); ?></p>
            <p><strong>Customer Name:</strong> <?php echo htmlspecialchars($order['customer_name']); ?></p>
            <p><strong>Address:</strong> <?php echo htmlspecialchars($order['address']); ?></p>
            <p><strong>Status:</strong> <?php echo htmlspecialchars($order['status']); ?></p>
            <p><strong>Delivery Deadline:</strong> <?php echo htmlspecialchars($order['batas_kirim']); ?></p>
          </div>
          <div class="col-sm-6">
            <h4><strong>Total Returned:</strong> <?php echo $total_returned; ?> products</h4>
            <h4><strong>Total Remaining:</strong> <?php echo ($total_item - $total_returned); ?> products</h4>
          </div>
        </div>

        <h4>Products:</h4>
        <table class="table table-striped table-bordered table-hover">
          <thead>
            <tr>
              <th>Product Name</th>
              <th>Original Quantity</th>
              <th>Price</th>
              <th>Subtotal</th>
              <th>Returned Quantity</th>
              <th>Remaining Quantity</th>
              <th>Remaining Subtotal</th> <!-- New column for remaining subtotal -->
            </tr>
          </thead>
          <tbody>
            <?php
            // Reset the pointer for details and process each product
            $details->data_seek(0);
            while ($detail = $details->fetch_assoc()):
              // Calculate the remaining quantity for each product
              $returned_quantity = 0;
              $returned_products->data_seek(0); // Reset the pointer for the returned products
              while ($returned_product = $returned_products->fetch_assoc()) {
                if ($returned_product['product_name'] == $detail['product_name']) {
                  $returned_quantity += $returned_product['quantity'];
                }
              }
              $remaining_quantity = $detail['quantity'] - $returned_quantity;
              $remaining_subtotal = $remaining_quantity * $detail['price']; // Calculate the remaining subtotal
              $total_remaining_subtotal += $remaining_subtotal; // Add to the total remaining subtotal
            ?>
            <tr>
              <td><?php echo htmlspecialchars($detail['product_name']); ?></td>
              <td><?php echo htmlspecialchars($detail['quantity']); ?></td>
              <td><?php echo formatCurrency($detail['price']); ?></td>
              <td><?php echo formatCurrency($detail['subtotal']); ?></td>
              <td><?php echo $returned_quantity; ?></td>
              <td><?php echo $remaining_quantity; ?></td>
              <td><?php echo formatCurrency($remaining_subtotal); ?></td> <!-- Show remaining subtotal -->
            </tr>
            <?php endwhile; ?>
          </tbody>
          <tfoot>
            <tr>
              <td colspan="6" class="text-right"><strong>Total Remaining Subtotal:</strong></td>
              <td><?php echo formatCurrency($total_remaining_subtotal); ?></td> <!-- Total Remaining Subtotal -->
            </tr>
          </tfoot>
        </table>
      </div>
    </div>
  </div>
</div>

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