 <!-- Main content -->
<section class="content">
  <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">List Data Perangkat</h3>
              </div>
              <div class="col-sm-6">
                <ol class="float-sm-right mb-0">
                  <?php if(in_array("Tambah", $json_array[25]['sub_menu'])) { ?>
                  <a type="button" class="btn btn-danger btn-sm" data-toggle="modal" data-target="#staticBackdrop">Tambah</a> <?php } ?>
                </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">
                  <div class="modal-content">
                    <form method="post" enctype="multipart/form-data">
                      <div class="modal-header bg-primary">
                        <h5 class="modal-title" id="staticBackdropLabel">Tambah Perangkat</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="form-group">
                              <label>Nama Server</label>
                              <select class="form-control" name="ids">
                                <?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>Jenis Remote</label>
                              <select class="form-control" name="jenis" required="required">
                                <option value="">Pilih</option>
                                <?php 
                                $perangkat = [['n' => 'Web Config'],['n' => 'Winbox'],['n' => 'Telnet']]; 
                                foreach ($perangkat as $data) {
                                  echo '<option value="'.$data['n'].'">'.$data['n'].'</option>';
                                }
                                ?>
                              </select>
                            </div>
                            <div class="form-group">
                              <label>Nama Perangkat</label>
                              <input type="text" class="form-control" name="nama" required="required">
                            </div>
                            <div class="form-group">
                              <label>Alamat / Link Perangkat</label>
                              <input type="text" class="form-control" name="alamat" required="required" placeholder="103.100.60.99:8080 atau https://remote.domian.com:8800">
                            </div>
                          </div>
                        </div>
                      </div>
                      <?php if(in_array("Tambah", $json_array[25]['sub_menu'])) { ?>
                      <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>
                    <?php } ?>
                    </form>
                    <?php
                    if (isset($_POST['tambah'])) {
                      try {
                          // Siapkan query
                          $query = "INSERT INTO master_perangkat (id_data_server, nama, jenis, alamat_perangkat) VALUES (:id_data_server, :nama, :jenis, :alamat_perangkat)";
                          $stmt = $pdo->prepare($query);

                          // Bind parameter
                          $stmt->bindParam(':id_data_server', $_POST['ids']);
                          $stmt->bindParam(':nama', $_POST['nama']);
                          $stmt->bindParam(':jenis', $_POST['jenis']);
                          $stmt->bindParam(':alamat_perangkat', $_POST['alamat']);

                          // Eksekusi query
                          $stmt->execute();

                          // Tampilkan pesan sukses
                          sweetAlert("Data telah di tambah", "success");
                      } catch (PDOException $e) {
                          // Tampilkan pesan kesalahan
                          sweetAlert("Terjadi kesalahan: " . $e->getMessage(), "error");
                      }
                    }
                    ?>
                  </div>
                </div>
              </div>
              <!-- END Modal Tambah -->
            </div>
          </div>
          <!-- /.card-header -->
          <div class="card-body">
            <table id="example2" class="table table-bordered table-striped">
              <thead>
              <tr>
                <th>No</th>
                <th>Nama Server</th>
                <th>Nama Perangkat</th>
                <th>Jenis</th>
                <th>Action</th>
              </tr>
              </thead>
              <tbody>
              <?php
                $no = 1;
                $aserver = mysqli_query($con,"SELECT * FROM master_perangkat $SqlDataWHId");
                while ($server = mysqli_fetch_assoc($aserver)) 
                {
              ?>
              <tr>
                <td><?php echo $no; ?></td>
                <td>
                  <?php 
                    $datasv = explode("|", $server['id_data_server']);
                    foreach ($datasv as $isidata)
                    {
                      $asv = mysqli_query($con,"SELECT nama_server FROM data_server WHERE id = '$isidata'");
                      $sv = mysqli_fetch_assoc($asv);
                      echo $sv['nama_server'];
                    } 
                  ?>
                </td>
                <td><?php echo $server['nama']; ?></td>
                <td><?php echo $server['jenis']; ?></td>
                <td>
                  <form method="post">
                    <a href="<?php echo $server['alamat_perangkat']; ?>" target="_blank" class="btn btn-success btn-sm ml-1">Buka Perangkat</a>
                    <!-- <a href="C:/Users/USER/Desktop/winbox.exe" target="_blank">Buka Aplikasi</a> -->
                    <?php if(in_array("Ubah", $json_array[25]['sub_menu'])) { ?>
                    <a type="button" class="btn btn-secondary btn-sm ml-1" data-toggle="modal" data-target="#modal-edit<?php echo $server['id'];?>">Edit</a>
                    <?php } if(in_array("Hapus", $json_array[25]['sub_menu'])) { ?>
                    <input type="submit" name="hapus<?php echo $server['id'];?>" value="Hapus" class="btn btn-danger btn-sm ml-1" />
                    <?php } ?>
                  </form>
                </td>
              </tr>
              <!-- Modal Edit -->
              <div class="modal fade" id="modal-edit<?php echo $server['id'];?>" data-backdrop="static" data-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
                <div class="modal-dialog modal-md">
                  <div class="modal-content">
                    <form method="post" enctype="multipart/form-data">
                      <div class="modal-header bg-secondary">
                        <h5 class="modal-title" id="staticBackdropLabel">Edit Master Desa</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="form-group">
                              <label>Nama Server</label>
                              <select class="form-control" name="ids">
                                <?php 
                                $List = new Listdata();
                                $List->set_list("data_server", $SqlDataWH);
                                foreach ($List->get_list() as $key => $value): ?>
                                <option value="<?php echo $value['id']; ?>" <?php if($value['id'] == $server['id_data_server']) { echo "selected";} ?> ><?php echo $value['nama_server']; ?></option>
                                <?php endforeach ?>
                              </select>
                            </div>
                            <div class="form-group">
                              <label>Jenis Remote</label>
                              <select class="form-control" name="jenis" required="required">
                                <option value="">Pilih</option>
                                <?php 
                                $perangkat = [['n' => 'Web Config'],['n' => 'Winbox'],['n' => 'Telnet']]; 
                                foreach ($perangkat as $data) {
                                  $selected = ($server['jenis'] == $data['n']) ? 'selected' : '';
                                  echo '<option value="'.$data['n'].'" '.$selected.'>'.$data['n'].'</option>';
                                }
                                ?>
                              </select>
                            </div>
                            <div class="form-group">
                              <label>Nama Perangkat</label>
                              <input type="text" class="form-control" value="<?php echo $server['nama']; ?>" name="nama" required="required">
                            </div>
                            <div class="form-group">
                              <label>Alamat / Link Perangkat</label>
                              <input type="text" class="form-control" value="<?php echo $server['alamat_perangkat']; ?>" name="alamat" required="required">
                            </div>
                          </div>
                        </div>
                      </div>
                      <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                        <input type="submit" name="edit<?php echo $server['id']; ?>" class="btn btn-warning" value="Edit">
                      </div>
                    </form>
                    <?php
                    if (isset($_POST['edit'.$server['id']])) 
                    {
                      try {
                          // Siapkan query
                          $query = "UPDATE master_perangkat SET id_data_server = :id_data_server, nama = :nama, jenis = :jenis, alamat_perangkat = :alamat_perangkat WHERE id = :id";
                          $stmt = $pdo->prepare($query);

                          // Bind parameter
                          $stmt->bindParam(':id_data_server', $_POST['ids']);
                          $stmt->bindParam(':nama', $_POST['nama']);
                          $stmt->bindParam(':jenis', $_POST['jenis']);
                          $stmt->bindParam(':alamat_perangkat', $_POST['alamat']);
                          $stmt->bindParam(':id', $server['id']);

                          // Eksekusi query
                          $stmt->execute();

                          // Tampilkan pesan sukses
                          sweetAlert("Data telah di ubah", "success");
                      } catch (PDOException $e) {
                          // Tampilkan pesan kesalahan
                          sweetAlert("Terjadi kesalahan: " . $e->getMessage(), "error");
                      }
                    }
                    if (isset($_POST['hapus'.$server['id']]))
                    {
                      $iddata = $server['id'];
                      deleteAlert("master_perangkat",$iddata );
                    } 
                    ?>
                  </div>
                </div>
              </div>
              <!-- END Modal Edit -->
            <?php $no ++; } ?>
              </tbody>
            </table>
          </div>
          <!-- /.card-body -->
        </div>
        <!-- /.card -->
      </div>
      <!-- /.col -->
    </div>
    <!-- /.row -->
  </div>
  <!-- /.container-fluid -->
</section>
<!-- /.content -->
