 <!-- Main content -->
<section class="content" style="zoom: 90%;">
  <div class="container-fluid">
    <div class="row">
      <div class="col-12">
        <div class="card">
          <div class="card-header">
            <div class="row">
              <div class="col-sm-6">
                <h3 class="card-title">Barang di Gudang</h3>
              </div>
              <div class="col-sm-6">
                <ol class="float-sm-right mb-0">
                  <a type="button" class="btn btn-danger btn-sm m-1" data-toggle="modal" data-target="#staticBackdrop">Tambah Barang</a>
                  <a type="button" class="btn btn-success btn-sm m-1" data-toggle="modal" data-target="#staticTambah">Tambah Stok</a>
                  <a type="button" class="btn btn-warning btn-sm m-1" data-toggle="modal" data-target="#staticKurangi">Kurangi Stok</a>
                  <a type="button" class="btn btn-primary btn-sm m-1" data-toggle="modal" data-target="#staticReturn">Return Barang</a>
                </ol>
              </div>
              <!-- Modal Tambah -->
              <div class="modal fade" id="staticBackdrop" data-backdrop="static" data-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
                <div class="modal-dialog modal-xl">
                  <div class="modal-content">
                    <form method="post" enctype="multipart/form-data">
                      <div class="modal-header bg-danger">
                        <h5 class="modal-title" id="staticBackdropLabel">Tambah Barang</h5>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                          <span aria-hidden="true">&times;</span>
                        </button>
                      </div>
                      <div class="modal-body">
                        <div class="row">
                          <div class="card-body pb-0">
                            <div class="row">
                              <div class="col-md-6">
                                <div class="form-group">
                                    <label>Nama Server</label>
                                    <select class="form-control" name="ids" required="required">
                                        <option value="">Pilih</option>
                                        <?php 
                                        $List = new Listdata();
                                        $List->set_list("data_server", $SqlDataWH);
                                        foreach ($List->get_list() as $key => $value): ?>
                                            <option value="<?php echo $value['id']; ?>"><?php echo $value['nama_server']; ?></option>
                                        <?php endforeach ?>
                                    </select>
                                </div>
                                <div class="form-group">
                                  <label>Nama Barang</label>
                                  <input type="text" class="form-control" name="nama" required="required">
                                </div>
                                <div class="form-group">
                                  <label>Jenis Barang</label>
                                  <select class="form-control" name="jenis" required="required">
                                    <option value="">Pilih</option>
                                    <option value="Baru">Baru</option>
                                    <option value="Bekas">Bekas</option>
                                  </select>
                                </div>
                              </div>
                              <div class="col-md-6">
                                <div class="form-group">
                                  <label>Jumlah</label>
                                  <input type="text" class="form-control" name="jumlah" required="required">
                                </div>
                                <div class="form-group">
                                  <label>Keterangan</label>
                                  <textarea  class="form-control" name="keterangan" required="required" rows="5"></textarea>
                                </div>
                              </div>
                            </div>
                          </div>
                        </div>
                      </div>
                      <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                        <input type="submit" name="tambah" class="btn btn-primary" value="Tambah">
                      </div>
                    </form>
                    <?php
                   if (isset($_POST['tambah'])) {
                        $waktu = date('Y-m-d H:i:s');

                        $sql = "INSERT INTO gudang (id_data_server, id_user_akses, start_time, nama, status, jenis, jumlah, keterangan) 
                                VALUES (:ids, :userId, :waktu, :nama, 'masuk', :jenis, :jumlah, :keterangan)";

                        try {
                            // Prepare the statement
                            $stmt = $pdo->prepare($sql);

                            // Bind the parameters
                            $stmt->bindParam(':ids', $_POST['ids']);
                            $stmt->bindParam(':userId', $UserId); // Ensure $UserId is defined and properly initialized
                            $stmt->bindParam(':waktu', $waktu);
                            $stmt->bindParam(':nama', $_POST['nama']);
                            $stmt->bindParam(':jenis', $_POST['jenis']);
                            $stmt->bindParam(':jumlah', $_POST['jumlah']);
                            $stmt->bindParam(':keterangan', $_POST['keterangan']);

                            // Execute the statement
                            $stmt->execute();

                            // Success message
                            sweetAlert("Barang telah di tambah", "success");

                        } catch (PDOException $e) {
                            // Handle error
                            echo 'Error: ' . $e->getMessage();
                        }
                    }
                    ?>
                  </div>
                </div>
              </div>
              <!-- END Modal Tambah -->
              <!-- Modal Tambah Stok -->
              <div class="modal fade" id="staticTambah" data-backdrop="static" data-keyboard="false" tabindex="-1" aria-labelledby="staticTambahLabel" aria-hidden="true">
                <div class="modal-dialog modal-xl">
                  <div class="modal-content">
                    <form method="post" enctype="multipart/form-data">
                      <div class="modal-header bg-success">
                        <h5 class="modal-title" id="staticBackdropLabel">Tambah Stok Barang</h5>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                          <span aria-hidden="true">&times;</span>
                        </button>
                      </div>
                      <div class="modal-body">
                        <div class="row">
                          <div class="card-body pb-0">
                            <div class="row">
                              <div class="col-md-6">
                                <div class="form-group">
                                    <label>Nama Server</label>
                                    <select class="form-control dropoption1" name="ids" required="required">
                                        <option value="">Pilih</option>
                                        <?php 
                                        $List = new Listdata();
                                        $List->set_list("data_server", $SqlDataWH);
                                        foreach ($List->get_list() as $key => $value): ?>
                                            <option value="<?php echo $value['id']; ?>"><?php echo $value['nama_server']; ?></option>
                                        <?php endforeach ?>
                                    </select>
                                </div>
                                <div class="form-group">
                                    <label>Nama Barang</label>
                                    <select class="form-control dropoption2" name="nama" required="required">
                                        <option value="">Pilih</option>
                                    </select>
                                </div>
                                <div class="form-group">
                                  <label>Jenis Barang</label>
                                  <select class="form-control" name="jenis" required="required">
                                    <option value="">Pilih</option>
                                    <option value="Baru">Baru</option>
                                    <option value="Bekas">Bekas</option>
                                  </select>
                                </div>
                              </div>
                              <div class="col-md-6">
                                <div class="form-group">
                                  <label>Jumlah</label>
                                  <input type="text" class="form-control" name="jumlah" required="required">
                                </div>
                                <div class="form-group">
                                  <label>Keterangan</label>
                                  <textarea  class="form-control" name="keterangan" required="required" rows="5"></textarea>
                                </div>
                              </div>
                            </div>
                          </div>
                        </div>
                      </div>
                      <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                        <input type="submit" name="tambahstok" class="btn btn-primary" value="Tambah">
                      </div>
                    </form>
                    <?php
                   if (isset($_POST['tambahstok'])) {
                        $waktu = date('Y-m-d H:i:s');

                        $sql = "INSERT INTO gudang (id_data_server, id_user_akses, start_time, nama, status, jenis, jumlah, keterangan) 
                                VALUES (:ids, :userId, :waktu, :nama, 'masuk', :jenis, :jumlah, :keterangan)";

                        try {
                            // Prepare the statement
                            $stmt = $pdo->prepare($sql);

                            // Bind the parameters
                            $stmt->bindParam(':ids', $_POST['ids']);
                            $stmt->bindParam(':userId', $UserId); // Ensure $UserId is defined and properly initialized
                            $stmt->bindParam(':waktu', $waktu);
                            $stmt->bindParam(':nama', $_POST['nama']);
                            $stmt->bindParam(':jenis', $_POST['jenis']);
                            $stmt->bindParam(':jumlah', $_POST['jumlah']);
                            $stmt->bindParam(':keterangan', $_POST['keterangan']);

                            // Execute the statement
                            $stmt->execute();

                            // Success message
                            sweetAlert("Stok ".$_POST['nama']." telah bertambah ".$_POST['jumlah'], "success");

                        } catch (PDOException $e) {
                            // Handle error
                            echo 'Error: ' . $e->getMessage();
                        }
                    }
                    ?>
                  </div>
                </div>
              </div>
              <!-- END Modal Tambah Stok -->
              <!-- Modal Kurangi  Stok -->
              <div class="modal fade" id="staticKurangi" data-backdrop="static" data-keyboard="false" tabindex="-1" aria-labelledby="staticTambahLabel" aria-hidden="true">
                <div class="modal-dialog modal-xl">
                  <div class="modal-content">
                    <form method="post" enctype="multipart/form-data">
                      <div class="modal-header bg-warning">
                        <h5 class="modal-title" id="staticBackdropLabel">Kurangi Stok Barang</h5>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                          <span aria-hidden="true">&times;</span>
                        </button>
                      </div>
                      <div class="modal-body">
                        <div class="row">
                          <div class="card-body pb-0">
                            <div class="row">
                              <div class="col-md-6">
                                <div class="form-group">
                                    <label>Nama Server</label>
                                    <select class="form-control dropoption1" name="ids" required="required">
                                        <option value="">Pilih</option>
                                        <?php 
                                        $List = new Listdata();
                                        $List->set_list("data_server", $SqlDataWH);
                                        foreach ($List->get_list() as $key => $value): ?>
                                            <option value="<?php echo $value['id']; ?>"><?php echo $value['nama_server']; ?></option>
                                        <?php endforeach ?>
                                    </select>
                                </div>
                                <div class="form-group">
                                    <label>Nama Barang</label>
                                    <select class="form-control dropoption2" name="nama" required="required">
                                        <option value="">Pilih</option>
                                    </select>
                                </div>
                                <div class="form-group">
                                  <label>Jenis Barang</label>
                                  <select class="form-control" name="jenis" required="required">
                                    <option value="">Pilih</option>
                                    <option value="Baru">Baru</option>
                                    <option value="Bekas">Bekas</option>
                                  </select>
                                </div>
                              </div>
                              <div class="col-md-6">
                                <div class="form-group">
                                  <label>Jumlah</label>
                                  <input type="text" class="form-control" name="jumlah" required="required">
                                </div>
                                <div class="form-group">
                                  <label>Keterangan</label>
                                  <textarea  class="form-control" name="keterangan" required="required" rows="5"></textarea>
                                </div>
                              </div>
                            </div>
                          </div>
                        </div>
                      </div>
                      <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                        <input type="submit" name="kurangi" class="btn btn-primary" value="Tambah">
                      </div>
                    </form>
                    <?php
                   if (isset($_POST['kurangi'])) {
                        $waktu = date('Y-m-d H:i:s');

                        $sql = "INSERT INTO gudang (id_data_server, id_user_akses, start_time, nama, status, jenis, jumlah, keterangan) 
                                VALUES (:ids, :userId, :waktu, :nama, 'keluar', :jenis, :jumlah, :keterangan)";

                        try {
                            // Prepare the statement
                            $stmt = $pdo->prepare($sql);

                            // Bind the parameters
                            $stmt->bindParam(':ids', $_POST['ids']);
                            $stmt->bindParam(':userId', $UserId); // Ensure $UserId is defined and properly initialized
                            $stmt->bindParam(':waktu', $waktu);
                            $stmt->bindParam(':nama', $_POST['nama']);
                            $stmt->bindParam(':jenis', $_POST['jenis']);
                            $stmt->bindParam(':jumlah', $_POST['jumlah']);
                            $stmt->bindParam(':keterangan', $_POST['keterangan']);

                            // Execute the statement
                            $stmt->execute();

                            // Success message
                            sweetAlert("Stok ".$_POST['nama']." telah bertambah ".$_POST['jumlah'], "success");

                        } catch (PDOException $e) {
                            // Handle error
                            echo 'Error: ' . $e->getMessage();
                        }
                    }
                    ?>
                  </div>
                </div>
              </div>
              <!-- END Modal Kurangi Stok -->
              <!-- Modal Return  Stok -->
              <div class="modal fade" id="staticReturn" data-backdrop="static" data-keyboard="false" tabindex="-1" aria-labelledby="staticTambahLabel" aria-hidden="true">
                <div class="modal-dialog modal-xl">
                  <div class="modal-content">
                    <form method="post" enctype="multipart/form-data">
                      <div class="modal-header bg-primary">
                        <h5 class="modal-title" id="staticBackdropLabel">Return Barang</h5>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                          <span aria-hidden="true">&times;</span>
                        </button>
                      </div>
                      <div class="modal-body">
                        <div class="row">
                          <div class="card-body pb-0">
                            <div class="row">
                              <div class="col-md-6">
                                <div class="form-group">
                                    <label>Nama Server</label>
                                    <select class="form-control dropoption1" name="ids" required="required">
                                        <option value="">Pilih</option>
                                        <?php 
                                        $List = new Listdata();
                                        $List->set_list("data_server", $SqlDataWH);
                                        foreach ($List->get_list() as $key => $value): ?>
                                            <option value="<?php echo $value['id']; ?>"><?php echo $value['nama_server']; ?></option>
                                        <?php endforeach ?>
                                    </select>
                                </div>
                                <div class="form-group">
                                    <label>Nama Barang</label>
                                    <select class="form-control dropoption2" name="nama" required="required">
                                        <option value="">Pilih</option>
                                    </select>
                                </div>
                                <div class="form-group">
                                  <label>Jenis Barang</label>
                                  <select class="form-control" name="jenis" required="required">
                                    <option value="">Pilih</option>
                                    <option value="Baru">Baru</option>
                                    <option value="Bekas">Bekas</option>
                                  </select>
                                </div>
                              </div>
                              <div class="col-md-6">
                                <div class="form-group">
                                  <label>Jumlah</label>
                                  <input type="text" class="form-control" name="jumlah" required="required">
                                </div>
                                <div class="form-group">
                                  <label>Keterangan</label>
                                  <textarea  class="form-control" name="keterangan" required="required" rows="5"></textarea>
                                </div>
                              </div>
                            </div>
                          </div>
                        </div>
                      </div>
                      <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                        <input type="submit" name="return" class="btn btn-primary" value="Return">
                      </div>
                    </form>
                    <?php
                   if (isset($_POST['return'])) {
                        $waktu = date('Y-m-d H:i:s');

                        $sql = "INSERT INTO gudang (id_data_server, id_user_akses, start_time, nama, status, jenis, jumlah, keterangan) 
                                VALUES (:ids, :userId, :waktu, :nama, 'return', :jenis, :jumlah, :keterangan)";

                        try {
                            // Prepare the statement
                            $stmt = $pdo->prepare($sql);

                            // Bind the parameters
                            $stmt->bindParam(':ids', $_POST['ids']);
                            $stmt->bindParam(':userId', $UserId); // Ensure $UserId is defined and properly initialized
                            $stmt->bindParam(':waktu', $waktu);
                            $stmt->bindParam(':nama', $_POST['nama']);
                            $stmt->bindParam(':jenis', $_POST['jenis']);
                            $stmt->bindParam(':jumlah', $_POST['jumlah']);
                            $stmt->bindParam(':keterangan', $_POST['keterangan']);

                            // Execute the statement
                            $stmt->execute();

                            // Success message
                            sweetAlert("Stok ".$_POST['nama']." telah bertambah ".$_POST['jumlah'], "success");

                        } catch (PDOException $e) {
                            // Handle error
                            echo 'Error: ' . $e->getMessage();
                        }
                    }
                    ?>
                  </div>
                </div>
              </div>
              <!-- END Modal Return Stok -->
            </div>
          </div>
          <!-- /.card-header -->
          <div class="card-body">
            <table id="example1" class="table table-bordered table-striped">
              <thead>
              <tr>
                <th>No</th>
                <th>Nama Barang</th>
                <th>Barang Masuk</th>
                <th>Barang Keluar</th>
                <th>Sisa Barang</th>
                <th>Nama Server</th>
                <th>Action</th>
              </tr>
              </thead>
              <tbody>
              <?php
                $no = 1;
                $aserver = mysqli_query($con,"SELECT * FROM v_jumlah_data_gudang_lama $SqlDataWHId");
                while ($server = mysqli_fetch_assoc($aserver)) 
                {
              ?>
              <tr>
                <td style="max-width: 30px;"><?php echo $no; ?></td>
                <td style="max-width: 200px;"><?php echo $server['nama']; ?></td>
                <td style="max-width: 200px;"><?php echo $server['masuk']; ?></td>
                <td style="max-width: 200px;"><?php echo $server['keluar']; ?></td>
                <td style="max-width: 200px;"><?php echo $server['sisa']; ?></td>
                <td style="max-width: 30px;"><?php echo $server['nama_server']; ?></td>
                <td>
                  <a href="?<?php echo encrypt_url('barang_gudang_detail|'.$server['id_data_server'].'|'.$server['nama']); ?>" class="btn btn-info btn-sm">Detail</a>
                </td>
              </tr>
            <?php $no ++; } ?>
              </tbody>
            </table>
          </div>
          <!-- /.card-body -->
        </div>
        <!-- /.card -->
      </div>
      <!-- /.col -->
    </div>
    <!-- /.row -->
  </div>
  <!-- /.container-fluid -->
