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

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

  public function get_user_list()
  {
    $result = $this->db->query("SELECT * FROM usuarios ORDER BY user_name ASC");
    return $result->fetch_all(MYSQLI_ASSOC);
  }


  public function get_segment_list()
  {
    $result = $this->db->query("SELECT * FROM segments ORDER BY segment_name ASC");
    return $result->fetch_all(MYSQLI_ASSOC);
  }

  public function get_training_list($selected_segment_id)
  {
    $query="";
    if(empty($selected_segment_id)){
      $query="SELECT * FROM training_details ORDER BY name ASC";
    }else{
      $query="SELECT * FROM training_details where segment_id=$selected_segment_id ORDER BY name ASC"; 
    }
    $result = $this->db->query($query);
    return $result->fetch_all(MYSQLI_ASSOC);
  }

  public function get_module_list($selected_segment_id)
  { $query="";
    if(empty($selected_segment_id)){
      $query="SELECT * FROM training_module order by id desc";
    }else{
      $query="SELECT * FROM training_module where segment_id=$selected_segment_id order by id desc"; 
    }
    $result = $this->db->query($query);//ORDER BY module_name ASC
    return $result->fetch_all(MYSQLI_ASSOC);
  }
  
  public function get_module_all_data($selected_segment_id)
  { $query="";
    if(empty($selected_segment_id)){
      $query="SELECT * FROM training_module ORDER BY id DESC";
    }else{
      $query="SELECT * FROM training_module where segment_id=$selected_segment_id ORDER BY id DESC"; 
    }
    $result = $this->db->query($query);
    return $result->fetch_all(MYSQLI_ASSOC);
  }

  public function get_training_details_all_data($selected_segment_id)
  { $query="";
    if(empty($selected_segment_id)){
      $query="SELECT * FROM training_details ORDER BY id DESC";
    }else{
      $query="SELECT * FROM training_details where segment_id=$selected_segment_id ORDER BY id DESC"; 
    }
    $result = $this->db->query($query);
    return $result->fetch_all(MYSQLI_ASSOC);
  }

  public function get_targets_all_data($selected_segment_id)
  {  $query="";
    if(empty($selected_segment_id)){
      $query="SELECT * FROM targets ORDER BY name ASC";
    }else{
      $query="SELECT * FROM targets where segment_id=$selected_segment_id ORDER BY name ASC"; 
    }
    $result = $this->db->query($query);
    return $result->fetch_all(MYSQLI_ASSOC);
  }

  public function get_trainees_all_data($selected_segment_id)
  {
    $query="";
    if(empty($selected_segment_id)){
      $query="SELECT * FROM trainees ORDER BY name ASC";
    }else{
      $query="SELECT * FROM trainees where segment_id=$selected_segment_id ORDER BY name ASC"; 
    }
    $result = $this->db->query($query);
    return $result->fetch_all(MYSQLI_ASSOC);
  }

  public function get_training_content_all_data($selected_segment_id)
  {
    $query="";
    if(empty($selected_segment_id)){
      $query="SELECT * FROM training_content ORDER BY name ASC";
    }else{
      $query="SELECT * FROM training_content where segment_id=$selected_segment_id ORDER BY name ASC"; 
    }
    $result = $this->db->query($query);
    return $result->fetch_all(MYSQLI_ASSOC);
  }

  public function get_trainees_list($selected_segment_id)
  {
    $query="";
    if(empty($selected_segment_id)){
      $query="SELECT * FROM trainees ORDER BY name ASC";
    }else{
      $query="SELECT * FROM trainees where segment_id=$selected_segment_id ORDER BY name ASC"; 
    }
    $result = $this->db->query( $query);
    return $result->fetch_all(MYSQLI_ASSOC);
  }

  public function save_training_details($data,$userId){ 
    $sql_query="INSERT INTO `training_details` (`name`, `days`, `marks`, `total_modules`, `segment_id`, `segment_name`, `created_by`, `created_date`,`is_active`) VALUES ('".$data["training_name"]."', ".$data["total_days"].",".$data["total_marks"].", ".$data["total_modules"].", ".$data["segment_id"].", '".$data["segment_name"]."', $userId,  '".date("Y/m/d")."', '1')";
    $result = $this->db->query($sql_query);
   if ($result === TRUE) {
      return 1;
  } else {
      return 0;
  }
  }

  public function save_module($data,$userId){ 
    $filename=$_FILES['image_data']['name'];
    $location = ROOT . "/trainingportal/app/views/assets/images/bg/".$_FILES['image_data']['name'];

    $file_path= ROOT . "/trainingportal/app/views/assets/images/bg/".$data["segment_id"]."_".$data["training_id"]."";
    $file_path_store="app/views/assets/images/bg/".$data["segment_id"]."_".$data["training_id"]."/".$_FILES['image_data']['name'];
    $file_path_copy=ROOT . "/trainingportal/app/views/assets/images/bg/".$data["segment_id"]."_".$data["training_id"]."/".$_FILES['image_data']['name'];
    if (!file_exists($file_path)) {
      mkdir($file_path, 0777, true);
     }
    move_uploaded_file($_FILES['image_data']['tmp_name'], $file_path_copy);

    $sql_query="INSERT INTO `training_module` (`module_name`, `training_name`, `training_id`, `segment_id`, `module_target`, `segment_name`,  `module_trainees`, `trainees_traget_id`, `module_trainer`, `created_by`, `created_date`, `is_active`, `sort_index`, `image_path`, `trainer_id`) VALUES 
    ('".$data["module_name"]."', '".$data["training_name"]."', ".$data["training_id"].", ".$data["segment_id"].", '".$data["module_target"]."','".$data["segment_name"]."','".$data["trainee_target_name"]."','".$data["trainee_target_id"]."', '".$data["module_trainer"]."', $userId, '".date("Y/m/d")."', '1', ".$data["module_sort_index"].", '$file_path_store', '".$data["trainer_id"]."')";
       $result = $this->db->query($sql_query);
   if ($result === TRUE) {
      return 1;
  } else {
      return 0;
  }
  }
