<?php 

$aserver = mysqli_query($con,"SELECT * FROM master_topologi WHERE id = '$page[1]'");
$server = mysqli_fetch_assoc($aserver);

list($latitude, $longitude) = explode(',', $server['titik_koordinat']);

?>
<section class="content">
  <div class="container-fluid">
    <div class="row">
      <div class="col-12">
        <div class="card">
          <div class="card-header bg-secondary">
            <div class="row">
              <div class="col-sm-6">
                <h3 class="card-title">Edit Topologi => <?php echo $server['nama']; ?></h3>
              </div>
              <div class="col-sm-6">
                <ol class="float-sm-right mb-0">
                </ol>
              </div>
            </div>
          </div>
          <form method="post">
            <div class="card-body">
              <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 if($server['id_data_server'] == $value['id']) { echo "selected"; } ?> ><?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" value="<?php echo $server['nama']; ?>" required="required">
                  </div>
                  <div class="form-group">
                    <label>Jumlah Port</label>
                    <input type="text" class="form-control" name="jumlah" value="<?php echo $server['jumlah_port']; ?>" 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" value="<?php echo $latitude; ?>" required="required">
                    <input type="hidden" id="longitude" data-maps="longitude" name="longitude" value="<?php echo $longitude; ?>" required="required">
                    <div id="map" style="height:250px;margin-top: 10px;"></div>
                  </div>
                </div>
              </div>
            </div>
            <div class="card-footer">
              <input type="submit" name="edit" class="btn btn-secondary btn-sm" value="Simpan">
            </div>
          </form>
        </div>
      </div>
    </div>
  </div>
</section>
<?php 

if (isset($_POST['edit'])) {
    try {
        // Prepare the SQL statement
        $stmt = $pdo->prepare("UPDATE master_topologi SET 
            id_data_server = :id_data_server, 
            nama = :nama,
            titik_koordinat = :titik_koordinat,
            jumlah_port = :jumlah_port
            WHERE id = :id");

        // 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']);
        $stmt->bindParam(':id', $page[1]); // assuming $page[1] is the ID you want to update

        // Execute the statement
        if ($stmt->execute()) {
            sweetAlert("Data telah diubah", "success", "?".encrypt_url('master_topologi'));
        } else {
            sweetAlert("Gagal mengubah data", "error");
        }
    } catch (PDOException $e) {
        // Handle any errors
        sweetAlert("Terjadi kesalahan: " . $e->getMessage(), "error");
    }
}


?>