</section>
<!-- /.content -->
<script>
document.addEventListener('DOMContentLoaded', function() {
    // Fungsi untuk mengupdate dropdown
    function updateDropdown(dropdown, data) {
        const firstOption = dropdown.querySelector('option[value=""]');

        // Hapus semua opsi kecuali opsi pertama
        dropdown.innerHTML = '';
        if (firstOption) {
            dropdown.appendChild(firstOption.cloneNode(true));
        } else {
            dropdown.innerHTML = '<option value="">Pilih</option>';
        }

        // Tambahkan opsi baru dari data server
        data.forEach(option => {
            const optionElement = document.createElement('option');
            optionElement.value = option.nama;
            optionElement.textContent = option.nama; // Pastikan label sesuai dengan data dari server
            dropdown.appendChild(optionElement);
        });
    }

    // Fungsi untuk mengupdate dropdown2 berdasarkan pilihan dari dropdown1
    function updateDropdown2(form, selectedOption) {
        const dropoption2 = form.querySelector('.dropoption2');
        
        fetch(`list_data/getItemBarang.php?dropdown=dropoption2&option=${encodeURIComponent(selectedOption)}`)
            .then(response => response.json())
            .then(data => updateDropdown(dropoption2, data))
            .catch(error => console.error('Error fetching dropoption2 data:', error));
    }

    // Menyiapkan dropdown ketika modal dibuka
    document.querySelectorAll('.modal').forEach(modal => {
        $(modal).on('shown.bs.modal', function () {
        });
    });

    // Event listener untuk dropdown1
    document.addEventListener('change', function(e) {
        if (e.target.classList.contains('dropoption1')) {
            const form = e.target.closest('form');
            updateDropdown2(form, e.target.value);
        }
    });
});
</script>