<?php
defined('BASEPATH') or exit('No direct script access allowed');

class Global_model extends CI_Model
{
	public function login_user($code)
	{
		$query = $this->db->query("SELECT id , lastname,phone, country_id , country FROM users_simple WHERE validation_code='" . $code . "'");
		if ($query->num_rows() > 0) {
			return $query->first_row();
		} else {
			return false;
		}
	}
//    public function getCountry($code){
//        $query = $this->db->query("SELECT countries.id,countries.name FROM `users` INNER JOIN `networks` ON `users`.network_id=`networks`.id INNER JOIN countries ON networks.country_id=countries.id WHERE users.validation_code='".$code."'");
//        if($query->num_rows()>0)
//        {
//            return $query->row()->name;
//        }
//        else{
//            return false;
//        }
//    }
	public function getTotal($code)
	{
		$query = $this->db->query("SELECT count(*) AS c
                                    FROM game
                                    INNER JOIN users ON game.id_parrain=users.id
                                    WHERE users.validation_code='" . $code . "'");
		if ($query->num_rows() > 0) {
			return $query->row()->c;
		} else {
			return null;
		}
	}

	public function getMembres($id_parrain)
	{
		$query = $this->db->query("SELECT users.lastname,users.validation_code FROM `game`
                                     INNER JOIN `users` ON `users`.id=`game`.id_joueur 
                                      WHERE `game`.id_parrain='" . $id_parrain . "'");
		if ($query->num_rows() > 0) {
			return $query;
		} else {
			return false;
		}
	}

	public function getSimpleUser($idParrain,$code)
	{
		$query = $this->db->query("SELECT id , country, country_id FROM users_simple WHERE id <> $idParrain AND validation_code='" . $code . "'");
		if ($query->num_rows() > 0) {
			return $query->first_row();
		} else {
			return false;
		}
	}

	public function addMember($id_parrain, $id_newMember, $id_pays)
	{
		$query = $this->db->query("INSERT INTO game(id_parrain, id_joueur, id_pays) VALUE (" . $id_parrain . "," . $id_newMember . "," . $id_pays . ")");
		return $query;
	}

	public function checkIfIdExist($id)
	{
		$query = $this->db->query("SELECT * FROM game WHERE id_joueur=" . $id . " OR id_parrain=" . $id);
		if ($query->num_rows() > 0) {
			return true;
		} else {
			return false;
		}
	}

	public function getIdCountry($code)
	{
		$query = $this->db->query("SELECT countries.id FROM `users` INNER JOIN `networks` ON `users`.network_id=`networks`.id INNER JOIN countries ON networks.country_id=countries.id WHERE users.validation_code='" . $code . "'");
		if ($query->num_rows() > 0) {
			return $query->row()->id;
		} else {
			return false;
		}
	}

	public function checkIfGameActived($id_pays)
	{
		$query = $this->db->query("SELECT * FROM `configGame` WHERE id_pays='" . $id_pays . "'");
		if ($query->num_rows() > 0) {
			if ($query->row()->etat == 1) {
				return true;
			} elseif ($query->row()->etat == 0) {
				return false;
			}
		} else {
			return false;
		}
	}
}
