<?php
defined('BASEPATH') or exit('Direct access is not allowed');
/**
 * Pne Model
 */
class PneModel extends Model
{
  /**
  * Inicia conexión DB
  */
  public function __construct()
  {
    parent::__construct();
  }

  /**
  * Obtiene el usuario de la DB por ID
  */

  public function get_pne_course_data()
  { 
    $array_all_course = array(); 
    $training_details_result = $this->db->query("SELECT * FROM training_details where segment_id=3 order by id desc");
    $training_details_result->fetch_all(MYSQLI_ASSOC);
    $i = 0;
    foreach($training_details_result as $training_details_result_obj) {
      $trainingid=$training_details_result_obj['id'];
      $segmentid=$training_details_result_obj['segment_id'];
      $days=$training_details_result_obj['days'];
      $marks=$training_details_result_obj['marks'];
      $total_modules=$training_details_result_obj['total_modules'];
      $segment_name=$training_details_result_obj['segment_name'];
      $created_by=$training_details_result_obj['created_by'];
      $created_date=$training_details_result_obj['created_date'];
      $model_result = $this->db->query("SELECT * FROM training_module where training_id=$trainingid and segment_id=$segmentid");
      $model_result->fetch_all(MYSQLI_ASSOC);
      $training_details_result_obj["model_data"]=$model_result;
      $array_all_course[$i]=$training_details_result_obj;
      $i++;
    }
    return $array_all_course;
  }

  

  public function get_module_data($data)
  { 
    $array_all_data = array(); 
    $module_id=$data['module_id'];
    $course_id=$data['course_id'];
    $module_data_result = $this->db->query("SELECT * FROM training_module where id=$module_id and training_id= $course_id and segment_id=3");
    $module_data_result->fetch_all(MYSQLI_ASSOC);
    $i = 0;
    foreach($module_data_result as $module_data_result_obj) {
      $module_id=$module_data_result_obj['id'];
      $course_id=$module_data_result_obj['training_id'];
      $segment_id=$module_data_result_obj['segment_id'];

      $content_result = $this->db->query("SELECT * FROM training_content where module_id=$module_id  and training_id=$course_id and segment_id= $segment_id order by id desc");
      $content_result->fetch_all(MYSQLI_ASSOC);
      $content_data = array(); 
      $content_id = 0;
      foreach($content_result as $content_result_obj) {
            $data=$content_result_obj;
            $content_data[$content_id]=$content_result_obj;
            $content_id++;
      }
      $module_data_result_obj["model_content"]=$content_data;
      $array_all_data[$i]=$module_data_result_obj;

      $content_approval = $this->db->query("SELECT * FROM segments where id=3");
      $content_approval->fetch_all(MYSQLI_ASSOC);
      $content_approval_data = array(); 
      $approval_count = 0;
      foreach($content_approval as $content_approval_obj) {
            $data=$content_approval_obj;
            $content_approval_data[$approval_count]=$content_approval_obj;
            $approval_count++;
      }
      $module_data_result_obj["approvel_data"]=$content_approval_data;
      $array_all_data[$i]=$module_data_result_obj;

      $i++;
    }
    return $array_all_data;
  }

  public function apply_training($data,$userId,$user_name){ 
    $sql_query="INSERT INTO `training_executer` (`training_name`, `training_id`,`module_name`,`module_id`,`segment_id`,`segment_name`, `training_mode`, `applied_date`, `assigned_trainer`, `assigned_manager`,  `applied_user_id`, `applied_user_name`, `approval_by_team_lead`, `approval_by_team_lead_id`, `approval_by_bu_head`, `approval_by_bu_head_id`, `approval_by_co_seg_head`, `approval_by_co_seg_head_id`, `approval_by_seg_head`, `approval_by_seg_head_id`, `approval_by_seg_leader`, `approval_by_seg_leader_id`, `is_active`) VALUES 
              ( '".$data["training_name"]."', ".$data["training_id"].",'".$data["module_name"]."',".$data["module_id"].",'".$data["segment_id"]."','".$data["segment_name"]."', '".$data["training_mode"]."', '".date("Y/m/d")."', '".$data["trainer_id"]."', '".$data["team_lead_id"]."', '$userId', '$user_name', 'pending',  ".$data["team_lead_id"].", 'pending', ".$data["bu_id"].", 'pending', ".$data["co_seg_head_id"].", 'pending', ".$data["seg_head_id"].",'pending',".$data["seg_president_id"].",1)";
       $result = $this->db->query($sql_query);
   if ($result === TRUE) {
      return 1;
  } else {
      return 0;
  }
  }





  // public function getUser($id)
  // {
  //   return $this->db->query("SELECT * FROM `usuario` WHERE `id_dev` = $id")->fetch_array(MYSQLI_ASSOC);
  // }

}