<?php
require_once __DIR__ . '/../config/user_functions.php';

session_start();
require_once __DIR__ . '/../config/db_remote.php';
$user = $_SESSION['user'] ?? null;
$role = $user['role'] ?? 'viewer';
if ($role !== 'admin') {
    die("You do not have permission to perform this action.");
}

// Path to start.bat
$bat = realpath(__DIR__ . '/../keyence_service/start.bat');

if ($bat && file_exists($bat)) {
    // WARNING: requires shell_exec enabled and permissions on Windows
    $cmd = 'cmd /c "start \"KeyenceService\" \"' . $bat . '\""';
    @shell_exec($cmd);
}

header('Location: keyence_logs.php');
exit;