//INSERT INTO `targets` (`id`, `name`, `training_id`, `training_name`, `module_id`, `module_name`, `segment_id`, `segment_name`, `created_by`, `created_date`, `updated_by`, `updated_date`, `deleted_by`, `deleted_date`, `is_active`, `sort_index`) VALUES (<{id: }>, <{name: }>, <{training_id: }>, <{training_name: }>, <{module_id: }>, <{module_name: }>, <{segment_id: }>, <{segment_name: }>, <{created_by: }>, <{created_date: }>, <{updated_by: }>, <{updated_date: }>, <{deleted_by: }>, <{deleted_date: }>, <{is_active: }>, <{sort_index: }>);
public function save_target($data,$userId){ 
  $sql_query="INSERT INTO `targets` (`name`, `training_id`, `training_name`,  `segment_id`, `segment_name`, `created_by`, `created_date`,`is_active`, `sort_index`) VALUES 
        ('".$data["target_name"]."', ".$data["training_id"].", '".$data["training_name"]."', ".$data["segment_id"].",'".$data["segment_name"]."', $userId, '".date("Y/m/d")."', '1', ".$data["target_sort_index"].")";
  $result = $this->db->query($sql_query);
 if ($result === TRUE) {
    return 1;
} else {
    return 0;
}
}

//INSERT INTO `trainingportal`.`trainees` (`id`, `name`, `segment_id`, `segment_name`, `created_by`, `created_date`, `updated_by`, `updated_date`, `deleted_by`, `deleted_date`, `sort_index`) VALUES (<{id: }>, <{name: }>, <{segment_id: }>, <{segment_name: }>, <{created_by: }>, <{created_date: }>, <{updated_by: }>, <{updated_date: }>, <{deleted_by: }>, <{deleted_date: }>, <{sort_index: }>);
public function save_trainees($data,$userId){ 
  $sql_query="INSERT INTO `trainees` (`name`, `segment_id`, `segment_name`, `created_by`, `created_date`, `sort_index`,`is_active`) VALUES 
        ('".$data["trainees_name"]."', ".$data["segment_id"].", '".$data["segment_name"]."', $userId, '".date("Y/m/d")."', ".$data["trainees_sort_index"].",'1')";
  $result = $this->db->query($sql_query);
 if ($result === TRUE) {
    return 1;
} else {
    return 0;
}
}


public function save_training_content($data,$userId){ 
  $sql_query="INSERT INTO `training_content` (`name`, `module_id`, `module_name`, `training_id`, `training_name`, `segment_id`,`segment_name`,`created_by`,`created_date`,`is_active`,`sort_index`) VALUES 
        ('".$data["content_name"]."', ".$data["model_id"].", '".$data["model_name"]."', ".$data["training_id"].", '".$data["training_name"]."',".$data["segment_id"].",'".$data["segment_name"]."',$userId,'".date("Y/m/d")."', '1',".$data["content_sort_index"].")";
  $result = $this->db->query($sql_query);
 if ($result === TRUE) {
    return 1;
} else {
    return 0;
}
}

}