<?php
include "../session.php"; 

if($role!="ADMIN"){
  header("Location:index.php");
  die();
}

include "conect.php";

$token = $_POST['tokenCode'];

date_default_timezone_set('America/Guatemala');
$date = date("d-m-Y");
$dateReg = date("Y-m-d");
$hora = date('H:i:s');

$consulta_nueva = mysqli_query($con, "SELECT * FROM token WHERE TOKEN='$token'");
if ($consulta_nueva){

        $conteoNuevo = mysqli_num_rows($consulta_nueva);
    if ($conteoNuevo > 0) {
        while ($row = mysqli_fetch_array($consulta_nueva)) {
            $tokenC = $row['TOKEN'];
            $statusC = $row['STATUS'];
        }


        if ($statusC==1){


            $consulta_status = mysqli_query($con, "SELECT * FROM token WHERE STATUS=1");
            if ($consulta_status){
        
                $conteoStatus = mysqli_num_rows($consulta_status);
            if ($conteoStatus > 0) {
                    while ($row = mysqli_fetch_array($consulta_status)) {
                        $tokenS = $row['TOKEN'];
                        $statusS = $row['STATUS'];
        
                        $actualizacion = mysqli_query($con, "UPDATE token SET STATUS = 0 WHERE STATUS = 1 ");
                        $actualizacion = mysqli_query($con, "UPDATE token SET STATUS = 1 WHERE TOKEN ='$token'");

                    }

                }
            }



            $response2 = array(
                "CodigoRespuesta" => 1,
                "MensajeRespuesta" => $tokenC,
                "MensajeStatus" => $statusC

        
            );

            header('Content-Type: application/json');
            echo json_encode($response2);

            
        }else{

            $consulta_status = mysqli_query($con, "SELECT * FROM token WHERE TOKEN = '$token'");

            $actualizacion = mysqli_query($con, "UPDATE token SET STATUS = 1 WHERE STATUS = 0 AND TOKEN ='$token'");

                if ($consulta_status){

                    $conteoStatus = mysqli_num_rows($consulta_status);
                if ($conteoStatus > 0) {
                        while ($row = mysqli_fetch_array($consulta_status)) {
                            $tokenS = $row['TOKEN'];
                            $statusS = $row['STATUS'];

                        
            }

            $response2 = array(
                "CodigoRespuesta" => 1,
                "MensajeRespuesta" => $tokenC,
                "MensajeStatus" => $statusC

        
            );

            header('Content-Type: application/json');
            echo json_encode($response2);


        }
    }
}
        
        
        
    } else {

        $consulta_status = mysqli_query($con, "SELECT * FROM token WHERE STATUS=1");
        if ($consulta_status){
    
            $conteoStatus = mysqli_num_rows($consulta_status);
        if ($conteoStatus > 0) {
                while ($row = mysqli_fetch_array($consulta_status)) {
    
                    $actualizacion = mysqli_query($con, "UPDATE token SET STATUS = 0 WHERE STATUS = 1 ");

                }

            }
        }
        // Agregar Nuevo Token
        $ingreso = mysqli_query($con, "INSERT INTO token(TOKEN, DATE, STATUS) VALUES ('$token', '$dateReg', 1)");

            if ($ingreso) {
                $consulta_ingresado = mysqli_query($con, "SELECT * FROM token WHERE TOKEN='$token'");

                $conteoIng = mysqli_num_rows($consulta_ingresado);
                if ($conteoIng > 0) {
                    while ($row3 = mysqli_fetch_array($consulta_ingresado)) {
                        $tokenC3 = $row3['TOKEN'];
                    }

                    $response2 = array(
                        "CodigoRespuesta" => 11,
                        "MensajeRespuesta" => $tokenC3
                    );
                    
                    header('Content-Type: application/json');
                    echo json_encode($response2);
                    
                } else {
                    $response2 = array(
                        "CodigoRespuesta" => 2,
                        "MensajeRespuesta" => 'no se ha logrado actualizar'
                    );
                    
                    header('Content-Type: application/json');
                    echo json_encode($response2);
                }
                
            }
            else{
                echo ('No se ha realizado el registro');
            }
        };


}
else {
    $response2 = array(
        "CodigoRespuesta" => -1,
        "MensajeRespuesta" => 'No se ha logrado establecer conexion'
    );
    
    header('Content-Type: application/json');
    echo json_encode($response2);
}



?>