<!-- Main content -->
<section class="content">
  <div class="container-fluid">
    <div class="row">
      <div class="col-12">
        <div class="card">
          <div class="card-header bg-primary">
            <div class="row">
              <div class="col-sm-6">
                <h5>Tambah Pelanggan</h5>
              </div>
              <div class="col-sm-6">
              </div>
            </div>
          </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 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" required>
                    <option value="">Pilih</option>
                    <option value="PPPOE">PPPOE</option>
                    <option value="Hotspot">Hotspot</option>
                  </select>
                </div>

                <div class="form-group m-1" id="group-dropdown5">
                  <label>Mikrotik</label>
                  <select id="dropdown5" class="form-control select2bs4" name="ism">
                    
                  </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" name="nik">
                </div>
                <div class="form-group m-1">
                  <label>Nama</label>
                  <input type="text" class="form-control" name="nama">
                </div>
                <div class="form-group m-1">
                  <label>Email</label>
                  <input type="email" class="form-control" name="email">
                </div>
                <div class="form-group m-1">
                  <label>Nomor WhatsApp</label>
                  <input type="text" class="form-control" name="nowa">
                </div>
                <div class="form-group m-1">
                  <label>Alamat</label>
                  <input type="text" class="form-control" 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" data-id="">
                    <option value="">Pilih</option>
                  </select>
                </div>
                <hr><center><label>INFORMASI TOPOLOGI</label></center>
                <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>
                <div class="form-group m-1">
                  <label for="secondDropdown">Port ODP</label>
                  <select id="secondDropdown" name="port" class="form-control" required="required" data-id="">
                  </select>
                </div>
              </div>
              <div class="col-sm-6">
                <div class="form-group m-1">
                  <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 class="row">
                  <div class="col-sm-6">
                    <center>
                      <label>Upload Foto Port ODP</label>
                      <div class="uploadImage" data-id="odp">
                        <span>Click to upload image</span>
                        <img src="#" alt="Image preview">
                        <div class="progressContainer">
                          <div class="progressBar"></div>
                        </div>
                      </div>
                      <label>Upload Foto Rumah</label>
                      <div class="uploadImage" data-id="rmh">
                        <span>Click to upload image</span>
                        <img src="#" alt="Image preview">
                        <div class="progressContainer">
                           <div class="progressBar"></div>
                        </div>
                      </div>
                    </center>
                  </div>
                  <div class="col-sm-6">
                    <center>
                      <label>Upload Foto ONU terinstalasi</label>
                      <div class="uploadImage" data-id="onu">
                        <span>Click to upload image</span>
                        <img src="#" alt="Image preview">
                        <div class="progressContainer">
                          <div class="progressBar"></div>
                        </div>
                      </div>
                      <label>Upload SpeedTest</label>
                        <div class="uploadImage" data-id="tes">
                        <span>Click to upload image</span>
                        <img src="#" alt="Image preview">
                        <div class="progressContainer">
                            <div class="progressBar"></div>
                        </div>
                      </div>
                    </center>
                  </div>
                </div>
              </div>
            </div>
            <div class="card-footer">
              <button type="submit" class="btn btn-primary btn-sm" name="tambah">Tambah Pelanggan</button>
            </div>
          </form>
        </div>
      </div>
    </div>
  </div>
</section>
<?php 
if (isset($_POST['tambah'])) {
    $akun = date('YmdHis');

    $fotoodp1 = $_SESSION['uploaded_files']['odp'];
    $fotorumah1 = $_SESSION['uploaded_files']['rmh'];
    $fotoonu1 = $_SESSION['uploaded_files']['onu'];
    $fototes1 = $_SESSION['uploaded_files']['tes'];
    $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) {
            
            // Siapkan pernyataan SQL
            $stmt = $pdo->prepare("INSERT INTO pelanggan (id_data_server, id_setting_mikrotik, id_profile_paket, id_master_topologi, id_marketing, id_master_desa, port_odp, jenis_layanan, nama, nik, alamat, latitude, longitude, nomor_whatsapp, email, foto_odp, foto_rumah, foto_onu, foto_test, akun, status) VALUES 
                (:id_data_server, :id_setting_mikrotik, :id_profile_paket, :id_master_topologi, :id_marketing, :id_master_desa, :port_odp, :jenis_layanan, :nama, :nik, :alamat, :latitude, :longitude, :nomor_whatsapp, :email, :foto_odp, :foto_rumah, :foto_onu, :foto_test, :akun, 'register')");
    
            // Ikat parameter
            $stmt->bindParam(':id_data_server', $_POST['ids']);
            $stmt->bindParam(':id_setting_mikrotik', $_POST['ism']);
            $stmt->bindParam(':id_profile_paket', $_POST['idp']);
            $stmt->bindParam(':id_master_topologi', $_POST['odp']);
            $stmt->bindParam(':id_marketing', $_POST['idm']);
            $stmt->bindParam(':id_master_desa', $_POST['desa']);
            $stmt->bindParam(':port_odp', $_POST['port']);
            $stmt->bindParam(':jenis_layanan', $_POST['jenis']);
            $stmt->bindParam(':nama', $_POST['nama']);
            $stmt->bindParam(':nik', $_POST['nik']);
            $stmt->bindParam(':alamat', $_POST['alamat']);
            $stmt->bindParam(':latitude', $_POST['latitude']);
            $stmt->bindParam(':longitude', $_POST['longitude']);
            $stmt->bindParam(':nomor_whatsapp', $nowa);
            $stmt->bindParam(':email', $_POST['email']);
            $stmt->bindParam(':foto_odp', $fotoodp1);
            $stmt->bindParam(':foto_rumah', $fotorumah1);
            $stmt->bindParam(':foto_onu', $fotoonu1);
            $stmt->bindParam(':foto_test', $fototes1);
            $stmt->bindParam(':akun', $akun);
    
            // Eksekusi pernyataan
            $stmt->execute();
            $last_id = $pdo->lastInsertId();
    
            $watsap = $pdo->prepare("SELECT nomor_whatsapp FROM data_server WHERE id = :iddata");
            $watsap->execute([':iddata' => $_POST['ids']]);
            $watsapp = $watsap->fetch(PDO::FETCH_ASSOC);
    
            // Kirim Pesan WhatsApp
            $nomor = explode(',', $nowa.','.$watsapp['nomor_whatsapp']);
            $APIwa = new PesanWA();
            $APIwa->Create_Pesan($_POST['ids'], $last_id, 'pesan_registrasi');
            $Pesan = $APIwa->Get_Pesan();
            $APIwa->Send_Pesan($_POST['ids'], 'sendMessage', $nomor, $Pesan);
    
            $_SESSION['uploaded_files'] = '';
            $_SESSION['uploaded_files'] = '';
            $_SESSION['uploaded_files'] = '';
            $_SESSION['uploaded_files'] = '';
    
            sweetAlert("Data pelanggan telah di tambahkan", "success", "?".encrypt_url('user_register'));
            
        } 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..! Data sudah terdaftar", "error");
            }
        }
        // print_r($nomor);
    } catch (PDOException $e) {
        // Tangani kesalahan
        sweetAlert("Error: " . $e->getMessage(), "error");
    }
}

?>