<?php
// required headers
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");

include_once '../config/database.php';
include_once '../objects/ont.php';
include_once '../objects/ont_status.php';
date_default_timezone_set('Asia/Rangoon');
session_start();
$database = new Database();
$db = $database->getConnection();
 
$ont = new ONT($db);
$ont_status = new OntStatus($db);
$data = json_decode(file_get_contents("php://input"));

$ont->id_no = $data->id_no;
$ont_status->id_no = $data->id_no;
$ont_status->entry_by = $_SESSION['user'];
$ont_status->entry_date_time = date("Y-m-d H:i:s");	

$t = "";
if($data->status=="Enabled"){
    $ont->status = 2;
	$ont_status->status = 2;
	$t = "disable";
}else if($data->status=="Disabled"){
    $ont->status = 1;
	$ont_status->status = 1;
	$t = "enable";
}

if($data->id_no){
	if($ont->updateStatus()){
		$ont_status->create();
		$msg_arr = array(
			"message" => "updated"
		);
	}else{
		$msg_arr = array(
			"message" => "error"
		);
	}
}

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://tkknet.smartolt.com/api/onu/'.$t.'/'.$data->sno,
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_HTTPHEADER => array(
    'X-Token: ' . $data->smart_key,
    'Cookie: ci_session=155rkcpps2qbjgr3hbbav4gl8cmmcnnsaaaaa'
  ),
));

$response = curl_exec($curl);

curl_close($curl);

echo json_encode($msg_arr);
?>	