<!-- Main content -->
<section class="content">
  <div class="container-fluid">
    <!-- general form elements -->
    <div class="card card-primary">
      <div class="card-header">
        <h3 class="card-title">Tambah Order List</h3>
      </div>
      <form method="post" enctype="multipart/form-data">
        <div class="row">
          <div class="col-md-6">
            <center><label>INFORMASI LAYANAN</label></center>
            <div class="card-body">
              <div class="form-group m-1">
                <label>Pilih Server</label>
                <select id="dropdown0" class="form-control" 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 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" required name="jenis">
                    <option value="">Pilih</option>
                    <option value="PPPOE">PPPOE</option>
                    <option value="Hotspot">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 name="idp">
                    
                </select>
              </div>
              <div class="form-group m-1" id="group-dropdown3">
                <label>Marketing Area / Penagih</label>
                <select id="dropdown3" class="form-control select2bs4" required name="idm">
                    
                </select>
              </div>
              <hr><center><label>INFORMASI PRIBADI</label></center>
              <div class="form-group m-1">
                <label>NIK</label>
                <input type="text" class="form-control" required="required" name="nik">
              </div>
              <div class="form-group m-1">
                <label>Nama</label>
                <input type="text" class="form-control" required="required" name="nama">
              </div>
              <div class="form-group m-1">
                <label>Email</label>
                <input type="email" class="form-control" required="required" name="email">
              </div>
              <div class="form-group m-1">
                <label>Nomor WhatsApp</label>
                <input type="text" class="form-control" required="required" name="nowa">
              </div>
            </div>
          </div>
          <div class="col-md-6">
            <div class="card-body">
              <div class="form-group m-1">
                <label>Alamat</label>
                <input type="text" class="form-control" required="required" name="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">
                  
                </select>
              </div>
              <div class="form-group m-1">
                <label>Catatan</label>
                <textarea class="form-control" name="catatan" rows="3"></textarea>
                <i class="text-danger"><small>Isi jika di perlukan</small></i>
              </div>
              <div class="form-group m-1" >
                <label>Pilih Lokasi Maps / Isi Koordinat</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" 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 class="card-footer">
          <button type="submit" class="btn btn-primary" name="tambah">Tambah</button>
        </div>
      </form>
    </div>
  </div>
</section>
<?php 

if (isset($_POST['tambah'])) {
    $nowa = preg_replace("/[^0-9]/", "", $_POST['nowa']);
    try {
        
        $cekExist = $pdo->prepare("SELECT id FROM pelanggan WHERE email = :email OR nik = :nik");
        $cekExist->execute([
            ':email' => $_POST['email'],
            ':nik' => $_POST['nik']
        ]);

        if ($cekExist->rowCount() === 0) {
            
            $stmt = $pdo->prepare("INSERT INTO order_list (id_data_server, id_profile_paket, id_marketing, jenis_layanan, nama, nik, alamat, id_master_desa, latitude, longitude, nomor_whatsapp, email, status, catatan, create_with) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'order', ?, ?)");
            $stmt->execute([$_POST['ids'], $_POST['idp'], $_POST['idm'], $_POST['jenis'], $_POST['nama'], $_POST['nik'], $_POST['alamat'], $_POST['desa'], $_POST['latitude'], $_POST['longitude'], $nowa, $_POST['email'], $_POST['catatan'], $SvNama]);
            
            $last_id = $pdo->lastInsertId();
            
            $ano = $pdo->prepare("SELECT nomor_whatsapp FROM data_server WHERE id = ?");
            $ano->execute([$_POST['ids']]);
            $no = $ano->fetch(PDO::FETCH_ASSOC);
            
            $nomor = explode(',', $no['nomor_whatsapp'] . ',' . $nowa);
            
            // Kirim Pesan WhatsApp
            $APIwa = new PesanWA();
            $APIwa->Create_Pesan($_POST['ids'], $last_id, 'pesan_registrasi', 'order');
            $Pesan = $APIwa->Get_Pesan();
            $APIwa->Send_Pesan($_POST['ids'], 'sendMessage', $nomor, $Pesan);
            
            sweetAlert("Order list telah di tambah", "success", "?" . encrypt_url('order_list'));
            
        } else {
            $existRow = $cekExist->fetch(PDO::FETCH_ASSOC);
            
            if ($existRow['email'] === $_POST['email']) {
                sweetAlert("Registrasi GAGAL..! EMAIL sudah terdaftar", "error");
            } elseif ($existRow['nik'] === $_POST['nik']) {
                sweetAlert("Registrasi GAGAL..! NIK sudah terdaftar", "error");
            } else {
                sweetAlert("Registrasi GAGAL..! gagal upload ke database", "error");
            }
        }
    } catch (PDOException $e) {
        sweetAlert("Error: " . $e->getMessage(), "error");
        // Handle exception
        // echo "Error: " . $e->getMessage();
    }
}

?>
