<?php
require_once('includes/load.php');
$page_title = 'Sales Report Detail';

// Get parameters from the URL
$shop_id = isset($_GET['shop_id']) ? $_GET['shop_id'] : '';
$platform = isset($_GET['platform']) ? $_GET['platform'] : '';
$startDate = isset($_GET['startDate']) ? $_GET['startDate'] : '';
$endDate = isset($_GET['endDate']) ? $_GET['endDate'] : '';

// Base SQL query to get the details based on passed parameters
$sql = "SELECT 
            s.kode AS shop_name,
            p.platform_name AS platform_name,
            o.order_sn,
            o.create_time AS order_date,
            CASE 
        WHEN s.platform_id = 4 THEN od.model_sku
        WHEN od.model_sku IS NOT NULL AND od.model_sku != '' AND LENGTH(od.model_sku) >= LENGTH(od.item_sku) 
        THEN od.model_sku 
        ELSE od.item_sku 
    END AS product_name,
            od.model_quantity_purchased AS quantity,
            od.model_original_price AS original_price,
            od.model_discounted_price AS discounted_price,
            (CASE WHEN od.model_discounted_price = 0 THEN od.model_original_price ELSE od.model_discounted_price END) * od.model_quantity_purchased AS total_price
        FROM 
            shop s
        JOIN 
            platform p on s.platform_id = p.id
        LEFT JOIN 
            orders o ON s.shop_id = o.shop_id
        LEFT JOIN 
            order_detail od ON o.id = od.order_id
        WHERE 
            s.kode = '$shop_id' AND 
            p.platform_name = '$platform'";

if (!empty($startDate)) {
  $sql .= " AND o.create_time >= '" . addslashes($startDate) . "'";
}

if (!empty($endDate)) {
  $sql .= " AND o.create_time <= '" . addslashes($endDate) . "'";
}

$all_details = $db->query($sql);
?>

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

<div class="row">
  <div class="col-md-12">
    <div class="panel panel-default">
      <div class="panel-heading clearfix">
        <strong>
          <span class="glyphicon glyphicon-th"></span>
          <span>Order Detail for Shop: <?php echo $shop_id; ?>, Platform: <?php echo $platform; ?></span>
        </strong>
      </div>
      <div class="panel-body">
        <table id="detailTable" class="table table-bordered">
          <thead>
            <tr>
              <th class="text-center" style="width: 50px;">#</th>
              <th>Invoice</th>
              <th>Part Number</th>
              <th>Quantity</th>
              <th>Price</th>
              <th>Total</th>
            </tr>
          </thead>
          <tbody>
            <?php
            $no = 1;
            $total_order_price = 0;
            foreach ($all_details as $item):
              $item_total_price = ($item['discounted_price'] == 0 ? $item['original_price'] : $item['discounted_price']) * $item['quantity'];
              $total_order_price += $item_total_price;
            ?>
              <tr>
                <td class="text-center"><?php echo $no++; ?></td>
                <td><?php echo $item['order_sn']; ?></td>
                <td><?php echo $item['product_name']; ?></td>
                <td><?php echo $item['quantity']; ?></td>
                <td>
                  <?php echo formatCurrency($item_total_price); ?>
                </td>
                <td><?php echo formatCurrency($item_total_price); ?></td>
              </tr>
            <?php endforeach; ?>
          </tbody>
          <tfoot>
            <tr>
              <th colspan="5" class="text-center">Total Price</th>
              <th><?php echo formatCurrency($total_order_price); ?></th>
            </tr>
          </tfoot>
        </table>
      </div>
    </div>
  </div>
</div>

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