<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">Master Topologi</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 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">Tambah ODP / Port</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="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">
                                  <?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 Topologi ODP</label>
                                <input type="text" class="form-control" name="nama" required="required">
                              </div>
                              <div class="form-group">
                                <label>Jumlah Port</label>
                                <input type="text" class="form-control" name="jumlah" required="required">
                              </div>
                            </div>
                            <div class="col-md-6">
                              <div class="form-group" >
                                <label>Pilih Lokasi Maps / Isi Koordinat</label>
                                <input class="form-control" type="text" id="latlong" required name="koordinat">
                                <i class="text-danger"><small>Anda bisa pilih salah satu untuk mengisi bagian ini</small></i>
                                <!-- Input hidden untuk latitude dan longitude -->
                                <input type="hidden" id="latitude" data-maps="latitude" name="latitude" required="required">
                                <input type="hidden" id="longitude" data-maps="longitude" name="longitude" required="required">
                                <div id="map" style="height:250px;margin-top: 10px;"></div>
                              </div>
                            </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 {
                          // Prepare the SQL statement
                          $stmt = $pdo->prepare("INSERT INTO master_topologi (id_data_server, nama, titik_koordinat, jumlah_port) VALUES (:id_data_server, :nama, :titik_koordinat, :jumlah_port)");

                          // Bind parameters
                          $stmt->bindParam(':id_data_server', $_POST['ids']);
                          $stmt->bindParam(':nama', $_POST['nama']);
                          $stmt->bindParam(':titik_koordinat', $_POST['koordinat']);
                          $stmt->bindParam(':jumlah_port', $_POST['jumlah']);

                          // Execute the statement
                          if ($stmt->execute()) {
                              sweetAlert("Data telah ditambah", "success");
                          } else {
                              sweetAlert("Gagal menambah data", "error");
                          }
                      } catch (PDOException $e) {
                          // Handle any errors
                          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>Topologi</th>
                <th>Koordinat</th>
                <th>Jumlah Port</th>
                <th>Port Terpakai</th>
                <th>Port Tersedia</th>
                <th>Action</th>
              </tr>
              </thead>
              <tbody>
              <?php
                $no = 1;
                $aserver = mysqli_query($con,"SELECT * FROM v_master_topologi $SqlDataWHId ORDER BY nama_server");
                while ($server = mysqli_fetch_assoc($aserver)) 
                {
              ?>
              <tr>
                <td><?php echo $no; ?></td>
                <td><?php echo $server['nama_server']; ?></td>
                <td><?php echo $server['nama']; ?></td>
                <td><?php echo $server['titik_koordinat']; ?></td>
                <td><?php echo $server['jumlah_port']; ?></td>
                <td><?php echo $server['jumlah_terpakai']; ?></td>
                <td><?php echo $server['sisa_port']; ?></td>
                <td>
                  <div class="row">
                    <form method="post">
                      <?php if(in_array("Ubah", $json_array[25]['sub_menu'])) { ?>
                      <a href="?<?php echo encrypt_url('master_topologi_edit|'.$server['id']); ?>" class="btn btn-secondary btn-sm ml-1">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>
                  </div>
                </td>
              </tr>
              <?php
              
              if (isset($_POST['hapus'.$server['id']]))
              {
                $iddata = $server['id'];
                deleteAlert("master_topologi",$iddata );
              }
            
              $no++; } ?>
              </tbody>
            </table>
          </div>
        </div>
      </div>
    </div>
  </div>
</section>
