<?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); 

$lat = $isi['latitude'];
$long = $isi['longitude'];

$sql = "
SELECT *, 
(6371000 * 
    acos(
        cos(radians(:lat)) * 
        cos(radians(SUBSTRING_INDEX(titik_koordinat, ',', 1))) * 
        cos(radians(SUBSTRING_INDEX(titik_koordinat, ',', -1)) - radians(:long)) + 
        sin(radians(:lat)) * 
        sin(radians(SUBSTRING_INDEX(titik_koordinat, ',', 1)))
    )
) AS distance
FROM master_topologi
ORDER BY distance ASC";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':lat', $lat);
$stmt->bindParam(':long', $long);
$stmt->execute();
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);

?>

<!-- Main content -->
<section class="content">
  <div class="container-fluid">
    <!-- general form elements -->
    <div class="card card-warning">
      <div class="card-header">
        <h3 class="card-title">Proses 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 class="row">
            <div class="col-md-6">
              <div class="form-group m-1">
                <label for="firstDropdown">Topologi ODP</label>
                <select class="select2bs4" name="odp" style="width: 100%" id="firstDropdown" required="required" onchange="updateSecondDropdown()">
                  <option value="">Pilih</option>
                  <?php 
                  $List = new Listdata();
                  $List->set_list("master_topologi", $SqlDataWHId);
                  foreach ($List->get_list() as $key => $value): ?>
                  <option value="<?php echo $value['id']; ?>"><?php echo $value['nama']; ?></option>
                  <?php endforeach ?>
                </select>
              </div>
              <?php //echo $SqlDataWHId; ?>
            </div>
            <div class="col-md-6">
              <div class="form-group m-1">
                <label for="secondDropdown">Port ODP</label>
                <select id="secondDropdown" name="port" class="form-control" required="required">
                    <option value="">Pilih Port ODP</option>
                </select>
              </div>
            </div>
          </div>
        </div>
        <div class="card-footer">
          <button type="submit" class="btn btn-primary btn-sm m-1" name="ubah">Proses Order</button>
          <a type="button" class="btn btn-danger btn-sm m-1" data-toggle="modal" data-target="#modal-batal">Batalkan Order</a>
          <!-- <button type="submit" class="btn btn-danger" name="ubah">Batalkan Order</button> -->
        </div>
      </form>
    </div>
  </div>
</section>
<?php

if (isset($_POST['ubah'])) {
    // Data Akun PPPOE Atau Hotspot
    $akun = date('YmdHis');
    $date = date('Y-m-d H:i:s'); // Tambahkan ini jika belum ada

    try {
        // Cek apakah email sudah ada di database
        $cekmail = $pdo->prepare("SELECT id FROM pelanggan WHERE email = :email");
        $cekmail->execute([':email' => $_POST['email']]);

        // Ambil hasil dari query
        $result = $cekmail->fetch(PDO::FETCH_ASSOC);

        // Jika email ada, lanjutkan update
        if ($result) { 
          sweetAlert("Email sudah terdaftar", "error");
          } 
        else {
            // Siapkan pernyataan SQL untuk update
            $stmt = $pdo->prepare("UPDATE order_list SET 
                id_profile_paket  = :id_profile_paket,
                jenis_layanan     = :jenis_layanan,
                nama              = :nama,
                nik               = :nik,
                alamat            = :alamat,
                id_master_desa    = :id_master_desa,
                latitude          = :latitude,
                longitude         = :longitude,
                nomor_whatsapp    = :nomor_whatsapp,
                id_master_topologi= :id_master_topologi,
                port_odp          = :port_odp,
                email             = :email,
                time_update       = :time_update,
                status            = 'pending',
                akun              = :akun
                WHERE id = :id");

            // Ikat parameter
            $stmt->bindParam(':id_profile_paket', $_POST['idp']);
            $stmt->bindParam(':jenis_layanan', $_POST['jenis']);
            $stmt->bindParam(':nama', $_POST['nama']);
            $stmt->bindParam(':nik', $_POST['nik']);
            $stmt->bindParam(':alamat', $_POST['alamat']);
            $stmt->bindParam(':id_master_desa', $_POST['desa']);
            $stmt->bindParam(':latitude', $_POST['latitude']);
            $stmt->bindParam(':longitude', $_POST['longitude']);
            $stmt->bindParam(':nomor_whatsapp', $_POST['nowa']);
            $stmt->bindParam(':id_master_topologi', $_POST['odp']);
            $stmt->bindParam(':port_odp', $_POST['port']);
            $stmt->bindParam(':email', $_POST['email']);
            $stmt->bindParam(':time_update', $date);
            $stmt->bindParam(':akun', $akun);
            $stmt->bindParam(':id', $page[1]);

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

            // Jika berhasil, tampilkan pesan sukses
            sweetAlert("Data telah di lanjutkan menuju proses order", "success", "?".encrypt_url('order_list_proses'));
          } 
        
    } catch (PDOException $e) {
        // Tangani kesalahan
        sweetAlert("Error: " . $e->getMessage(), "error");
    }
}


?>

<!-- Modal Batal -->
  <div class="modal fade" id="modal-batal">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header bg-danger">
          <h4 class="modal-title">Batalkan Order</h4>
          <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
          </button>
        </div>
        <div class="modal-body">
          <h4>Order <b><?php echo $isi['nama'] ?></b> <small>akan di batalkan</small></h4>
          <form method="post" enctype="multipart/form-data">
          <div class="form-group m-1">
            <label>Keterangan</label>
            <textarea type="text" class="form-control" name="ket" rows="4"></textarea>
          </div>
        </div>
        <div class="modal-footer justify-content-between">
            <button type="submit" name="batal" class="btn btn-danger">Batalkan Order</button>
          </form>
          <?php
             if (isset($_POST['batal'])) 
              {
                $update = mysqli_query($con,"UPDATE order_list SET status = 'batal', time_update = '$date', keterangan_batal = '$_POST[ket]'
                  WHERE id = '$page[1]' ");
                if ($update) 
                {
                  sweetAlert("Order telah di batalkan", "success", "?".encrypt_url('order_list_cancel'));
                }
              }
          ?>
        </div>
      </div>
    </div>
  </div>
<!-- END Modal Batal -->
