<?php
require_once('includes/load.php');
page_require_level(1);
$page_title = 'List Pricing Product Offline';
$sql = "select product_pricing_offline.*, type_box.name as type_box_name from product_pricing_offline join type_box on type_box.id = product_pricing_offline.type_box_id";
$data = $db->query($sql);

// cek and remove flush msg
if (isset($_SESSION['flush_msg'])) {
  $msg = $_SESSION['flush_msg'];
  unset($_SESSION['flush_msg']);
}

// Delete a record if requested
if (isset($_GET['delete'])) {
  $ok = delete_by_id('product_pricing_offline', $_GET['delete']);
  $msg = (!empty($ok) ? 'Berhasil' : 'Gagal') . ' hapus : ' . $_GET['delete_part'];
  $_SESSION['flush_msg'] = [(!empty($ok) ? 'success' : 'danger') => $msg];
  redirect('list_product_pricing.php');
}

// Cek apakah form telah disubmit
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['create'])) {
  // Ambil data dari form
  $part_number = $_POST['part_number'];
  $type_box_id = intval($_POST['type_box_id']);
  $qty_per_box = intval($_POST['qty_per_box']);
  $min_box = intval($_POST['min_box']);
  $price = floatval($_POST['price']) ?? 0;

  // Validasi data (opsional, tambahkan validasi sesuai kebutuhan)
  if (!empty($part_number) && $type_box_id > 0 && $qty_per_box > 0 && $min_box > 0) {
    // Query untuk menyimpan data ke database
    $query = "INSERT INTO product_pricing_offline (part_number, type_box_id, qty_per_box, min_box, price) 
                VALUES ('$part_number', $type_box_id, $qty_per_box, $min_box, $price)";

    if ($db->query($query)) {
      $msg = ['success' => 'Data berhasil ditambahkan! : ' . $part_number];
    } else {
      $msg = ['danger' => "Gagal menambahkan data ({$part_number}) : " . $db->error];
    }
  } else {
    $msg = ['danger' => 'Harap isi semua data dengan benar!'];
  }

  $_SESSION['flush_msg'] = $msg;
  redirect('list_product_pricing.php');
}
?>

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

<div class="row">
  <div class="col-md-12">
    <?php echo display_msg_new($msg); ?>
  </div>
</div>
<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>Daftar Product Pricing</span>
        </strong>
        <a href="#" class="btn btn-sm btn-primary mb-4 pull-right" data-toggle="modal" data-target="#createModal">
          Tambah Product Pricing
        </a>
      </div>
      <div class="panel-body">

        <table class="table table-bordered">
          <thead>
            <tr>
              <th style="width: 25px">No</th>
              <th>Part Number</th>
              <th>Type Box</th>
              <th>Qty per Box</th>
              <th>Min Box</th>
              <th>Price</th>
              <th>Aksi</th>
            </tr>
          </thead>
          <tbody>
            <?php foreach ($data as $row): ?>
              <tr>
                <td><?php echo $row['id']; ?></td>
                <td><?php echo $row['part_number']; ?></td>
                <td><?php echo $row['type_box_name']; ?></td>
                <td><?php echo $row['qty_per_box']; ?></td>
                <td><?php echo $row['min_box']; ?></td>
                <td><?php echo number_format($row['price'], 0); ?></td>
                <td>
                  <a href="edit_product_pricing.php?id=<?php echo $row['id']; ?>" class="btn btn-warning btn-sm"><i
                      class="glyphicon glyphicon-pencil"></i></a>
                  <a href="?delete=<?php echo $row['id']; ?>&delete_part=<?= $row['part_number'] ?>" class="btn btn-danger btn-sm"
                    onclick="return confirm('Yakin ingin menghapus?')"><i class="glyphicon glyphicon-remove"></i></a>
                </td>
              </tr>
            <?php endforeach; ?>
          </tbody>
        </table>
      </div>
    </div>
  </div>
</div>

<!-- Modal Tambah -->
<div class="modal fade" id="createModal" tabindex="-1" aria-labelledby="createModalModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">Tambah Product Pricing</h5>
        <button type="button" class="close" data-dismiss="modal">&times;</button>
      </div>
      <div class="modal-body">
        <form method="POST" action="list_product_pricing.php">
          <div class="form-group">
            <label>Part Number</label>
            <input type="text" name="part_number" class="form-control" 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()) {
                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" required>
          </div>
          <div class="form-group">
            <label>Min Box</label>
            <input type="number" name="min_box" class="form-control" required>
          </div>
          <div class="form-group">
            <label>Price</label>
            <input type="number" step="0.01" name="price" class="form-control" required>
          </div>
          <button type="submit" name="create" class="btn btn-primary">Tambah</button>
        </form>
      </div>
    </div>
  </div>
</div>
<script>
  var table = $('.table').DataTable({});
</script>
<?php include_once('layouts/footer.php'); ?>