<?php
session_start();
require('routeros_api.class.php');

// Baca file list Mikrotik
$dataFile = __DIR__ . '/mikrotik_list.json';
if (!file_exists($dataFile)) {
    $_SESSION['toast_error'] = "File data tidak ditemukan";
    header("Location: index.php");
    exit;
}
$mikrotikList = json_decode(file_get_contents($dataFile), true);

if (!isset($_GET['id']) || !isset($mikrotikList[$_GET['id']])) {
    $_SESSION['toast_error'] = "Data Mikrotik tidak ditemukan";
    header("Location: index.php");
    exit;
}

$mt = $mikrotikList[$_GET['id']];

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

if ($API->connect($mt['ip'], $mt['user'], $mt['pass'])) {
    $_SESSION['ip']   = $mt['ip'];
    $_SESSION['user'] = $mt['user'];
    $_SESSION['pass'] = $mt['pass'];

    $API->disconnect();
    header("Location: peta.php");
    exit;
} else {
    $_SESSION['toast_error'] = "Gagal terhubung ke Mikrotik ({$mt['ip']}) Mohon Periksa kembali VPN anda";
    header("Location: index.php");
    exit;
}
