<?php

/*function require_auth() {
    
    if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With');
        header('HTTP/1.1 200 OK');
        exit;
    }
    
	$AUTH_USER = 'Dirk.Adm';
	$AUTH_PASS = 'Da#+Q24d1Cr';
	header('Cache-Control: no-cache, must-revalidate, max-age=0');
	$has_supplied_credentials = !(empty($_SERVER['PHP_AUTH_USER']) && empty($_SERVER['PHP_AUTH_PW']));
	$is_not_authenticated = (
		!$has_supplied_credentials ||
		$_SERVER['PHP_AUTH_USER'] != $AUTH_USER ||
		$_SERVER['PHP_AUTH_PW']   != $AUTH_PASS
	);
	if ($is_not_authenticated) {
		header('HTTP/1.1 401 Authorization Required');
		header('WWW-Authenticate: Basic realm="Access denied"');
		exit;
	
    }
}

    require_auth();*/

    // HEADERS
    header('Access-Control-Allow-Origin: *');
    header('Content-Type: application/json');
    header('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With');

if ($_SERVER['REQUEST_METHOD'] == 'GET') {
    
    include_once '../../config/Database.php';
    include_once '../../models/Usuario.php';

    // INSTANTIATE DB AND CONNECT
    $database = new Database();
    $db = $database->connect();
    
    // INSTANTIATE POST OBJECT
    $usuario = new Usuario($db);
    
    // BLOG POST QUERY
    $result = $usuario->read();
    
    // GET ROW COUNT
    $num = $result->rowCount();
    
    // CHECK IF ANY POSTS
    if($num > 0){
        // POST ARRAY
        $usuarios_arr = array();
        $usuarios_arr['data'] = array();

        while($row = $result->fetch(PDO::FETCH_ASSOC)){
            extract($row);

            $usuario_item = array(
                'id' => $id,
                'usuario' => $usuario,
                'senha' => $senha,
                'permissao' => $permissao,
                'bloqueado' => $bloqueado,
                'nome' => $nome
            );

            // PUSH TO 'DATA'
            array_push($usuarios_arr['data'], $usuario_item);
        }

    // TURN TO JSON AND OUTPUT
    echo json_encode($usuarios_arr);

    }else{
        // NO POSTS
        echo json_encode(
            array('message' => 'No Posts Found')
        );
    }
}