<?php
defined('BASEPATH') or exit('Direct access is not allowed');

require_once ROOT . '/trainingportal/app/models/Login/LoginModel.php';
require_once LIBS_ROUTE .'Session.php';

/**
* Login controller
*/
class LoginController extends Controller
{
  private $model;

  private $session;

  public function __construct()
  {
    $this->model = new LoginModel();
    $this->session = new Session();
  }

  public function exec()
  {

    $this->render(__CLASS__);
  }

  public function signin($request_params)
  {
    if($this->verify($request_params))
      return $this->renderErrorMessage('El email y password son obligatorios');

    $result = $this->model->signIn($request_params['email']);

    if(!$result->num_rows)
      return $this->renderErrorMessage("El email {$request_params['email']} no fue encontrado");

    $result = $result->fetch_object();

    if(!password_verify($request_params['password'], $result->password))
      return $this->renderErrorMessage('La contraseña es incorrecta');
      $segment_data = $this->model->get_current_segment_data($this->session->get('session_seg_name'));
      if (empty($segment_data)) {
       // header('location: /trainingportal/Home'); 
            $this->session->init();
      $this->session->add('selected_segment', $segment_data[0]["id"]);
      $this->session->add('user_id', $result->id);
      $this->session->add('role_id', $result->role_id);
      $this->session->add('email', $result->email);
      $this->session->add('user_name', $result->user_name);
      $this->session->add('emp_code', $result->emp_code);
      $this->session->add('segment_name', $result->segment_name);
      $this->session->add('profile_image_path', $result->profile_image_path);
      $this->session->add('rating', $result->rating);
      $this->session->add('designation', $result->designation);
      $this->session->add('enroll_courses', $result->enroll_courses);
      header('location: /trainingportal/'.$this->session->get('session_seg_name')); 
    } else 
    {
      $this->session->init();
      $this->session->add('selected_segment', $segment_data[0]["id"]);
      $this->session->add('user_id', $result->id);
      $this->session->add('role_id', $result->role_id);
      $this->session->add('email', $result->email);
      $this->session->add('user_name', $result->user_name);
      $this->session->add('emp_code', $result->emp_code);
      $this->session->add('segment_name', $result->segment_name);
      $this->session->add('profile_image_path', $result->profile_image_path);
      $this->session->add('rating', $result->rating);
      $this->session->add('designation', $result->designation);
      $this->session->add('enroll_courses', $result->enroll_courses);
      header('location: /trainingportal/'.$this->session->get('session_seg_name')); 
    }
    
  }

  public function set_segment_name($request_params)
  {
    $this->session->init();
    $this->session->add('session_seg_name', $request_params['segment_name_for_session']);
    echo $this->session->get('session_seg_name');
  }


  private function verify($request_params)
  {
    return empty($request_params['email']) OR empty($request_params['password']);
  }

  private function renderErrorMessage($message)
  {
    $params = array('error_message' => $message);
    $this->render(__CLASS__, $params);
  }

}