<?php
defined('BASEPATH') or exit('Direct access is not allowed');
/**
 * Automotive Model
 */
class SettingsModel 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 where is_active = '1' ORDER BY user_name ASC");
    return $result->fetch_all(MYSQLI_ASSOC);
  }
    public function update_password($data,$userId,$segment_id,$segment_name){ 
      $current_password=password_hash($data["current_password"], PASSWORD_DEFAULT);
      $new_password=password_hash($data["new_password"], PASSWORD_DEFAULT);
      $confirm_password=password_hash($data["confirm_password"], PASSWORD_DEFAULT);

      $check_query="SELECT * FROM usuarios where is_active= '1' and id='$userId'";
      $check_result = $this->db->query($check_query);

      $check_data = $check_result->fetch_all(MYSQLI_ASSOC);
     // $ispassword_correct=password_verify($data["current_password"],$check_data[0]['password']);
      if (password_verify($data["current_password"], $check_data[0]['password'])) {
                $sql_query="UPDATE `usuarios` SET `password` = '$confirm_password' WHERE (`id` = '$userId')";
                $result = $this->db->query($sql_query);
                  if ($result === TRUE) {
                       return 1;
                   } else {
                       return 0;
                }
     } else {
         return 0;
     }
      $sql_query="UPDATE `training_schedule` SET `is_active` = '0' WHERE (`id` = '$id')";
      $result = $this->db->query($sql_query);
     if ($result === TRUE) {
        return 1;
    } else {
        return 0;
    }
    }


    public function update_profile_pic($data,$userId,$segment_id,$emp_code) {
      $filename=$_FILES['image_data']['name'];
      $location = ROOT . "/trainingportal/app/views/assets/images/avatar/".$_FILES['image_data']['name']; 
      $file_path= ROOT . "/trainingportal/app/views/assets/images/avatar/".$userId."_".$emp_code."";
      $file_path_store="app/views/assets/images/avatar/".$userId."_".$emp_code."/".$_FILES['image_data']['name'];
      $file_path_copy=ROOT . "/trainingportal/app/views/assets/images/avatar/".$userId."_".$emp_code."/".$_FILES['image_data']['name'];
      if (!file_exists($file_path)) {
        mkdir($file_path, 0777, true);
       }else if(file_exists($file_path_copy)){
        if (unlink($file_path_copy)) { 
        } 
       }
       move_uploaded_file($_FILES['image_data']['tmp_name'], $file_path_copy);

       $sql_query="UPDATE `usuarios` SET `profile_image_path` = '$file_path_store' WHERE (`id` = '$userId')";
       $result = $this->db->query($sql_query);
      if ($result === TRUE) {
         return 1;
     } else {
         return 0;
     }
    }

}