<?php

if (session_status() === PHP_SESSION_NONE) session_start();

require_once realpath(dirname(__DIR__, 2) . '/services/Model.php');
$obj = new Model();

require_once realpath(dirname(__DIR__, 2) . '/services/Mikrotik.php'); 


session_write_close();


$id = (int)($_POST['id'] ?? 0);
$response = ['connected' => false, 'status' => 0];

if($id > 0){
    $user = $obj->getSingleData('re_mikrotik_user', ['where'=>['id','=', $id]]);
    if($user && !empty($user['mik_ip'])){
        $mk = new Mikrotik($user['mik_ip'], $user['mik_port'], $user['mik_username'], $user['mik_password']);
        $response['connected'] = !empty($mk->connected);
        $response['status'] = $user['status'];
    }
}

echo json_encode($response);