<?php
include '../config.php';  include '../checkSession.php';
$id = $_GET['id'];
$status = 'eliminado';
$modelo = 'UsuarioSitioPrivado';


$endpoint =  $endpoint_graph_cms;
if(strlen($id)>10 && strlen($status)>2 && strlen($modelo)>2){

    $curl = curl_init();

    if($status =='eliminado'){
        curl_setopt_array($curl, array(
            CURLOPT_URL => $endpoint,
            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  update'.$modelo.'(data: {estatus: \\"'.$status.'\\", localizations: {update: {data: {estatus: \\"'.$status.'\\"}, locale: es_MX}}}, where: {id: \\"'.$id.'\\"}) {\\r\\n    id\\r\\n  }\\r\\n  unpublish'.$modelo.'(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'
            ),
          ));
    }


        

    $modeloupdate = 'update'.$modelo;
    $response = curl_exec($curl);
    curl_close($curl);
    //echo $response;
    
    $jsonrespuesta = json_decode($response, true);
    $id = $jsonrespuesta['data'][$modeloupdate]['id'];

    //echo $id;

    $sucess = "<p class='successmensaje'>Status actualizado correctamente</p>";
    $fail = "<p class='errormensaje'>Falló la actualización de status...<span style='display:none;'>".$response."</span></p> ";


    if(strlen($id)>10){

        

        if($status== 'eliminado'){
            echo '<script>
                    $("body table .'.$id.'").fadeOut();
                    $(".'.$id.'").addClass("oculto");
                 </script>';
            exit();     
            
        }

        echo $sucess;

        if($status == 'oculto'){
            $newstatus = "<span class='statusenespera'>Oculto</span>";
        }
        
        if($status== 'publicado'){
            $newstatus = "<span class='statusentregado'>Publicado</span>";
        }
    
        $string = $newstatus;
        $string = '"'.$string.'"';
        echo '<script>
            $("#status'.$id.'").html('.$string.');
            setTimeout(() => {
                
                $(".popUpStatus").fadeOut();
            }, 1500);
        </script>';
    }

    else{
        echo $fail;
    }


}