<?php
header('Content-Type: application/json');
require_once('includes/definition.php');
require_once('includes/encrypt.php');
$decryp=new encrypthelper();
class login_controller
{
	private $db;
	private $query;
	private $validatename;
	private $validatename1;
	private $validatename2;

	public function __construct(){
		$this->db=Conectar::conexion();
		$this->query=array();
		$this->validatename=array();
		$this->validatename1=array();
		$this->validatename2=array();
	}

	public function login(){
		$decryp=new encrypthelper();
		$name_user=strip_tags(trim($_POST['name_user']));
		$pass=strip_tags(trim($_POST['pass']));

		if (empty($name_user) || empty($pass)) {
			$datos = array('msg' =>'Todos los campos son obligatorios','status' => 'F');
			return $datos;
		}
		if (strlen($name_user) > 30) {
			$datos = array('msg' =>'Nombre de usuario máximo 30 caracteres','status' => 'F');
			return $datos;
		}
		if (strlen($pass) < 8) {
			$datos = array('msg' =>'Contraseña mínima 8 caracteres','status' => 'F');
			return $datos;
		}
		$opciones = [
		'cost' => 12,
		];
		$clave=password_hash($pass, PASSWORD_BCRYPT, $opciones);
		$consulta1=$this->db->query("SELECT * FROM users WHERE systems_name='$name_user' AND id_status='1'"); 
		while($filas=$consulta1->fetch_assoc()){
			$this->validatename[]=$filas;
		}
		$count=$this->validatename;
		if (count($count)>0) {
			if (!password_verify($pass, $count[0]['user_pass'])) {
				$datos1 = array('msg' => 'Contraseña invalida','status' => 'F','datos' => '');
				return $datos1;
			} 
			session_start();
			session_regenerate_id();
			$x=$count[0]['id_user'].$count[0]['user_pass'];
			$s=$count[0]['id_user'];
			$token=$decryp->encode($x);
			$user=$s.'/';
			$user=$decryp->encode($user);
		    $user=password_hash($user, PASSWORD_BCRYPT, $opciones);
			$token=md5($token);
			$consulta=$this->db->query("UPDATE users SET token='$token',token_user='$user' WHERE id_user='$s'");
			$_SESSION['bn_name_system_global'] = $count[0]['user_name'];
			$_SESSION['bn_user_global'] = $s;
			$_SESSION['bn_name'] = $count[0]['systems_name'];
			$_SESSION['bn_perfil'] = $count[0]['id_profile'];
			$_SESSION['bn_token_system_global'] = $token;
			$_SESSION['bn_token_global'] = md5(date('Y-m-d H:i:s').$token);
			$_SESSION['bn_verify_global'] = $user;
			$datos1 = array('msg' => 'Successfully Login','status' => 'T','datos' => '');
		}else{
			$datos1 = array('msg' => 'Usuario no existe','status' => 'F','datos' => '');
		}
		return $datos1;

	}


}
$solicitud = $_SERVER['REQUEST_METHOD'];
if(!isset($_POST['option']) || empty($_POST['option']) || $solicitud!="POST"){
	$datos = array('msg' => 'Error method','status' => 'F','datos' => '');
	print (json_encode(['status' => $datos['status'],'msg' => $datos['msg'],'datos' => $datos['datos']]));	
}else{
	$option = filter_input(INPUT_POST, 'option');
	$metodo=$_POST['option'];
	$metodo=$decryp->decode($metodo);
	$per=new login_controller();
	$valid1=method_exists($per, $metodo);
	$datos1 = array('msg' => 'Error method','status' => 'F','datos' => '');
	$datos=($valid1) ? $per->$metodo() : $datos1;
	print (json_encode(['status' => $datos['status'],'msg' => $datos['msg'],'datos' => $datos['datos']]));
}

?>
