<?php

$id = trim($_POST['id']);
$id = strip_tags($id);
$id = htmlspecialchars($id);

$status = trim($_POST['status']);
$status = strip_tags($status);
$status= htmlspecialchars($status);

if($status == 'entregado')
    $newstatus = 1;{
}
if($status == 'enespera')
    $newstatus = 0;{
}

$curl = curl_init();

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  __typename\\r\\n  updateEntradaFormulario(where: {id: \\"'.$id.'\\"}, data: {statusForm: \\"'.$newstatus.'\\"}) {\\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);
$id = $jsonrespuesta['data']['updateEntradaFormulario']['id'];

//echo $id;


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


if(strlen($id)>10){

    echo $sucess;

    if(strlen($status) <1 || $status == 'enespera'){
        $newstatus = "<span class='statusenespera'>En espera</span>";
    }
    if($status== 'cancelado'){
        $newstatus = "<span class='statuscancelado'>Cancelado</span>";
    }
    if($status== 'entregado'){
        $newstatus = "<span class='statusentregado'>Entregado</span>";
    }

    $string = $newstatus;
    $string = '"'.$string.'"';
    echo '<script>
        $("#status'.$id.'").html('.$string.');
        setTimeout(() => {
            $(".popUpStatus").fadeOut();
        }, 1500);
    </script>';
}

else{
    echo $fail;
}