<?php
    
class Animal{

    private $con;

    public function __construct($db){
		$this->con = $db;
    }

    public function readAnimal($id, $lang){
        $lang_table = "animal_text_" . mysqli_real_escape_string($this->con, $lang);

        $sql = "SELECT ar.name as area,
        t.name,
        a.url_slika,
        t.endangerment_status,
        t.habitat,
        a.weight,
        t.diet,
        a.population
        FROM animal a 
        JOIN area ar ON ar.id_area = a.id_area 
        JOIN $lang_table t ON a.id_animal = t.id_animal
        WHERE a.id_animal = $id"; 
        $result = mysqli_query($this->con, $sql); 
        $data = array(); 
        $row = mysqli_fetch_assoc($result);
        $data = $row;  
        return $data; 
    }
    public function readAreaAnimals($data_input){
        $sql = "SELECT level FROM user where id_user='{$data_input["id_user"]}'"; 
        $result = mysqli_query($this->con, $sql); 
        $row = mysqli_fetch_assoc($result);
        $level = $row["level"];

        $lang_table = "animal_text_" . mysqli_real_escape_string($this->con, $data_input["lang"]);

        $sql = "SELECT
        a.id_animal,
        t.name,
        a.url_slika
        FROM animal a
        JOIN $lang_table t ON a.id_animal = t.id_animal
        WHERE a.id_area = '{$data_input["id_area"]}' and a.level <= $level"; 
        $result = mysqli_query($this->con, $sql); 
        $data["animal"] = array(); 
        while($row = mysqli_fetch_assoc($result)){
          $data["animal"][] = $row;  
        } 
        return $data; 
    }
    public function readAnimalModel($id){
      $sql = "SELECT url_model
      FROM animal 
      WHERE id_animal = $id"; 
      $result = mysqli_query($this->con, $sql); 
      $row = mysqli_fetch_assoc($result);
      $data = $row["url_model"];  
      return $data; 
  }
    public function readNames($id, $lang){
        $sql = "SELECT level FROM user where id_user=$id"; 
        $result = mysqli_query($this->con, $sql); 
        $row = mysqli_fetch_assoc($result);
        $level = $row["level"];

        $lang_table = "animal_text_" . mysqli_real_escape_string($this->con, $lang);

        $sql = "SELECT a.id_animal, t.name 
                FROM animal a
                JOIN $lang_table t ON a.id_animal = t.id_animal
                WHERE a.level <= $level";
    
        $result = mysqli_query($this->con, $sql);
        $data["animal"] = array();
    
        while ($row = mysqli_fetch_assoc($result)) {
            $data["animal"][] = $row;
        }
    
        return $data;
    }

}
?>