<?php
class Users extends Controller{
    public function __construct()
    {
        $this->userModel = $this->model('User');
    }

    public function register(){
        if ($_SERVER['REQUEST_METHOD'] == 'POST'){
           // process form
           $_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING); 
            $data = [
                'name' => trim($_POST['name']),
                'email' => trim($_POST['email']),
                'password' => trim($_POST['password']),
                'confirm_password' => trim($_POST['confirm_password']),
                'name_err' => '',
                'email_err' => '',
                'password_err' => '',
                'confirm_password_err' => '' 
            ];

            //valide name
            if(empty($data['name'])){
                $data['name_err'] = 'Please enter name';
            }

            //validate email
            if(empty($data['email'])){
                $data['email_err'] = 'Please enter email';
            }else{
                //check for email
                if($this->userModel->findUserByEmail($data['email'])){
                    $data['email_err'] = 'Email already exist';
                }
            }

            //validate password 
            if(empty($data['password'])){
                $data['password_err'] = 'Please enter your password';
            }elseif(strlen($data['password']) < 6){
                $data['password_err'] = 'Password must be atleast six characters';
            }

            //validate confirm password
            if(empty($data['confirm_password'])){
                $data['confirm_password_err'] = 'Please confirm password';
            }else{
                if($data['password'] != $data['confirm_password'])
                {
                    $data['confirm_password_err'] = 'Password does not match';
                }
            }

            //make sure error are empty
            if(empty($data['name_err']) && empty($data['email_err']) && empty($data['password_err']) && empty($data['password_confirm_err'])){
                $data['password'] = password_hash($data['password'], PASSWORD_DEFAULT);
                if($this->userModel->register($data)){
                    flash('register_success', 'you are registerd you can login now');
                    redirect('users/login');
                }
            }else{
                $this->view('users/register', $data);
            }
        }else{
            //init data
            $data = [
                'name' => '',
                'email' => '',
                'password' => '',
                'confirm_password' => '',
                'name_err' => '',
                'email_err' => '',
                'password_err' => '',
                'confirm_password_err' => '' 
            ];
            //load view
            $this->view('users/register', $data);          
        }
    }

    public function login(){
        if ($_SERVER['REQUEST_METHOD'] == 'POST'){

           $data = [
               'email' => trim($_POST['email']),
               'password' => trim($_POST['password']),
               'email_err' => '',
               'tipo_err' => '',
               'password_err' => ''
           ];

 $_POST=[];
            //validate email
            if(empty($data['email'])){
                $data['email_err'] = 'Escriba un usuario';
                                            $this->view('users/login', $data);
                EXIT;
            }else{
                if($this->userModel->findUserByEmail($data['email'])){
                    //user found
                }else{
                    $data['email_err'] = 'Usuario NO encontrado...';
                                                $this->view('users/login', $data);
                EXIT;
                }
            }

            //validate password
            if(empty($data['password'])){
                $data['email_err'] = 'Escriba un password';
                $this->view('users/login', $data);
                EXIT;
/*            }elseif(strlen($data['password']) < 6){
                $data['email_err'] = 'Password mayor 6 digitos';
                            $this->view('users/login', $data);
                EXIT;*/
            }
            //var_dump($data);
 //          exit;
            //make sure error are empty



            if(empty($data['email_err']) ){
                $loggedInUser = $this->userModel->login($data['email'], $data['password']);

                    var_dump($loggedInUser);

                if($loggedInUser){
                    //create session
                    $this->createUserSession($loggedInUser);
                }else{
                    $data['email_err'] = 'Password incorrecto...';
                    $this->view('users/login', $data);
                }
            }else{
//     var_dump($data);
// EXIT;
                $this->view('users/login', $data);
            }

        }else{
            //init data f f
            $data = [
                'email' => '',
                'password' => '',
                'email_err' => '',
                'password_err' => ''
            ];
            //load view
            $this->view('users/login', $data);          
        }
    }

    //setting user section variable
    public function createUserSession($user){
        $_SESSION['session'] = encrypt_decrypt('encrypt', $user->id);
        $_SESSION['name'] = $user->codigo;
        $_SESSION['email'] = $user->email;
        $_SESSION['razon'] = $user->razon;
        //var_dump($user);
        //exit;
        redirect('Tableros/');
    }

    //logout and destroy user session
    public function logout(){
        unset($_SESSION['user_id']);
        unset($_SESSION['name']);
        unset($_SESSION['email']);
        unset($_SESSION['tipo']);
        session_destroy();
        redirect('users/login');
    }

    public function Act_prof(){
    $ver_prof = $this->userModel->Instructores();

    foreach ($ver_prof as $p){
            $dataa = [
            'password' => password_hash($p->cedula, PASSWORD_DEFAULT),
            'name' => $p->apellidos.' '.$p->nombres,
            'email' => $p->email,
            'cedula' => $p->cedula,
            ];
    var_dump($dataa);
    $this->userModel->InstructoresIns($dataa);
    //$dataa=[];
    }
    echo "Listo";
    }


}