<section class="content" style="zoom : 90%;">
  <div class="container-fluid">
    <div class="row">
      <div class="col-md-6">
        <div class="card">
          <div class="card-header">
            <div class="row">
              <div class="col-sm-6">
                <h4>Remote Router / ONU Pelanggan</h4>
              </div>
              <div class="col-sm-6">
              </div>
            </div>
          </div>

<?php

$ambil=$con->query("SELECT id,ip_address,username,password,port_api,port_remote,user_remote,last_remote FROM setting_mikrotik WHERE id='$page[2]'");
$pecah= $ambil->fetch_assoc();

if ($page[1] == 0000) {

$addressuser = $page[3];
$nama       = $page[4];
$lasttime   = "";
$alamat     = "";

} else {

$sqlpel=$con->query("SELECT id,nama,ip_address,time_update,alamat FROM pelanggan WHERE id='$page[1]'");
$pel= $sqlpel->fetch_assoc();
// print_r($pecah);

$addressuser = $pel['ip_address'];
$nama       = $pel['nama'];
$lasttime   = $pel['time_update'];
$alamat     = $pel['alamat'];

}

$idset      = $pecah['id'];
$addressmik = $pecah['ip_address'];
$username   = $pecah['username'];
$password   = $pecah['password'];
$portapi    = $pecah['port_api'];
$portremote = $pecah['port_remote'];
$last_remote = $pecah['last_remote'];
$user_remote = $pecah['user_remote'];
$now 		 = date('Y-m-d H:i:s');

$start = new DateTime($last_remote);
$end = new DateTime($now);

$diff_in_seconds = $end->getTimestamp() - $start->getTimestamp();//

// print_r($pecah);
// echo "<br><br><br>";
// print_r($pel);
if ($SvNama != $user_remote && $diff_in_seconds <= 180) 
{
	?>
	    <div class="card-body">
		    <div class="text-center">
		        <div class="alert alert-danger">
		            <h5>
		            <b>PERHATIAN..!!</b><br>
		            Akun <b><?php echo $user_remote;?></b> Sedang remote pelanggan lain..<br>
		            Tunggu beberapa saat untuk bisa buka Router/Modem Pelanggan.</h5>
		        </div>
		    </div>
	    </div>
	<?php
}

else
{	
$con->query("UPDATE setting_mikrotik SET user_remote='$SvNama', last_remote='$now' WHERE id = '$idset'");

$linkremote = $addressmik.':'.$portremote;

//set dst-nat mikrotik

//require('router_api/routeros_api.class.php');

$API = new RouterosAPI();

$API->debug = false;

if ($API->connect2($addressmik, $username, $password, $portapi)) {

	$arrID=$API->comm("/ip/firewall/nat/getall", 
      array(
      ".proplist"=> ".id",
      "?dst-port" => $portremote ,
      ));

	$S=$API->comm("/ip/firewall/nat/set",
	  array(
	   ".id" => $arrID[0][".id"],
	   "to-addresses"  => $addressuser,
	   )
	  );
	$API->disconnect();
} else {
    echo "APLIKASI TIDAK DAPAT TERHUBUNG DENGAN MIKROTIK";
}

if($S !='')
{
	?>
	    <div class="card-body">
		    <table class="table table-borderless">
		        <tr>
		            <td>Nama</td>
		            <td><?php echo $nama; ?></td>
		        </tr>
		        <tr>
		            <td>Alamat</td>
		            <td><?php echo $alamat; ?></td>
		        </tr>
		        <tr>
		            <td>Last Ip Address</td>
		            <td><?php echo $addressuser; ?></td>
		        </tr>
		        <tr>
		            <td>Last Up Time</td>
		            <td><?php echo $lasttime; ?></td>
		        </tr>
		    </table><br><br>
		    <div class="text-center">
		        <div class="alert alert-danger">
		            <b>PERINGATAN..!!</b>
		            Router / Modem Pelanggan bisa di remote jika fitur remote di Router/Modem di aktifkan.
		        </div>
			    <a target="_blank" class="btn btn-primary btn-rouded btn-sm" href="http://<?php echo $linkremote;?>">Buka Web Browser</a>
		    </div>
	    </div>
	<?php
	}
}
?>
</div></div></div></div></section>