<?php
    session_start();
    if(strlen($_COOKIE['persistLog'])<5){
        header("Location: /admin");
    }   

    if(isset($_COOKIE['persistID']))
    {
        $userid = $_COOKIE['persistID'];
        $_SESSION['user'] = $userid;
    }
    if($userid == ''){
        header("Location: /admin");
    }

    $passUpdated =  $_COOKIE['userPassUpdated'];

    if(strlen( $_SESSION['requestDataUser22'] ) < 10){

        $correo = $userid;

        if(strlen($correo) > 5){
            $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":"query MyQuery {\\r\\n  usuariosAdmin(where: {email: \\"'.$correo.'\\"}) {\\r\\n    id\\r\\n  resetPass\\r\\n fechaCambioPassword\\r\\n rol\\r\\n  }\\r\\n}\\r\\n","variables":{}}',
            CURLOPT_HTTPHEADER => array(
              'Content-Type: application/json',
              'Cookie: __cfduid=da2693cdfd39b7377a8a05e305da45b8f1613001694'
            ),
          ));

          $response = curl_exec($curl);
          $_SESSION['requestDataUser'] = $response;
          curl_close($curl);
          //echo $response;

        }

        
    }
    else{
        $response = $_SESSION['requestDataUser'];
    }
    
    $jsonrespuesta = json_decode($response, true);
    $id = $jsonrespuesta['data']['usuariosAdmin'][0]['id'];
    $resetPass = $jsonrespuesta['data']['usuariosAdmin'][0]['resetPass'];
    $fechaPass = $jsonrespuesta['data']['usuariosAdmin'][0]['fechaCambioPassword'];
    $rol = $jsonrespuesta['data']['usuariosAdmin'][0]['rol'];

    if(strlen($passUpdated) < 5){
        if($resetPass == '1' || $resetPass == 1 ){
            header("Location: /admin/cambio-password");
            //echo "<script>window.location = '/admin/cambio-password'</script>";
        }  
 
    }
    //echo $fechaPass;

    $ts1 = $fechaPass;
    $ts2 = time();     
    $seconds_diff = $ts2 - $ts1;                            
    $dias = ($seconds_diff/86400);

    //echo $dias;

    if($dias > 89){
        //Activa el Flag de cambio de Password después de 90 días del último cambio

        $curlpass = curl_init();
        curl_setopt_array($curlpass, 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  updateUsuarioAdmin(data: {resetPass: \\"1\\", ultimaActividad: \\"'.$ts2.'\\", localizations: {update: {data: {resetPass: \\"1\\", ultimaActividad: \\"'.$ts2.'\\"}, 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'
            ),
          ));

          $responsepass = curl_exec($curlpass);
          curl_close($curlpass);
    }

?>