<?php

class User {

    private $db;

    public function __construct()

    {

        $this->db = new Database;

    }



    //register new user

    public function register($data){

        $this->db->query('INSERT INTO user (name, email, password) VALUES (:name, :email, :password)');

        $this->db->bind(':name', $data['name']);

        $this->db->bind(':email', $data['email']);

        $this->db->bind(':password', $data['password']);



        if($this->db->execute()){

            return true;

        }else{

            return false;

        }

    }

    //find user by email

    public function findUserByEmail($email){

        $this->db->query('SELECT * FROM usuarios WHERE usuario = :email');

        $this->db->bind(':email', $email);

        $row = $this->db->single();



        //check the row 

        if($this->db->rowCount() > 0){

            return true;

        }else{

            return false;

        }

    }



    public function login($email, $password){

//        $this->db->query('SELECT * FROM user where email = :email');

        $this->db->query('SELECT * FROM usuarios where usuario = :email');

        $this->db->bind(':email', $email);

        $row = $this->db->single();



        $hash_password = $row->contrasena;

        $viene_password=sha1($password);

//var_dump($row);

//echo '<br><br>'.$hash_password.' - '.$viene_password;

//exit;

//        if(password_verify($password, $hash_password)){

        if($viene_password==$hash_password){

            return $row;

        }else{

            return false;

        }

    }



    public function vertipo($tipo){

        if($tipo->tipo=='Profesor'){

            return true;

        }else{

            return false;

        }

    }



    public function getUserById($id){

        $this->db->query('SELECT * FROM user WHERE id = :id');

        $this->db->bind(':id', $id);



        $row = $this->db->single();



        return $row;

    }



    public function Instructores(){

        $this->db->query('SELECT cedula,apellidos,nombres,estatus,email FROM profesores where estatus="ACTIVO"');

        //$this->db->bind(':id', $id);

        $result = $this->db->resultSet();

        return $result;

    }



    public function InstructoresIns($data){

    var_dump($data);

        $this->db->query('INSERT INTO user (cedula,name, email, password,tipo) VALUES (:cedula,:name, :email, :password, :tipo)');

        $this->db->bind(':cedula', $data['cedula']);

        $this->db->bind(':name', $data['name']);

        $this->db->bind(':email', $data['email']);

        $this->db->bind(':password', $data['password']);

        $this->db->bind(':tipo', 'Profesor');

        if($this->db->execute()){

            return true;

        }else{

            return false;

        }

    }

}