<?php

// error_reporting(E_ALL);
// ini_set('display_errors', 1);

$date = date('Y-m-d H:i:s');
$stmt = $pdo->prepare("SELECT * FROM pelanggan WHERE id = :id");
$stmt->execute(['id' => $page[1]]);
$isi = $stmt->fetch(PDO::FETCH_ASSOC);
$user = $isi['akun'];

$stmt1 = $pdo->prepare("SELECT catatan FROM order_list WHERE akun = :akun");
$stmt1->execute(['akun' => $user]);
$isi1 = $stmt1->fetch(PDO::FETCH_ASSOC);

?>
<!-- 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>Detail 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 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-dropdown5">
                  <label>Mikrotik</label>
                  <select id="dropdown5" class="form-control select2bs4" required="required" name="ism" data-id="<?php echo $isi['id_setting_mikrotik'];  ?>">
                    
                  </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-dropdown6">
                  <label>Profile Tagihan</label>
                  <select id="dropdown6" class="form-control select2bs4" required="required" name="profiletag" data-id="<?php echo $isi['id_profile_tagihan']; ?>">
                    
                  </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']; ?>">
                    <option value="">Pilih</option>
                  </select>
                </div>
                <div class="form-group m-1">
                  <label>Id Pelanggan</label>
                  <input type="text" class="form-control" name="akun" value="<?php echo $isi['akun']; ?>">
                </div>
                <div class="form-group m-1" id="group-hotspot">
                  <label>User Hotspot</label>
                  <input type="text" class="form-control" name="user_hotspot" value="<?php echo $isi['user_hotspot']; ?>">
                </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 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>
                <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 if($value['id'] == $isi['id_master_topologi']){ echo "selected"; } ?>><?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="<?php echo $isi['port_odp']; ?>">
                  </select>
                </div>
              </div>
              <div class="col-sm-6">
                <?php if($isi['status'] != 'sampah') { ?>
                <div class="row">
                   <div class="col-6">
                    <label class="ml-2">Status Pelanggan</label><br>
                    <?php if($isi['status'] == 'aktif') { ?>
                    <a href="#" class="btn btn-success btn-sm m-2 btn-block" disabled>Aktif</a>
                    <?php } elseif($isi['status'] == 'register') { ?>
                    <a href="#" class="btn btn-warning btn-sm m-2 btn-block" disabled>Register</a>
                    <?php } else { ?>
                    <a href="#" class="btn btn-danger btn-sm m-2 btn-block" disabled>Tidak Aktif</a>
                    <?php } ?>
                    <a href="?<?php echo encrypt_url('monitoring_pelanggan|'.$page[1]); ?>" class="btn btn-sm btn-secondary m-2 btn-block"><i class="fas fa-chart-bar mr-3"></i>Riwayat Koneksi</a>
                    <a href="https://google.com/maps/?q=<?php echo $isi['latitude'].','.$isi['longitude']; ?>" target="_blank" class="btn btn-sm btn-warning m-2 btn-block"><i class="fas fa-motorcycle mr-3"></i><?php echo $isi['latitude'].', '.$isi['longitude']; ?></a>
                  </div>
                  <div class="col-6">
                    <label class="ml-2">Status Koneksi</label>
                    <?php if($isi['status_koneksi'] == 'On') { ?>
                    <a href="#" class="btn btn-success btn-sm m-2 btn-block" disabled>Connected</a>
                    <a href="?<?php echo encrypt_url('user_remote|'.$isi['id'].'|'.$isi['id_setting_mikrotik']); ?>" class="btn btn-sm btn-primary m-2 btn-block"><i class="fas fa-podcast mr-3"></i>Remote Router</a>
                    <?php } else { ?>
                    <a href="#" class="btn btn-danger btn-sm m-2 btn-block" disabled>Disconnected</a>
                    <?php } ?>
                  </div>
                </div>
                <?php } ?>
                <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" value="<?php echo $isi['latitude']; ?>" required="required">
                  <input type="hidden" id="longitude" data-maps="longitude" name="longitude" value="<?php echo $isi['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<?php echo $isi['id']; ?>">
                        <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<?php echo $isi['id']; ?>">
                        <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<?php echo $isi['id']; ?>">
                        <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<?php echo $isi['id']; ?>">
                        <span>Click to upload image</span>
                        <img src="#" alt="Image preview">
                        <div class="progressContainer">
                            <div class="progressBar"></div>
                        </div>
                      </div>
                    </center>
                  </div>
                </div>
                <div class="form-group m-1">
                  <label>Catatan Saat Order</label>
                  <textarea class="form-control" rows="2" disabled><?php echo $isi1['catatan']; ?></textarea>
                </div>
              </div>
            </div>
            <div class="card-footer">
              <?php if(in_array("Ubah", $json_array[2]['sub_menu'])){ ?>
              <button type="submit" class="btn btn-secondary btn-sm m-1" name="ubah">Simpan Perubahan</button>
              <?php } if($isi['status'] == 'register' && in_array("Aktivasi", $json_array[2]['sub_menu'])) { ?>
              <button type="submit" class="btn btn-warning btn-sm m-1" name="aktivasi">Aktivasi Pelanggan</button>
              <button type="submit" class="btn btn-danger btn-sm m-1" name="hapus">Hapus Permanen</button>
              <?php } if($isi['status'] == 'tidak aktif' && in_array("Aktivasi", $json_array[2]['sub_menu'])) { ?>
              <button type="submit" class="btn btn-primary btn-sm m-1" name="aktifkan">Aktifkan Status Pelanggan</button>
              <button type="submit" class="btn btn-danger btn-sm m-1" name="sampah">Hapus Pelanggan</button>
              <button type="submit" class="btn btn-success btn-sm m-1" name="berita_acara">Buat Berita Acara Pengambilan Alat</button>
              <?php } if($isi['status'] == 'aktif' && in_array("Aktivasi", $json_array[2]['sub_menu'])) { ?>
              <button type="submit" class="btn btn-danger btn-sm m-1" name="nonaktifkan">Non Aktifkan Status Pelanggan</button>
              <?php } if($isi['status'] == 'sampah' && in_array("Aktivasi", $json_array[2]['sub_menu'])) { ?>
              <button type="submit" class="btn btn-primary btn-sm m-1" name="aktifkan">Aktifkan Status Pelanggan</button>
              <button type="submit" class="btn btn-danger btn-sm m-1" name="hapus">Hapus Permanen</button>
              <?php } ?>
            </div>
          </form>
        </div>
      </div>
    </div>
  </div>
