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

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $partNumbers = $_POST['part_number'];
    $quantities = $_POST['quantity'];

    if (!empty($partNumbers) && !empty($quantities)) {
        // Lakukan validasi atau manipulasi data jika diperlukan

        // Simpan data ke tabel MRP
        foreach ($partNumbers as $key => $partNumber) {
            $partNumber = $db->escape($partNumber);
            $quantity = (int)$quantities[$key];

            $sql = "INSERT INTO mrp (part_number, required_quantity) VALUES ('$partNumber', $quantity)";
            $db->query($sql);
        }

        // Pesan sukses untuk pop-up
        $popupMessage = "MRP data has been saved successfully!";
        $popupType = "success";
    } else {
        // Pesan error untuk pop-up
        $popupMessage = "Failed to save MRP data. Please check your inputs.";
        $popupType = "danger";
    }
}
?>

<!DOCTYPE html>
<html>

<head>
  <title>MRP Save Result</title>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <!-- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> -->
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <script>
  $(document).ready(function() {
    // Tampilkan pop-up sesuai pesan dan tipe
    $('#messageModal').modal('show');
    // Setelah beberapa detik, alihkan ke input_mrp.php
    setTimeout(function() {
      window.location.href = 'input_mrp.php';
    }, 3000); // Ubah angka 3000 ke jumlah milidetik yang diinginkan
  });
  </script>
</head>

<body>

  <!-- Modal Pop-up untuk Pesan -->
  <div id="messageModal" class="modal fade" tabindex="-1" role="dialog">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h4 class="modal-title">MRP Save Result</h4>
        </div>
        <div class="modal-body">
          <div class="alert alert-<?php echo $popupType; ?>">
            <?php echo $popupMessage; ?>
          </div>
        </div>
      </div>
    </div>
  </div>

</body>

</html>