<?php
class Usuario{

	protected $id = 0;
	protected $apodo = null;
	protected $nombre = null;
	protected $ap_pat = null;
	protected $ap_mat = null;
	protected $celular = null;
	protected $correo = null;
	protected $estado = null;
	protected $cn = null;

	function __construct(){
		$this->cn = conectarDB();
	}

	protected function autenticar($usuario,$contrasena,$privilegio){
		$query = 'SELECT usu_id AS id, usu_nombre AS nombre, usu_ap_paterno AS ap_paterno, usu_ap_materno AS ap_materno, usu_celular AS celular, usu_correo AS correo, usu_estado AS estado FROM usuario WHERE usu_apodo = ? AND usu_contrasena = ? AND usu_privilegio = ? LIMIT 1;';
		$rs = $this->cn->Execute($query,array($usuario,$contrasena,$privilegio));
		if(!empty($rs) && !$rs->EOF){
			$usuario = $rs->GetArray();
			$rs->Close();
			return $usuario[0];
		}
		return false;
	}

	protected function verificar($usuario,$contrasena,$privilegio){
		$query = 'SELECT usu_id AS id, usu_apodo AS apodo, usu_nombre AS nombre, usu_ap_paterno AS ap_paterno, usu_ap_materno AS ap_materno, usu_celular AS celular, usu_correo AS correo, usu_estado AS estado FROM usuario WHERE usu_apodo = ? AND usu_contrasena = ? AND usu_privilegio = ? LIMIT 1;';
		$rs = $this->cn->Execute($query,array($usuario,$contrasena,$privilegio));
		if(!empty($rs) && !$rs->EOF){
			$usuario = $rs->GetArray();
			$rs->Close();
			$usuario = $usuario[0];
			$this->id = $usuario['id'];
			$this->apodo = $usuario['apodo'];
			$this->nombre = $usuario['nombre'];
			$this->ap_paterno = $usuario['ap_paterno'];
			$this->ap_materno = $usuario['ap_materno'];
			$this->celular = $usuario['celular'];
			$this->correo = $usuario['correo'];
			$this->estado = $usuario['estado'];
			return true;
		}
		return false;
	}

	protected function registrar($usuario,$contrasena,$nombre,$paterno,$materno,$celular,$correo,$privilegio){
		$query = 'INSERT INTO usuario(usu_apodo, usu_contrasena, usu_nombre, usu_ap_paterno, usu_ap_materno, usu_celular, usu_correo, usu_estado, usu_privilegio) VALUES(?, ?, ?, ?, ?, ?, ?, 1, ?);';
		$rs = $this->cn->Execute($query,array($usuario,$contrasena,$nombre,$paterno,$materno,$celular,$correo,$privilegio));
		if($this->cn->Affected_Rows()==1)
			return true;
		return false;
	}

	public function comprobarUsuario($usuario){
		$query = 'SELECT usu_apodo IS NOT NULL FROM usuario WHERE usu_apodo = ? LIMIT 1;';
		$rs = $this->cn->Execute($query,array($usuario));
		if(!empty($rs) && !$rs->EOF){
			$rs->Close();
		}else{
			return true;
		}
		return false;
	}

	public function actualizarDatos($usuario,$contrasena,$nombre,$paterno,$materno,$celular,$correo){
		$query = 'UPDATE usuario SET usu_nombre = ?, usu_ap_paterno = ?, usu_ap_materno = ?, usu_celular = ?, usu_correo = ? WHERE usu_apodo = ? AND usu_contrasena = ?;';
		$rs = $this->cn->Execute($query,array($nombre,$paterno,$materno,$celular,$correo,$usuario,$contrasena));
		if($this->cn->Affected_Rows()==1)
			return true;
		return false;
	}

	public function actualizarContrasena($usuario,$contrasena,$cNueva){
		$query = 'UPDATE usuario SET usu_contrasena = ? WHERE usu_apodo = ? AND usu_contrasena = ?;';
		$rs = $this->cn->Execute($query,array($cNueva,$usuario,$contrasena));
		if($this->cn->Affected_Rows()==1)
			return true;
		return false;
	}

	function __destruct(){
		$this->cn->Close();
	}
}
?>