</section>


<?php
if (isset($_POST['aktivasi'])) {
    try {
        // Fetching setting_mikrotik
        $stmt = $pdo->prepare("SELECT * FROM setting_mikrotik WHERE id = :id");
        $stmt->execute(['id' => $isi['id_setting_mikrotik']]);
        $mik = $stmt->fetch(PDO::FETCH_ASSOC);

        // Fetching profile_paket
        $stmt = $pdo->prepare("SELECT nama_profile FROM profile_paket WHERE id = :id");
        $stmt->execute(['id' => $isi['id_profile_paket']]);
        $pro = $stmt->fetch(PDO::FETCH_ASSOC);

        // Fetching master_topologi
        $stmt = $pdo->prepare("SELECT nama FROM master_topologi WHERE id = :id");
        $stmt->execute(['id' => $isi['id_master_topologi']]);
        $top = $stmt->fetch(PDO::FETCH_ASSOC);

        $API = new RouterosAPI();
        $API->debug = false;

        $komen = $top['nama'] . ' Port ' . $isi['port_odp'] . '-' . $isi['nama'];
        $akun = $isi['akun'];
        $profile = $pro['nama_profile'];
        $idsv = $isi['id_data_server'];
        $profRemote = $mik['profile_remote'];
        $hotspot = $isi['user_hotspot'];

        // Connect to Mikrotik
        if ($API->connect2($mik['ip_address'], $mik['username'], $mik['password'], $mik['port_api'])) {
            // PPPOE add mikrotik
            if ($isi['jenis_layanan'] == "PPPOE") {
                $in = $API->comm('/ppp/secret/add', [
                    "name" => $akun,
                    "password" => $akun,
                    "profile" => $profile,
                    "service" => "pppoe",
                    "comment" => $komen,
                ]);
            } else { // Hotspot add mikrotik
                $in = $API->comm('/ppp/secret/add', [
                    "name" => $akun,
                    "password" => $akun,
                    "profile" => $profRemote,
                    "service" => "pppoe",
                    "comment" => $komen,
                ]);
                $in2 = $API->comm('/ip/hotspot/user/add', [
                    "name" => $hotspot,
                    "password" => $hotspot,
                    "profile" => $profile,
                    "server" => "all",
                    "comment" => $komen,
                ]);
            }
            $API->disconnect();

            // Memeriksa hasil dari API
            if (!isset($in['!trap']) && (!isset($in2['!trap']) || $isi['jenis_layanan'] == "PPPOE")) {
                // IF Success bikin akun

                // Kirim Pesan WhatsApp
                $nomor = [$isi['nomor_whatsapp']];
                $time_start = date('Y-m-d H:i:s');
                $APIwa = new PesanWA();
                $APIwa->Create_Pesan($isi['id_data_server'], $isi['id'], 'pesan_aktivasi');
                $Pesan = $APIwa->Get_Pesan();
                $APIwa->Send_Pesan($isi['id_data_server'], 'sendMessage', $nomor, $Pesan);

                // Menggunakan prepared statement untuk keamanan
                $stmt = $pdo->prepare("SELECT kode_unik_tagihan FROM pelanggan WHERE id_data_server = :id ORDER BY kode_unik_tagihan DESC LIMIT 1");
                $stmt->execute(['id' => $idsv]);
                $pckut1 = $stmt->fetch(PDO::FETCH_ASSOC);

                if ($pckut1) {
                    $lastCode = $pckut1['kode_unik_tagihan'];
                    $arr2 = range(1, $lastCode);
                    $stmt = $pdo->prepare("SELECT kode_unik_tagihan FROM pelanggan WHERE id_data_server = :id ORDER BY kode_unik_tagihan");
                    $stmt->execute(['id' => $idsv]);
                    $arr1 = $stmt->fetchAll(PDO::FETCH_COLUMN, 0);

                    $missing = array_diff($arr2, $arr1);
                    $kodeuser = !empty($missing) ? reset($missing) : $lastCode + 1;
                } else {
                    $kodeuser = 1;
                }
                // Update Pelanggan
                $stmt = $pdo->prepare("UPDATE pelanggan SET 
                    id_setting_mikrotik = :id_setting_mikrotik,
                    id_profile_paket = :id_profile_paket,
                    id_marketing = :id_marketing,
                    id_master_desa = :id_master_desa, 
                    id_profile_tagihan = :id_profile_tagihan,
                    status = 'aktif', 
                    user_hotspot = :user_hotspot,
                    kode_unik_tagihan = :kodeuser,
                    time_start = :time_start 
                    WHERE id = :id");
                $stmt->execute([
                    ':id_setting_mikrotik' => $_POST['ism'],
                    ':id_profile_paket' => $_POST['idp'],
                    ':id_marketing' => $_POST['idm'],
                    ':id_master_desa' => $_POST['desa'],
                    ':id_profile_tagihan' => $_POST['profiletag'],
                    'user_hotspot' => $hotspot,
                    'kodeuser' => $kodeuser,
                    'time_start' => $time_start,
                    'id' => $isi['id']
                ]);

                sweetAlert("Aktivasi Pelanggan Berhasil..!!", "success");
            } else {
                // IF Gagal bikin akun
                $meror = isset($in['!trap']) ? $in['!trap'][0]['message']." In PPPOE" : $in2['!trap'][0]['message']." In Hotspot";
                sweetAlert($meror, "error");
            }
        } else {
            // IF Koneksi mikrotik gagal
            sweetAlert("Aktivasi gagal..!! Aplikasi ke Mikrotik Tidak Terhubung", "error");
        }
    } catch (PDOException $e) {
        // Tangani pengecualian PDO
        sweetAlert("Kesalahan Database: " . $e->getMessage(), "error");
    } catch (Exception $e) {
        // Tangani pengecualian lainnya
        sweetAlert("Terjadi kesalahan: " . $e->getMessage(), "error");
    }
}

// Buat Berita Acara Pengambilan Alat di Rumah pelanggan OFF
if (isset($_POST['berita_acara'])) {
    try {
        // Ambil data perusahaan
        $ainvp = mysqli_query($con,"SELECT 
        nama_perusahaan,
        alamat_perusahaan,
        telepon_perusahaan,
        logo_perusahaan
        FROM data_server WHERE id = '$isi[id_data_server]'");
        
        // Cek apakah query berhasil
        if (!$ainvp) {
            throw new Exception('Gagal mengambil data perusahaan: ' . mysqli_error($con));
        }

        $invp = mysqli_fetch_assoc($ainvp);
        
        $nama = $invp['nama_perusahaan'];
        $alamat = $invp['alamat_perusahaan'];
        $telepon = $invp['telepon_perusahaan'];
        $logo = $invp['logo_perusahaan'];

        // Membuat PDF
        require ('berita_acara/berita_acara.php');
        $pdf = new PDF();
        
        // Set header data perusahaan
        $pdf->setHeaderData($nama, $alamat, $telepon, $logo);
        $pdf->AddPage();

        // Tambahkan data pelanggan
        $pdf->AddBeritaAcara($isi['akun'], $isi['nomor_whatsapp'], $isi['nama'], $isi['alamat']);
        
        // Output PDF ke file
        $pdfFile = 'berita_acara/BA-pengambilan-alat-' . $isi['nama'] . '-' . $isi['akun'] . '.pdf';
        $pdf->Output('F', $pdfFile);

        // Tampilkan PDF di tab baru
        echo '<script type="text/javascript">window.open("' . $pdfFile . '", "_blank")</script>';

    } catch (Exception $e) {
        sweetAlert("Error: ' . $e->getMessage() . '", "error");
    }
}

if (isset($_POST['ubah'])) {
    $fotoodp1    = $_SESSION['uploaded_files']['odp'.$isi['id']];
    $fotorumah1  = $_SESSION['uploaded_files']['rmh'.$isi['id']];
    $fotoonu1    = $_SESSION['uploaded_files']['onu'.$isi['id']];
    $fototes1    = $_SESSION['uploaded_files']['tes'.$isi['id']];

    if($_POST['jenis'] == "Hotspot"){
      $hotspot = $_POST['user_hotspot'];
    } else {
      $hotspot = "";
    }

    $SQL = "UPDATE pelanggan SET 
        id_data_server = :id_data_server,
        id_setting_mikrotik = :id_setting_mikrotik,
        id_profile_paket = :id_profile_paket,
        id_master_topologi = :id_master_topologi,
        id_marketing = :id_marketing,
        id_master_desa = :id_master_desa, 
        id_profile_tagihan = :id_profile_tagihan,
        port_odp = :port_odp,
        jenis_layanan = :jenis_layanan,
        nama = :nama,
        nik = :nik, 
        alamat = :alamat,
        latitude = :latitude,
        longitude = :longitude,
        nomor_whatsapp = :nomor_whatsapp,
        email = :email,
        akun = :akun,
        user_hotspot = :user_hotspot,
        foto_odp = :foto_odp,
        foto_rumah = :foto_rumah,
        foto_onu = :foto_onu,
        foto_test = :foto_test
        WHERE id = :id";

    try {
        $stmt = $pdo->prepare($SQL);
        $stmt->execute([
            ':id_data_server' => $_POST['ids'],
            ':id_setting_mikrotik' => $_POST['ism'],
            ':id_profile_paket' => $_POST['idp'],
            ':id_master_topologi' => $_POST['odp'],
            ':id_marketing' => $_POST['idm'],
            ':id_master_desa' => $_POST['desa'],
            ':id_profile_tagihan' => $_POST['profiletag'],
            ':port_odp' => $_POST['port'],
            ':jenis_layanan' => $_POST['jenis'],
            ':nama' => $_POST['nama'],
            ':nik' => $_POST['nik'],
            ':alamat' => $_POST['alamat'],
            ':latitude' => $_POST['latitude'],
            ':longitude' => $_POST['longitude'],
            ':nomor_whatsapp' => $_POST['nowa'],
            ':email' => $_POST['email'],
            ':akun' => $_POST['akun'],
            ':user_hotspot' => $hotspot,
            ':foto_odp' => $fotoodp1,
            ':foto_rumah' => $fotorumah1,
            ':foto_onu' => $fotoonu1,
            ':foto_test' => $fototes1,
            ':id' => $page[1]
        ]);

        sweetAlert("Data pelanggan sudah diubah", "success");
    } catch (PDOException $e) {
        // Handle error appropriately
        sweetAlert("Error: " . $e->getMessage(), "error");
        // echo "Error: " . $e->getMessage();
    }
}


//Nonaktifkan Pelanggan
if (isset($_POST['nonaktifkan'])) {

$set1 = $con->query("SELECT ip_address,username,password,port_api FROM setting_mikrotik WHERE id = '$isi[id_setting_mikrotik]'");
$pset = $set1->fetch_assoc();
  $m_ip   = $pset['ip_address'];
  $m_user = $pset['username'];
  $m_pass = $pset['password'];
  $m_port = $pset['port_api'];

// require('router_api/routeros_api.class.php');
$API = new RouterosAPI();
$API->debug = false;

    if ($API->connect2($m_ip, $m_user, $m_pass, $m_port)) 
    {

    $arrID=$API->comm("/ppp/secret/getall", 
                array(
              ".proplist"=> ".id",
              "?name" => $user , 
              ));

    $API->comm("/ppp/secret/set",
      array(
         ".id" => $arrID[0][".id"],
         "disabled"  => "true",
         ));

    $arrID1=$API->comm("/ppp/active/getall",
      array(
          ".proplist"=> ".id",
         "?name" => $user ,
         ));

    $API->comm("/ppp/active/remove",
      array(
         ".id" => $arrID1[0][".id"],
         ));

    $API->disconnect();

    }
  $up = $con->query("UPDATE pelanggan SET status = 'tidak aktif', status_koneksi = 'Off' WHERE id = '$page[1]'");
  if ($up) 
  {
    sweetAlert("Pelanggan Telah di Non Aktifkan", "success");
  }

}

//Aktifkan Pelanggan
if (isset($_POST['aktifkan'])) {

$set1 = $con->query("SELECT ip_address,username,password,port_api FROM setting_mikrotik WHERE id = '$isi[id_setting_mikrotik]'");
$pset = $set1->fetch_assoc();
  $m_ip   = $pset['ip_address'];
  $m_user = $pset['username'];
  $m_pass = $pset['password'];
  $m_port = $pset['port_api'];
//   require('router_api/routeros_api.class.php');

        $API = new RouterosAPI();

        $API->debug = false;

        if ($API->connect2($m_ip, $m_user, $m_pass, $m_port)) 
        {
        $arrID=$API->comm("/ppp/secret/getall", 
                array(
              ".proplist"=> ".id",
              "?name" => $user , 
              ));

        $API->comm("/ppp/secret/set",
        array(
         ".id" => $arrID[0][".id"],
         "disabled"  => "false",
         ));
        $API->disconnect();
        }

  $up = $con->query("UPDATE pelanggan SET status = 'aktif' WHERE id = '$page[1]'");
  if ($up) 
  {
    sweetAlert("Pelanggan Telah di Aktifkan", "success");
  }
}

if (isset($_POST['sampah']))
{
    $set1 = $con->query("SELECT ip_address,username,password,port_api FROM setting_mikrotik WHERE id = '$isi[id_setting_mikrotik]'");
    $pset = $set1->fetch_assoc();
      $m_ip   = $pset['ip_address'];
      $m_user = $pset['username'];
      $m_pass = $pset['password'];
      $m_port = $pset['port_api'];
    
    // require('router_api/routeros_api.class.php');
    $API = new RouterosAPI();
    $API->debug = false;
    
        if ($API->connect2($m_ip, $m_user, $m_pass, $m_port)) 
        {
    
        $arrID=$API->comm("/ppp/secret/getall", 
                    array(
                  ".proplist"=> ".id",
                  "?name" => $user , 
                  ));
    
        $API->comm("/ppp/secret/set",
          array(
             ".id" => $arrID[0][".id"],
             "disabled"  => "true",
             ));
    
        $arrID1=$API->comm("/ppp/active/getall",
          array(
              ".proplist"=> ".id",
             "?name" => $user ,
             ));
    
        $API->comm("/ppp/active/remove",
          array(
             ".id" => $arrID1[0][".id"],
             ));
    
        $API->disconnect();
    
        }
      $up = $con->query("UPDATE pelanggan SET status = 'sampah', status_koneksi = 'Off' WHERE id = '$page[1]'");
      if ($up) 
      {
        sweetAlert("Pelanggan Telah di pindahkan ke Sampah", "info", "?".encrypt_url('user_sampah'));
      }
}

if (isset($_POST['hapus']))
{
  $iddata = $page[1];
  deleteAlert("pelanggan",$iddata, '?'.encrypt_url($page[2]));
}

$_SESSION['uploaded_files']['odp'.$isi['id']] = $isi['foto_odp'];
$_SESSION['uploaded_files']['rmh'.$isi['id']] = $isi['foto_rumah'];
$_SESSION['uploaded_files']['onu'.$isi['id']] = $isi['foto_onu'];
$_SESSION['uploaded_files']['tes'.$isi['id']] = $isi['foto_test'];
?>