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

class Users extends CI_Controller {

	public function __construct(){
		parent::__construct();
		$this->load->model('Global_model');
	}

	public function index()
	{
		if (!$this->session->userdata('id_user')) {
			if(!empty($this->uri->segment(3))){
				$data['message'] = $this->uri->segment(3);
				$this->load->view('login',$data);
			}else{
				$data['message'] = null;
				$this->load->view('login',$data);
			}

		} else {
			
			if(!empty($this->uri->segment(3))){
				$data['message'] = $this->uri->segment(3);
				$this->load->view('login',$data);
			}else{
				$data['message'] = null;
				$this->load->view('login',$data);
			}
		}
	}
	public function login()
	{
		$user_login = array(
			'user_validation_code' => $this->input->post('code')
		);
		$user = $this->Global_model->login_user($user_login['user_validation_code']);
		if($user!=false){
			$countryId = $this->Global_model->getIdCountry($user_login['user_validation_code']);
			$checkIfGameActived = $this->Global_model->checkIfGameActived($countryId);
			if($checkIfGameActived==true){
				$data['user'] = $user;
				$data['pays'] = $this->Global_model->getCountry($user_login['user_validation_code']);
				$data['total'] = $this->Global_model->getTotal($user_login['user_validation_code']);
				$data['id_user'] = $this->Global_model->getIdUser($user_login['user_validation_code']);
				$data['membre'] = $this->Global_model->getMembre($data['id_user']);
				$data['message'] = "";
				$this->session->set_userdata('user_validation_code', $user_login['user_validation_code']);
				$this->session->set_userdata('user', $user);
				$this->session->set_userdata('pays', $data['pays']);
				$this->session->set_userdata('total', $data['total']);
				$this->session->set_userdata('id_user', $data['id_user']);
				$this->load->view('dashbord',$data);
			}else{
				$data['message'] = "Le jeu n'est pas activé pour votre pays";
				$this->load->view('login',$data);
			}
		}else{
			$data['message'] = "Ce code de validation est incorrecte";
			$this->load->view('login',$data);
		}
	}
	public function logout()
        {
			$this->session->sess_destroy();
			$message = 'Vous avez été déconnecté.';
			//$this->load->view('login',$data);
			redirect('index.php/');
        }
	public function add_member(){
		$validation_code = $this->input->post('code');
		$idNewMember = $this->Global_model->getIdUser($validation_code);
		if($idNewMember!=false){
			$codeExist = $this->Global_model->checkIfIdExist($idNewMember);
			$idPaysParrain = $this->Global_model->getIdCountry($this->session->userdata('user_validation_code'));
			$idPaysNewMember = $this->Global_model->getIdCountry($validation_code);
			if($codeExist==false){
				if($idPaysParrain==$idPaysNewMember){
					$res = $this->Global_model->addMember($this->session->userdata('id_user'),$idNewMember,$idPaysParrain);
					if($res==true){
						$this->session->set_userdata('total', $this->Global_model->getTotal($this->session->userdata('user_validation_code')));
						$data['membre'] = $this->Global_model->getMembre($this->session->userdata('id_user'));
						$data['message'] = 'Le membre a bien été ajouté';
						$this->load->view('dashbord',$data);
					}else{
						$data['message'] = "Une erreur s'est produite";
						$data['membre'] = $this->Global_model->getMembre($this->session->userdata('id_user'));
						$this->load->view('dashbord',$data);
					}
				}else{
					$data['message'] = "Le code de validation appartient à un autre pays.";
					$data['membre'] = $this->Global_model->getMembre($this->session->userdata('id_user'));
					$this->load->view('dashbord',$data);
				}
			}else{
				$data['message'] = "Ce code a déjà été ajouté ou appartient à un utilisateur participant au jeu en tant que parrain.";
				$data['membre'] = $this->Global_model->getMembre($this->session->userdata('id_user'));
				$this->load->view('dashbord',$data);
			}
		}else{
			$data['message'] = "Ce code est incorrecte ou ne correspond à aucun utilisateur.";
			$data['membre'] = $this->Global_model->getMembre($this->session->userdata('id_user'));
			$this->load->view('dashbord',$data);
		}
		
	}
}
