<?php  

require_once "models/connection.php";
require_once "controllers/post.controller.php";

if(isset($_POST)){

	$response = new PostController();

	if(isset($_GET['login']) && $_GET['login'] == true ){

		$response -> postLogin($table,$_POST);

	}else{

			if($_GET['token']){

				$validate = Connection::tokenValidate($_GET['token'],"usuario_app");

				if($validate == "ok"){
					
					$columns = array();

					foreach(array_keys($_POST) as $key=>$value){

						array_push($columns,$value);

					}

					$existe = Connection::getColumnsData($table,$columns);

					if(empty($existe)){

						$json = array(
							'status'=> 400,
							'result'=> 'Error: Fields in the form do not match the database'
						);

						echo json_encode($json,http_response_code($json['status']));
						return;
					}

					
					$response->postData($table,$_POST);
				}

				if($validate == "no-auth"){

					$json = array(
						'status'=> 400,
						'result'=> 'Error: The user is not authorized'
					);

					echo json_encode($json,http_response_code($json['status']));
					return;
				}

			}else{

				$json = array(
						'status'=> 400,
						'result'=> 'Error: Authorization required'
					);

					echo json_encode($json,http_response_code($json['status']));
					return;
			}

	}

}

?>