<?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 empresas WHERE aportante = :email');
        $this->db->query('SELECT * FROM empresas WHERE rif = :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 empresas where aportante = :email');
        $this->db->query('SELECT * FROM empresas where rif = :email');
        $this->db->bind(':email', $email);

        $row = $this->db->single();
        //var_dump($row);
        $hash_password = $row->rif;

        if($password==$hash_password){
            return $row;
        }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;
        }
    }
}