<?php
$bucket="https://indeval-webdocs-publico-01122020.s3.amazonaws.com/";
$newimg = trim($_POST['newimg']);

$oldimg = trim($_POST['oldimg']);

$lang = $_GET['lang'];
$id = $_GET['id'];

$enviado = $_GET['enviado'];

$textoTooltip = trim($_POST['textoTooltip']);
$titulo = trim($_POST['titulo']);
$categoria = trim($_POST['categoria']);

$textoTooltip = urlencode( $textoTooltip );

    $curl = curl_init();

    if($lang == "en"){

        curl_setopt_array($curl, array(
            CURLOPT_URL => 'https://api-us-east-1.graphcms.com/v2/ckkxmrn6cw3ga01ywc1spb2qu/master',
            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  updateInstrumento(data: {categoria: \\"'.$categoria.'\\", textoTooltip: \\"'.$textoTooltip.'\\", enviadoRevision: \\"'.$enviado.'\\", titulo: \\"'.$titulo.'\\"}, 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'
            ),
          ));

    }

    if($lang == "es"){
        curl_setopt_array($curl, array(
            CURLOPT_URL => 'https://api-us-east-1.graphcms.com/v2/ckkxmrn6cw3ga01ywc1spb2qu/master',
            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  updateInstrumento(data: {localizations: {update: {data: {categoria: \\"'.$categoria.'\\", enviadoRevision: \\"'.$enviado.'\\", textoTooltip: \\"'.$textoTooltip.'\\", titulo: \\"'.$titulo.'\\"}, 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']['updateInstrumento']['id'];

//echo $id;

  $sucess = "<p class='successmsg' style='text-transform:Capitalize;'>Instrumento actualizado correctamente</p>";
  $fail = "<p class='errormsg'>Falló la actualización..</p> ";


if(strlen($id)>10){

    echo $sucess;

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

else{
    echo $fail;
}

?>