<?php
$date = date('Y-m-d H:i:s');
$db = mysqli_query($con,"SELECT * FROM order_list WHERE id = '$page[1]'");
$isi = mysqli_fetch_assoc($db); 
?>

<!-- Main content -->
<section class="content">
  <div class="container-fluid">
    <!-- general form elements -->
    <div class="card card-secondary">
      <div class="card-header">
        <h3 class="card-title">Ubah Data Order List</h3>
      </div>
      <form method="post" enctype="multipart/form-data">
        <div class="card-body">
          <div class="row">
            <div class="col-md-6">
              <center><label>INFORMASI LAYANAN</label></center>
              <div class="form-group m-1">
                <label>Pilih Server</label>
                <select id="dropdown0" class="form-control" required="required" name="ids">
                  <option value="">Pilih</option>
                  <?php 
                  $List->set_list("data_server", $SqlDataWH);
                  foreach ($List->get_list() as $key => $value): ?>
                    <option value="<?php echo htmlspecialchars($value['id']); ?>" <?php if($value['id'] == $isi['id_data_server']) { echo "selected";} ?> ><?php echo htmlspecialchars($value['nama_server']); ?></option>
                  <?php endforeach ?>
                </select>
              </div>
              <div class="form-group m-1">
                <label>Jenis Layanan</label>
                <select id="dropdown1" class="form-control" name="jenis">
                  <option value="PPPOE" <?php if($isi['jenis_layanan'] == 'PPPOE') { echo "selected";} ?>>PPPOE</option>
                  <option value="Hotspot" <?php if($isi['jenis_layanan'] == 'Hotspot') { echo "selected";} ?>>Hotspot</option>
                </select>
              </div>
              <div class="form-group m-1" id="group-dropdown2">
                <label>Paket Berlangganan</label>
                <select id="dropdown2" class="form-control select2bs4" required="required" name="idp" data-id="<?php echo $isi['id_profile_paket']; ?>">
                </select>
              </div>
              <div class="form-group m-1" id="group-dropdown3">
                <label>Marketing Area / Penagih</label>
                <select id="dropdown3" class="form-control select2bs4" required="required" name="idm" data-id="<?php echo $isi['id_marketing']; ?>">
                </select>
              </div>
              <hr><center><label>INFORMASI PRIBADI</label></center>
              <div class="form-group m-1">
                <label>NIK</label>
                <input type="text" class="form-control" name="nik" value="<?php echo $isi['nik']; ?>">
              </div>
              <div class="form-group m-1">
                <label>Nama</label>
                <input type="text" class="form-control" name="nama" value="<?php echo $isi['nama']; ?>">
              </div>
              <div class="form-group m-1">
                <label>Email</label>
                <input type="text" class="form-control" name="email" value="<?php echo $isi['email']; ?>">
              </div>
              <div class="form-group m-1">
                <label>Nomor WhatsApp</label>
                <input type="text" class="form-control" name="nowa" value="<?php echo $isi['nomor_whatsapp']; ?>">
              </div>
            </div>
            <div class="col-md-6">
              <div class="form-group m-1">
                <label>Alamat</label>
                <input type="text" class="form-control" name="alamat" value="<?php echo $isi['alamat']; ?>">
              </div>
              <div class="form-group m-1" id="group-dropdown4">
                <label>Desa</label>
                <select id="dropdown4" class="form-control select2bs4" required="required" name="desa" data-id="<?php echo $isi['id_master_desa']; ?>">
                </select>
              </div>
              <div class="form-group m-1">
                <label>Catatan</label>
                <textarea class="form-control" name="catatan" rows="3"><?php echo $isi['catatan']; ?></textarea>
                <i class="text-danger"><small>Isi jika di perlukan</small></i>
              </div>
              <div class="form-group m-1">
                <label>Pilih Lokasi Maps</label><input class="form-control" type="text" id="latlong" required>
                <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" value="<?php echo $isi['latitude']; ?>" name="latitude">
                <input type="hidden" id="longitude" data-maps="longitude" value="<?php echo $isi['longitude']; ?>" name="longitude">
                <div id="map" style="height:250px;margin-top: 10px;"></div>
              </div>
            </div>
          </div>
          <hr>
        </div>
        <div class="card-footer">
          <button type="submit" class="btn btn-primary btn-sm m-1" name="ubah">Ubah</button>
        </div>
      </form>
    </div>
  </div>
</section>
<?php
if (isset($_POST['ubah'])) {
    try {
      $cekmail = $pdo->prepare("SELECT id FROM pelanggan WHERE email = :email");
      $cekmail->execute([':email' => $_POST['email']]);

      if ($cekmail->rowCount() === 0) {
        // Siapkan statement untuk melakukan update
        $stmt = $pdo->prepare("UPDATE order_list SET 
            id_profile_paket  = :idp,
            id_data_server    = :ids,
            id_marketing      = :idm,
            jenis_layanan     = :jenis,
            nama              = :nama,
            nik               = :nik,
            alamat            = :alamat,
            id_master_desa    = :desa,
            latitude          = :latitude,
            longitude         = :longitude,
            nomor_whatsapp    = :nowa,
            email             = :email,
            catatan           = :catatan
            WHERE id = :id");

        // Bind parameter
        $stmt->bindParam(':idp', $_POST['idp']);
        $stmt->bindParam(':ids', $_POST['ids']);
        $stmt->bindParam(':idm', $_POST['idm']);
        $stmt->bindParam(':jenis', $_POST['jenis']);
        $stmt->bindParam(':nama', $_POST['nama']);
        $stmt->bindParam(':nik', $_POST['nik']);
        $stmt->bindParam(':alamat', $_POST['alamat']);
        $stmt->bindParam(':desa', $_POST['desa']);
        $stmt->bindParam(':latitude', $_POST['latitude']);
        $stmt->bindParam(':longitude', $_POST['longitude']);
        $stmt->bindParam(':nowa', $_POST['nowa']);
        $stmt->bindParam(':email', $_POST['email']);
        $stmt->bindParam(':catatan', $_POST['catatan']);
        $stmt->bindParam(':id', $page[1]);

        // Eksekusi statement
        if ($stmt->execute()) {
            sweetAlert("Data telah diubah", "success");
        } else {
          sweetAlert("GAGAL..! Merubah data", "error");
        }
      } else {
        sweetAlert("GAGAL..! Email sudah terdaftar", "error");
      }
    } catch (PDOException $e) {
        // Tangani kesalahan
      sweetAlert("Error: " . $e->getMessage(), "success");
        // echo "Error: " . $e->getMessage();
    }
}



?>