<?php
include "../config/connect.php";

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Ambil data dari permintaan POST
    $postData = file_get_contents('php://input');
    $data = json_decode($postData);
    
$id     = $data->id;
$pppa   = $data->pppa;
$hsa    = $data->hsa;
$ut 	= $data->ut;
$bm 	= $data->bm;
$times  = date('Y-m-d H:i:s');
$data2 = $id."\n".$pppa."\n".$hsa."\n".$ut."\n".$bm;

$koneksi->query("UPDATE setting SET pppa='$pppa', hsa='$hsa', uptime_mikrotik='$ut', device_mikrotik='$bm', status_mikrotik='Connected', last_update_time='$times' WHERE serial_number='$id'");

if($pppa != "")
{
//UPDATE PPPOE
    $am = $koneksi->query("SELECT pppoe FROM pelanggan JOIN setting ON pelanggan.IdSetting=setting.IdSetting WHERE serial_number = '$id'");
    while($pc = $am->fetch_assoc())
    {
        echo $pc['pppoe']."<br>";
    	if(preg_match("/$pc[pppoe]/i", $pppa)==1)
    	{
    	    //cari nama awalan
    	    $cari00 = stristr($pppa, $pc['pppoe']);
    	    //ambil sampai akhir tanda |
            $cari02 = strtok($cari00, "|");
            //rubah ke array
            $data = explode("_",$cari02);
            //ambil data ip address
            $ipadd = $data[1];
            //update data pelanggan
    	    $koneksi->query("UPDATE pelanggan SET StatusKon = 'On', IpAddress = '$ipadd', TimeUpdate = '$times' WHERE pppoe = '$pc[pppoe]'");
    	}
    	else
    	{
    		$koneksi->query("UPDATE pelanggan SET StatusKon = 'Off' WHERE pppoe = '$pc[pppoe]'");
    	}
    }
}

if($hsa != "")
{
    //UPDATE HOTSPOT
    $amhs = $koneksi->query("SELECT Voucher,DurasiVoucher FROM genvoucher 
    	INNER JOIN setting ON genvoucher.IdSetting=setting.IdSetting
    	INNER JOIN paketvoucher ON genvoucher.IdVoucher=paketvoucher.IdVoucher
    	WHERE serial_number = '$id' AND Aktif = ''");
    while($pchs = $amhs->fetch_assoc())
    {
    	if(preg_match("/$pchs[Voucher]/i", $hsa)==1)
    	{
    		//cari nama awalan
    	    $cari0 = stristr($hsa, $pchs['Voucher']);
    	    //ambil sampai akhir tanda |
            $cari2 = strtok($cari0, "|");
            //rubah ke array
            $datahs = explode("_",$cari2);
            //ambil data waktu aktif
            $timeactive = str_replace("."," ",$datahs[1]);
            //cek format tanggal IF Y-m-d
    		if(substr($timeactive,0,4) == date('Y'))
    		{
    		 	$activetime = $timeactive;
    		}
    		else
    		{
    			$date 		= date_create_from_format("M/d/Y H:i:s",$timeactive);
    			$activetime = date_format($date,"Y-m-d H:i:s");
    		}
    
    		$date1 = date_create($activetime);
    		date_add($date1, date_interval_create_from_date_string($pchs['DurasiVoucher']));
    		$exptime = date_format($date1, 'Y-m-d H:i:s');
    		$koneksi->query("UPDATE genvoucher SET Aktif = '$activetime', Expaired = '$exptime', StaVoucher='0' WHERE Voucher = '$pchs[Voucher]'");
    		
    		echo $pchs['Voucher']." => ".$activetime." => ".$exptime."<br>";
    	}
    	else
    	{
    		//CEK IF Update Aktif Kosong OR Aktif Terisi. 
    		//$koneksi->query("UPDATE genvoucher SET StaVoucher = '1' WHERE Voucher = '$pchs[Voucher]' ");
    	}
    }
}
file_put_contents('apimik-log.log',$data);
}

?>