<?php
require_once('includes/load.php');
page_require_level(1);

$id = (int) $_GET['id'];
$product = find_by_id('product_pricing_offline', $id);

if (isset($_POST['update'])) {
  $part_number = $db->escape($_POST['part_number']);
  $type_box_id = (int) $_POST['type_box_id'];
  $qty_per_box = (int) $_POST['qty_per_box'];
  $min_box = (int) $_POST['min_box'];
  $price = (float) $_POST['price'];

  $query = "UPDATE product_pricing_offline SET ";
  $query .= "part_number='{$part_number}', type_box_id='{$type_box_id}', qty_per_box='{$qty_per_box}', ";
  $query .= "min_box='{$min_box}', price='{$price}' WHERE id='{$id}'";

  if ($db->query($query)) {
    $session->msg('s', "Product Pricing berhasil diperbarui.");
    redirect('list_product_pricing.php', false);
  } else {
    $session->msg('d', "Gagal memperbarui Product Pricing.");
    redirect('list_product_pricing.php', false);
  }
}
?>

<?php include_once('layouts/header.php'); ?>
<form method="POST">
  <div class="form-group">
    <label>Part Number</label>
    <input type="text" name="part_number" class="form-control" value="<?php echo $product['part_number']; ?>" required>
  </div>
  <div class="form-group">
    <label>Type Box</label>
    <select name="type_box_id" class="form-control type_box" required>
      <option value="">Pilih Type Box</option>
      <?php
      $query = "SELECT * FROM type_box";
      $result = $db->query($query);
      while ($row = $result->fetch_assoc()) {
        if ($row['id'] == $product['type_box_id']) {
          echo "<option value='{$row['id']}' selected>{$row['name']}</option>";
        } else {
          echo "<option value='{$row['id']}'>{$row['name']}</option>";
        }
      }
      ?>
    </select>
  </div>
  <div class="form-group">
    <label>Qty per Box</label>
    <input type="number" name="qty_per_box" class="form-control" value="<?php echo $product['qty_per_box']; ?>"
      required>
  </div>
  <div class="form-group">
    <label>Min Box</label>
    <input type="number" name="min_box" class="form-control" value="<?php echo $product['min_box']; ?>" required>
  </div>
  <div class="form-group">
    <label>Price</label>
    <input type="number" step="0.01" name="price" class="form-control" value="<?php echo $product['price']; ?>"
      required>
  </div>
  <button type="submit" name="update" class="btn btn-primary">Update</button>
</form>
<?php include_once('layouts/footer.php'); ?>