<?php
defined('BASEPATH') or exit('Direct access is not allowed');
require_once ROOT . '/trainingportal/app/models/Trainingschedule/TrainingscheduleModel.php';//trainingportal
require_once LIBS_ROUTE .'Session.php';

/**
 * Automotive controller
 */
class TrainingscheduleController extends Controller
{
  /**
   * string 
   */
  public $nombre;
  private $session;
  /**
   * object 
   */
  public $model;

  /**
   * Inicializa valores 
   */
  public function __construct()
  {
    $this->model = new TrainingscheduleModel();
    $this->session = new Session();
  }

  /**
  * Método estándar
  */
  public function exec()
  {
    $this->show();
    
  }

  /**
  * Método de ejemplo
  */
  public function show()
  {
    //$params = array('nombre' => $this->nombre);
    $this->session->init();
    $user_list = $this->model->get_user_list();
    $res_training_list = $this->model->get_training_list($this->session->get('selected_segment')); 
    $res_module_list = $this->model->get_module_list($this->session->get('selected_segment')); 
    $res_schedule_list = $this->model->get_schedule_list($this->session->get('selected_segment')); 
    $sanitizedData = mb_convert_encoding($res_schedule_list, 'UTF-8', 'UTF-8');
    $schedules = json_encode($sanitizedData); 
    $res_schedule_data_count = $this->model->get_schedule_data_count($this->session->get('selected_segment')); 
     $sanitizedschedule_Data = mb_convert_encoding($res_schedule_data_count, 'UTF-8', 'UTF-8');
     $schedules_data_count = json_encode($sanitizedschedule_Data); 
    $params = array('schedules_data_count' => $schedules_data_count,'schedule_list' => $schedules,'user_list' => $user_list,'res_training_list' => $res_training_list,'res_module_list' => $res_module_list,'selected_segment' => $this->session->get('selected_segment'),'email' => $this->session->get('email'),'user_name' => $this->session->get('user_name'),'role_id' => $this->session->get('role_id'),'emp_code' => $this->session->get('emp_code'),'segment_name' => $this->session->get('segment_name'),'rating' => $this->session->get('rating'),'designation' => $this->session->get('designation'),'enroll_courses' => $this->session->get('enroll_courses'),'profile_image_path' => $this->session->get('profile_image_path'));

    $this->render(__CLASS__,$params); 
  }

  /**
  * Método de ejemplo con model. Obtiene un usuario segun ID
  */
  public function usuario($param)
  {
    $res = $this->model->getUser($param);
    $this->nombre = $res['usuario_dev'];
    $this->show();
  }

  public function get_traning_schedule($param){
    $userId=$this->session->get('user_id');
    $segment_id=$this->session->get('selected_segment');
    $segment_name=$this->session->get('segment_name');
    $res = $this->model->get_schedule_list($segment_id);
    $sanitizedData = mb_convert_encoding($res, 'UTF-8', 'UTF-8');
    $jsonRes2 = json_encode($sanitizedData);
    if ($jsonRes2 === false) {
      throw new Exception("Error encoding data to JSON: " . json_last_error_msg());
     }
     echo $jsonRes2;
  }


  public function get_traning_schedule_data_count($param){
    $userId=$this->session->get('user_id');
    $segment_id=$this->session->get('selected_segment');
    $segment_name=$this->session->get('segment_name');
    $res = $this->model->get_schedule_data_count($segment_id);
    $sanitizedData = mb_convert_encoding($res, 'UTF-8', 'UTF-8');
    $jsonRes2 = json_encode($sanitizedData);
    if ($jsonRes2 === false) {
      throw new Exception("Error encoding data to JSON: " . json_last_error_msg());
     }
     echo $jsonRes2;
  }


  public function create_traning_schedule($param){
    $userId=$this->session->get('user_id');
    $segment_id=$this->session->get('selected_segment');
    $segment_name=$this->session->get('segment_name');
    $res = $this->model->save_training_schedule($param,$userId,$segment_id,$segment_name);
    echo json_encode($res);
  }

  
  public function delete_traning_schedule($param){
    $userId=$this->session->get('user_id');
    $segment_id=$this->session->get('selected_segment');
    $segment_name=$this->session->get('segment_name');
    $res = $this->model->delete_training_schedule($param,$userId,$segment_id,$segment_name);
  }
  public function herfpage(){
    $this->render("MainController",$params); 
    //echo "MeThod";
  }

}