<?php

session_start();

include '../config.php';

$id = trim($_GET['id']);
$lang = trim($_GET['lang']);

$id = strip_tags($id);

//$id = htmlspecialchars($id);



if($lang == "es"){
  $errorcaptcha = "El captcha es inválido.";
  $successmsg = "Datos guardados correctamente. Los cambios pueden tardar unos minutos en reflejarse.";
  $errormsg = "Falló la actualización. Revise los datos e intente nuevamente en unos minutos.";
}

if($lang == "en"){
  $errorcaptcha = "The captcha is not correct.";
  $successmsg = "Data saved successfully. Changes may take a few minutes to reflect.";
  $errormsg = "Update failed. Please review the data and try again in a few minutes.";
}
//echo $id;





if ( isset($_GET['captchaedituser']) && ($_GET['captchaedituser']!="") ){

    // Validation: Checking entered captcha code with the generated captcha code

    if(strcasecmp($_SESSION['captcha2'], $_GET['captchaedituser']) != 0){

    // Note: the captcha code is compared case insensitively.

    // if you want case sensitive match, update the check above to strcmp()

    echo "<p class='errormsg'>".$errorcaptcha."</p> ";

    exit();

    }

  }



$nombre = trim(urldecode($_GET['nombre']));

$area = trim(urldecode($_GET['area']));

$funcion = trim(urldecode($_GET['funcion']));

$permisos = trim($_GET['permisos']);



$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 MyMutation {\\r\\n  updateUsuarioSitioPrivado(data: {nombre: \\"'.$nombre.'\\", area: \\"'.$area.'\\", funcion: \\"'.$funcion.'\\", permisos: \\"'.$permisos.'\\", localizations: {update: {data: {area: \\"'.$area.'\\", funcion: \\"'.$funcion.'\\",  permisos: \\"'.$permisos.'\\", nombre: \\"'.$nombre.'\\"}, locale: es_MX}}}, where: {id: \\"'.$id.'\\"}) {\\r\\n    id\\r\\n  }\\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);

$id = $jsonrespuesta['data']['updateUsuarioSitioPrivado']['id'];

//echo $id;

  $sucess = "<p class='successmsg'>".$successmsg."</p>";
  $fail = "<p class='errormsg'>".$errormsg."</p> ";





if(strlen($id)>10){
    echo $sucess;
    $curlpub = curl_init();
    curl_setopt_array($curlpub, 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 MyMutation {\\r\\n  publishUsuarioSitioPrivado(\\r\\n    where: {id: \\"'.$id.'\\"}\\r\\n    to: PUBLISHED\\r\\n    locales: es_MX\\r\\n  ) {\\r\\n    id\\r\\n  }\\r\\n}\\r\\n","variables":{}}',
        CURLOPT_HTTPHEADER => array(
          'Content-Type: application/json',
          'Cookie: __cfduid=da2693cdfd39b7377a8a05e305da45b8f1613001694'
        ),
  
      ));
  
      $responsepub = curl_exec($curlpub);
      curl_close($curlpub);

}



else{

    echo $fail;

}