<?php
include("../../includes/config.php"); // or correct path


global $conn, $gsi;

$workOrderss = [];

$query = "
SELECT 
    m.work_order_id,
    m.part_id,
    m.end_date,
    m.machine_id,
    m.quantity,
    m.total_hours,
    m.id,
    w.work_order_name
FROM 
    manufacture_machine_table m
LEFT JOIN 
    work_order_name w 
    ON m.work_order_id = w.work_order_id 
    AND m.part_id = w.part_id
JOIN 
    station_detail_table s 
    ON m.machine_id = s.id
WHERE 
    s.machine_type = 4
    AND m.in_scheduler_check = 0
";

$workOrderQuery = mysqli_query($conn, $query);
?>
<table class="table table-striped">
  <thead>
    <tr>
      <th>WO Number</th>
      <th>Part Number</th>
      <th>Part Description</th>
      <th>Due Date</th>
      <th>Qty</th>
      <th>Std Hours</th>
      <th>Machine</th>
      <th>Table</th>
      <th>Select</th>
    </tr>
  </thead>
  <tbody>
    <?php while ($row = mysqli_fetch_assoc($workOrderQuery)): ?>
      <tr data-part-id="<?= htmlspecialchars($row['part_id']) ?>" data-wo-id="<?= $row['work_order_id'] ?>">
        <td><?= htmlspecialchars($row['work_order_name']) ?></td>
        <td><?= utility::getTableFieldById("item_management", "part_number", "id", $row['part_id']); ?></td>
        <td><?= utility::getTableFieldById("item_management", "product_description", "id", $row['part_id']); ?></td>
        <td><?= htmlspecialchars($row['end_date']) ?></td>
        <td><?= htmlspecialchars($row['quantity']) ?></td>
        <td><?= htmlspecialchars($row['total_hours']) ?></td>
        <td><?= $gsi->getpuStationList("machinee_id_" . $row['id'], $row['machine_id'], "100%", false, "form-control select2 machine-select", ""); ?></td>
        <td><?= $gsi->getPutableList("machine_table_id_" . $row['id'], $row['machine_id'], "100%", false, "form-control select2 machine-select", ""); ?></td>
        <td>
          <input class="form-check-input" 
                 type="checkbox" 
                 style="margin-left: 10px;" 
                 name="workOrder[]" 
                 value="<?= $row['work_order_id']; ?>" 
                 id="workOrder<?= $row['work_order_id']; ?>"
                 data-machine-id="<?= $row['machine_id']; ?>"
                 data-id="<?= $row['id']; ?>">
        </td>
      </tr>
    <?php endwhile; ?>
  </tbody>
</table>
<script>
$('.select2').select2({
  dropdownParent: $('#workOrderModal')
});
</script>
