<?php
session_start();

if (isset($_POST['pid']))
{
if (isset($_SESSION['token'])) {
    $token = $_SESSION['token'];
    $pid1=$_POST['pid'];
    //$pid =$_POST[]; // Replace with your actual PID or get it dynamically from your application

    // Ensure $pid is properly sanitized or validated before using in the URL
        $jsonFilePath = __DIR__ . '/../urls/api_endpoints.json';
        $jsonData = file_get_contents($jsonFilePath);
        $endpoints = json_decode($jsonData, true);

        if (isset($endpoints['switch_delete'])) {
            $url = $endpoints['switch_delete'] . $pid1;


    $headers = [
        "Authorization: Bearer $token",
    ];

    $options = [
        'http' => [
            'header'  => implode("\r\n", $headers),
            'method'  => 'DELETE',
            'ignore_errors' => true
        ],
        'ssl' => [
            'verify_peer' => false,
            'verify_peer_name' => false
        ]
    ];

    $context = stream_context_create($options);
    $response = file_get_contents($url, false, $context);

    if ($response !== false) {
       echo '<script>window.location.href = "../configuration/switch.php";</script>';
       exit;
    }
}else 
 {
    $response = json_encode(['status' => 'error', 'message' => 'Failed to fetch data']);

 }

}
}
?>
