<?php
include '../config.php';
$id = trim($_POST['id']);
$id = strip_tags($id);
$id = htmlspecialchars($id);

$scripten = trim($_POST['scripten']);
$scriptes = trim($_POST['scriptes']);

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => $endpoint_graph_cms,
  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_POSTFIELDS =>'{"query":"mutation editScript {\\r\\n  __typename\\r\\n  updateTrackPixelOScript(data: {script: \\"'.$scripten.'\\", localizations: {update: {data: {script: \\"'.$scriptes.'\\"}, locale: es_MX}}}, where: {id: \\"'.$id.'\\"}) {\\r\\n    id\\r\\n  }\\r\\n}\\r\\n","variables":{}}',
  CURLOPT_HTTPHEADER => array(
    'Content-Type: application/json',
    'Cookie: __cfduid=da2693cdfd39b7377a8a05e305da45b8f1613001694'
  ),
));

$response = curl_exec($curl);

curl_close($curl);
//echo $response;

$jsonrespuesta = json_decode($response, true);
$id2 = $jsonrespuesta['data']['updateTrackPixelOScript']['id'];

//echo $id;


  $sucess = "<p class='successmsg'>Script actualizado</script>";
  $fail = "<p class='errormsg'>Falló la actualización..</p> ";


if(strlen($id2)>10){

    echo $sucess;

    $string = $newstatus;
    $string = '"'.$string.'"';
    echo '<script>
        
        setTimeout(() => {
            $(".popUpScript").fadeOut();
        }, 1500);
    </script>';
}

else{
    echo $fail;
}